我在使用指针时如何分配信息有问题。传递和分配指针C
我不能在readName
函数中分配任何值。你能检查我的malloc结构是否正确吗? 或 有没有另一种方法来做到这一点,而不改变敲击和函数参数?
typedef struct name
{
char info[];
int number;
//some more codes
} name;
typedef struct Data
{
name ** n;
//some more codes
} Data;
int readName(FILE *const fp, name **const names)
{
(*names)->number = 1; // no idea what to put here to store
strcat ((*names)->info, "aBC");
//codes
}
int read(FILE *const fp, Data *const data)
{
data->n = malloc(sizeof(name*)*1); // am I mallocing correctly?
data->n[0]=malloc(sizeof(name));
i = readName(fp, &data->n[Data->n]);
//codes
}
int main()
{
Data * d;
d = malloc (sizeof (Data));
i = read(fp, d); //assume fp is decleared
//codes that usses the struct
}
你需要声明一个大小'信息[]'。 – Barmar
'info'上的'strcat()'没有明显的'\ 0'初始化'info'。 – chux
“双指针”可能意味着指向“double”或指向某个指针的指针。按照惯例,它通常被理解为意味着前者。如果这不是你想要的,请编辑你的问题来重新修改。 – keshlam