请问,关于C的几个基本问题

swall 2001-09-04 05:09:40
1、以下边形式定义的变量(指针?)可有大小限制?
  char *replacedstring;

2、为什么我用strcat()函数连的几个内容中间会出现空格,而且是很有规律的,每两个strcat()函数之间就会多很多空格?比如:
    strcat(resultstring,"<tr><td><p><a href='text.php?id=");
    strcat(resultstring,ids);
    strcat(resultstring,"'>");
    strcat(resultstring,"newstitle");
    strcat(resultstring,"</a></p></td></tr><tr><td><p>");

结果是:
<tr><td><p><a href='text.php?id=10341 '>newstitle </a></p></td></tr><tr><td><p><tr><td><p><a href='text.php?id=10327 '>newstitle税
...全文
113 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
swall 2001-09-05
  • 打赏
  • 举报
回复
另一个很奇怪的问题:
  下边这段sendmail的程序,发送的结果是replacedstring的内容少了,但在sendmail之前打出来的replacedstring的内容却是完整的。
为什么会这样?可能原因是什么?

fprintf(sendmail, "To: %s\n",email);
fprintf(sendmail, "From: XXX@XXX.com\n");
fprintf(sendmail, "Subject: free magazine\n");
fprintf(sendmail, "MIME-Version: 1.0\n");
fprintf(sendmail, "Content-Type: text/html; charset=gb2312 \n");
fprintf(sendmail, "\n%s\n",replacedstring);
swall 2001-09-05
  • 打赏
  • 举报
回复
对不起了各位,我调了一早上,发现空格问题是因为定义了ids为char ids[100],改小一些就好了。
frankx 2001-09-05
  • 打赏
  • 举报
回复
我揣测你的意思,改写你的SAMPLE,
char sTemp[8];
int ids = 1034;
for( i=0; i < 2 ; i++)
{
memset(sTemp, 0, sizeof(sTemp));
ids = ids - 14;
sprintf(sTemp,"%d",ids);
strcat(resultstring,"<tr><td><p><a href='text.php?id=");
strcat(resultstring,sTemp);
strcat(resultstring,"'>");
strcat(resultstring,"newstitle");
strcat(resultstring,"</a></p></td></tr><tr><td><p>");
}

你可以试一下。
swall 2001-09-05
  • 打赏
  • 举报
回复
真不好意思,上边的程序之所以出现两个不同的id,是因为结果是由循环产生的。我没有把循环写出来。
krerix 2001-09-05
  • 打赏
  • 举报
回复
同意楼上的意见
cnss 2001-09-04
  • 打赏
  • 举报
回复
id=xxxxx后面的空格数一样,所以可能是算ids函数写的有问题

结果有id=10341又有id=10327,显然是两个不resultstring,可能是你给resultstring留的空间太小,导致越界,跟别的连上了,所以乱了

你试试
1.检查生成ids串的部分
2.把resultstring留的空间大一点!

猜的,不对见谅
MountLion 2001-09-04
  • 打赏
  • 举报
回复
如果你要使用strcat,只能这样:
#define MAXLENGTH 10000
char replacedstring[MAXLENGTH]
或者
char *replacedstring=new char[len];
...
delete replacedstring;

上文中你的用法是错误的,使用了未定义的指针。
swall 2001-09-04
  • 打赏
  • 举报
回复
fsb_12345,什么不可能?
fsb_12345 2001-09-04
  • 打赏
  • 举报
回复
不可能
zzh 2001-09-04
  • 打赏
  • 举报
回复
char *replacedstring;
定义的是一个指针,是用来指向一个地址的,它本身没有限制,但它指向的地址肯定有大小的。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧