c分段故障结构

iva*_*123 0 c segmentation-fault

我有一个名为table的结构,我只想创建一个表,就像java中的构造函数一样,但是当我在main中调用这个函数时,它会给出分段错误

struct table *create(char *name,int number,char *s_name)
{
  struct table *newTable;
  newTable->name = name;
  newTable->number = number;
  newTable->s_name = s_name;
  return newTable;
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*oof 10

struct table *newTable = malloc(sizeof(struct table));
Run Code Online (Sandbox Code Playgroud)

不要忘记在使用它时自由调用,因为C没有像java那样的垃圾收集器.


Lar*_*abe 8

您尚未为该对象分配任何内存,并且正在取消引用该结构的字段.在访问newTable之前,您需要使用malloc为newTable分配内存