【发布时间】:2014-02-21 02:23:28
【问题描述】:
我在使用指针时无法分配信息。
我无法在readName 函数中分配任何值。你能检查一下我是否正确地分配了结构吗?
或者
有没有其他方法可以做到这一点而不改变敲击和函数参数?
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
}
【问题讨论】:
-
你需要为
info[]声明一个大小。 -
strcat()oninfo没有明显的\0初始化info。 -
“双指针”可能意味着指向
double的指针或指向某事物的指针。按照惯例,它通常被理解为前者。如果这不是您想要的,请编辑您的问题以重新表述。
标签: c pointers struct pointer-to-pointer