问个简单问题

64288 2005-05-14 10:54:40
char buf[100]
stack textline
while (fgets(buf,100,file))//file为文件指针
{
char* string=(char*)malloc(strlen(buf)+1);//为什么要加1????
asser(string);
strcpy(string,buf);
textline.push(string);//把sting压栈;




...全文
128 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nerrymeeting 2005-05-16
  • 打赏
  • 举报
回复
顶,都很有道理.
CloudOfFly 2005-05-14
  • 打赏
  • 举报
回复
一楼已经说的很明白了
64288 2005-05-14
  • 打赏
  • 举报
回复
常量的定义要以通过"常量(赋以的值)"定义?是吗?
pdaliu 2005-05-14
  • 打赏
  • 举报
回复
:_id(i)是给
_id初始华!
64288 2005-05-14
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream.h>

class world
{
public:
world(int i)
:_id(i)//这是什么意思?是声明吗/
{
cout<<"Hello from "<<_id<<endl;
}
~world()
{
cout<<"Goodbye from "<<_id<<endl;
}
private:
const int _id;
};
int main(int argc, char* argv[])
{
for(int u=1;u<3;u++)
{
world Theworld(u);
}
}
zxw2844 2005-05-14
  • 打赏
  • 举报
回复
用一个类编写一段程序!!---让它按顺序输出"Hello 1, Goodbye 1, Hello 2, Goodbye2,"----方法越多越好!!!!-
-----我些了一个:----------各位帮忙写写!!!!!!!
#include "stdafx.h"
#include <iostream.h>

class world
{
public:
world(int i)
:_id(i)
{
cout<<"Hello from "<<_id<<endl;
}
~world()
{
cout<<"Goodbye from "<<_id<<endl;
}
private:
const int _id;
};
int main(int argc, char* argv[])
{
for(int u=1;u<3;u++)
{
world Theworld(u);
}
}



bxdx 2005-05-14
  • 打赏
  • 举报
回复
加个结束字符\0
blue_coco 2005-05-14
  • 打赏
  • 举报
回复
因为字符串是以'\0'结尾,只是不可见,对与一个字符串,strlen只是求出了其实际长度,起真正所占空间还要加上其结尾符号'\0',

eg: char *p = "abcd";
其长度为4, 但真正有五个符号, 'a', 'b',' c', 'd', '\0'


zhousqy 2005-05-14
  • 打赏
  • 举报
回复
char* string=(char*)malloc(strlen(buf)+1);//为什么要加1????
--------
嗯,放字符串结束符\0
llf_hust 2005-05-14
  • 打赏
  • 举报
回复
大家都说得很清楚了

64,635

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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