我四处寻找如何解决我的问题。我找到类似于我的问题的解决方案,但是当我应用更改错误时:error: request for member 'mark' in something not a structure or union
不断显示。为函数中的char数组指针分配内存
我到目前为止是struct
,我想通过函数调用来初始化它。
编辑我的代码:
typedef struct student * Student;
struct student {
char *mark; /* or array[2] like array[0] = 'A' , array[1] = 'B' */
int age;
int weight;
};
typedef enum{
MEMORY_GOOD,
MEMORY_BAD} Status;
int main(int argc, char* argv[]) {
int status = 0;
Student john
/* Function call to allocate memory*/
status = initMemory(&john);
return(0);
}
Status initMemory(Student *_st){
_st = malloc(sizeof(Student));
printf("Storage size for student : %d \n", sizeof(_st));
if(_st == NULL)
{
return MEMORY_BAD;
}
_st->mark = malloc(2*sizeof(char)); /* error: request for member 'mark' in something not a structure or union */
return MEMORY_GOOD;
}
Postfix' - >'具有比一元'*'更高的优先级。 – EOF
此代码充满语法错误。而且,'john'不是一个指向结构体的指针。 – melpomene
@EOF即使添加parens也无济于事。 '_st'是一个三重指针; '*'和' - >'只能解除引用两次。 – melpomene