Pie*_*ter 1 c malloc struct pointers operators
#include <stdio.h>
#include <stdlib.h>
typedef int element;
struct cell {
element e;
struct cell *p;
};
typedef struct cell* CELL;
int main() {
CELL* p;
p = (CELL*) malloc (sizeof(struct cell));
p->e = 8; /* This ain't working */
*p.e = 8; /* This doesn't help anything either */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我刚刚开始使用malloc,我刚刚创建了一个指向新创建的指针CELL,这是一个struct.现在我正试图用一些值来填充它,但是我在一个不是结构或联合的东西中对一个不友好的"成员e的请求"表示欢迎.我确实指向一个struct包含一个名为e的成员的指针,或者至少那是我的想法.为什么这会失败?
更改
cel* p;
p = (CELL*) malloc (sizeof(struct cell));
*p.e = 8;
Run Code Online (Sandbox Code Playgroud)
至
struct cell* p; // or CELL p;
p = (CELL) malloc (sizeof(struct cell));
(*p).e = 8; // . has higher precedence than *
Run Code Online (Sandbox Code Playgroud)