我还在学习C并且在尝试malloc结构时遇到了一些麻烦。我有以下结构:
struct data {
int *ref;
int *port;
char data[MAX_STRING];
}temp, valid, invalid;
我打算使用' temp' struct在验证之前存储输入数据,然后根据验证结果将结构复制到有效或无效结构数组的成员。
int main(){
char inputfile[100];
FILE *file; = fopen("file.txt" , "r");
if (file != NULL){
read_file (file);
}
else{
// Some code here..
}
return 0;
}
void read_file(FILE *file)
{
char buf[1024];
while(!feof (file))
{
struct temp *p_temp = malloc(sizeof(temp));
p_temp->ref = malloc(sizeof(int)); <<<<<<< 'dereferencing pointer to incomplete type'
p_temp->port = malloc(sizeof(int)); <<<<<<< 'dereferencing pointer to incomplete type'
p_temp->data = malloc(sizeof(MAX_STRING)); <<<<<<< 'dereferencing pointer to incomplete type'
fgets(buf, sizeof buf, file);
sscanf(buffer, "%d.%d.%s", p_temp->ref, p_temp->port, p_temp->data);
validate();
}
}
我是否已经开始使用malloc以正确的方式使用临时结构?我收到错误&#39;取消引用不完整类型&#39;
然后我将如何创建一个有效且无效的malloced结构数组?我会创建一个函数,如:
vaild* vaild_record(){
struct vaild *p_vaild = malloc(sizeof(vaild));
if ( p_vaild == NULL)
{
// some code here
}
p_vaild->ref= malloc(sizeof(int));
p_vaild->port = malloc(sizeof(int));
p_vaild->data =(char)malloc(sizeof(STRINGMAX);
if ( p_vaild->ref == NULL || p_vaild->port == NULL || p_vaild->data == NULL)
{
// some code here
}
return 0;
}
我对整件事感到有点困惑。任何清晰度都会非常感谢。
答案 0 :(得分:3)
你的问题是你应该声明结构的方式
struct data
{
int *ref;
int *port;
char data[MAX_STRING];
};
然后你做
struct data *p_valid;
p_valid = malloc(sizeof(struct data));
另一件事是
p_valid->data = malloc(sizeof(STRINGMAX));
是错误的,因为data
不是指针。并且sizeof(STRINGMAX)
也是错误的,因为STRINGMAX
似乎是一个宏,因此它会扩展到它的价值,如果你有#define STRINGMAX 4
那么它会扩展到{{1} }}
答案 1 :(得分:1)
Your definition of the struct:
struct data {
int *ref;
int *port;
char data[MAX_STRING];
}temp, valid, invalid;
should be more like this:
struct data
{
int *ref;
int *port;
char data[MAX_STRING];
};
then define the arrays similar to this:
struct data temp;
struct data* valid = NULL;
struct data* invalid = NULL;
int currentValidSize = 0;
int currentInvalidSize = 0;
struct data * validTemp = NULL;
struct data * invalidTemp = NULL;
then, each time the code needs room for (another) instance of a struct
struct data *validTemp = realloc(valid, (currentValidSize+1)* sizeof(data) );
if( NULL == validTemp )
{ // realloc failed
perrof( "realloc failed" );
// free everything, close files, etc here probably be writing a sub function
// and calling it here.
// a sub function that:
// that walks the valid and invalid arrays,
// first free'ing any malloc'd fields
// then finally free'ing the whole array
exit( EXIT_FAILURE );
}
// implied else, realloc successful
// update array size counter
currentValidSize++;
// update ptr to valid array of structs
valid = validTemp;
validTemp = NULL;
similar for adding an entry to the invalid array of structs
then update the valid array of structs from temp as:
(note the '-1' in the offset into valid[])
memcpy( &valid[currentValidSize-1], &temp, sizeof data );
// Note you will also have to perform a 'deep' copy of any areas
// that were malloc'd within the 'temp' struct
答案 2 :(得分:-1)
从快速查看,我认为您的问题可能是您在read_file函数中进行mallocing的地方。你应该有malloc(sizeof(struct temp))
。
同时施放malloc也不会受到伤害。
此外,结构末尾不需要temp, valid, invalid
,只需使用struct data
希望这有帮助