淮阳个人急售二手房:关于指针的简单概念
来源:百度文库 编辑:高校问答 时间:2024/05/08 19:53:21
char a[] = “hello”;
int len = strlen(a);
char *p = (char *)malloc(sizeof(char)*(len+1));
strcpy(p, a);
if (strcmp(p, a) == 0)
cout<<p<<endl;
free(p);
上面的这句:
char *p = (char *)malloc(sizeof(char)*(len+1));
不知道到底分配了多大空间?为什么会这么写呢?哪位兄弟能否解答一下?
int len = strlen(a);
char *p = (char *)malloc(sizeof(char)*(len+1));
strcpy(p, a);
if (strcmp(p, a) == 0)
cout<<p<<endl;
free(p);
上面的这句:
char *p = (char *)malloc(sizeof(char)*(len+1));
不知道到底分配了多大空间?为什么会这么写呢?哪位兄弟能否解答一下?
5+1个字节,五个字节为了存在 hello 的,另外一个为了存储字符串的结束符 '\0' 的