链接列表不兼容的指针类型

时间:2015-12-05 02:54:06

标签: c pointers struct

我不明白为什么要分配' root'等于' trieu'将是一个不兼容的指针:/

#include <stdio.h>
#include <stdlib.h>

struct uniform {
    char size;
    int number;
    struct uniform *ext;
};

struct uniform *trieu;
struct unifrom *root;

int main(void) {
    trieu = malloc(sizeof(struct uniform));
    root = trieu;
...
    trieu = root;

当我用gcc编译它时,它给了我:

program.c: In function ‘main’:
program.c:15:7: warning: assignment from incompatible pointer type
  root = trieu;
   ^
program.c:57:8: warning: assignment from incompatible pointer type
  trieu = root;

之前我用另一个程序工作过:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ll {
    char store;
    struct ll *ext;
};

struct ll *trieu;
struct ll *root;

int main(int argc, char* argv[]) {
    trieu = malloc(sizeof(struct ll));
    root = trieu;
...

2 个答案:

答案 0 :(得分:1)

你有一个错字。使用struct uniform *root;代替struct unifrom *root;

答案 1 :(得分:-2)

首先,当使用malloc分配新的sturct时,你需要转换返回指针。

struct uniform {
    char size;
    int number;
    struct uniform *ext;
}UNIFORM;

trieu = (UNIFORM*)malloc(sizeof(UNIFORM));