c - 尝试分配不兼容的类型时出错

标签 c types variable-assignment incompatibility

当我试图打印出这个列表时得到的错误是一个不兼容的类型错误。我尝试将其转换为结构宏、静态结构宏、指针,但都不起作用。

struct macro {
  struct macro *next;
  char * macro_name;
  char * macro_body;
};

static struct macro macro_list = {
  .next = NULL,
  .macro_name = NULL,
  .macro_body = NULL
};

//--------------------------------------------------------------------------------

void macro_list_print(void){
  printf("Printing macro_list\n");
  if(macro_list.next == NULL){
    printf("--No macros\n");
  }
  struct macro p = macro_list;
  while(p.next != NULL){
    printf("%s %s\n",p.macro_name,p.macro_body);
    p = macro_list.next; //This line gives me the error. 
  }
}

我不知道该做什么。任何帮助都会被挪用谢谢。

最佳答案

p 是一个 struct macromacro_list.next 是一个 struct macro*。更改为:

struct macro* p = &macro_list;
while(p != NULL){
    printf("%s %s\n",p->macro_name,p->macro_body);
    p = p->next;
}

我进行了以下额外更改:

  • macro_list.nextp->next,否则它永远不会超过列表中的第二项。
  • while 中的条件更改为 p != NULL,否则它不会在检查 p- 时处理列表中的最后一个元素>next != NULL

关于c - 尝试分配不兼容的类型时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625147/

相关文章:

c - EOF 未按预期工作 (C)

c - 如何在c中创建动态链表节点数组?

c - 使用 GDB 调试时读取变量 (C)

scala - 使用宏转换树后重新建立类型一致性的最佳方法是什么

python - 是否可以在 Python 中进行多行分配?

python - 使用索引的 Pandas 系列替换值

javascript - innerHTML 上的错误分配替换

c++ - 在C/C++中将变量名定义为__00000001有什么好处

python - 如何获取函数参数类型?

c++ - 为什么它试图发送一个字符数组而不是一个字符串?