【发布时间】:2010-09-02 12:16:47
【问题描述】:
如何为结构指针分配内存并为其在子函数中的成员赋值?
以下代码将编译但不执行:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _struct {char *str;};
void allocate_and_initialize(struct _struct *s)
{
s = calloc(sizeof(struct _struct), 1);
s->str = calloc(sizeof(char), 12);
strcpy(s->str, "hello world");
}
int main(void)
{
struct _struct *s;
allocate_and_initialize(s);
printf("%s\n", s->str);
return 0;
}
【问题讨论】:
标签: c