c编程,链表错误

标签 c

当我尝试编译以下代码时,出现错误

test.c: In function 'main':
test.c:16: error: incompatible types in assignment

代码是..

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

typedef struct {
    char name[20];
    struct planet* next;
} planet;


int main(int argc, char *argv[])
{
    planet *p, *start, *first, *second, *third;

    strcpy(start->name, "Suthan");
    start->next = *first;

}

最佳答案

1) 为您的指针分配一些内存。

planet *start, *first; these are uninitialized pointers.

start->next // that's dereferencing an uninitialized pointer.

2) 您正在将 start->next(指向行星的指针)设置为 first 的引用指针。这就是你错误的根源。

start->next = *first;  // should be  = first;

3) 移动 typedef 名称以消除您看到的警告。

typedef struct planet{
   char name[20];
   struct planet* next;
};

关于c编程,链表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13808146/

相关文章:

c - MPI并行编程

c - 从字符串数组中搜索字符串

c - 链接库头时对...的 undefined reference

c - C 中的 Metropolis 函数的意外输出

mysql - C 程序使用 MySQL API 进行备份?

c++ - 运行时类型定义

c - C语言中整数和单个字符赋值时如何理解指针?

php - 在某些情况下无法从 PHP 停止 C 守护进程

c - gcc 找不到-lX11

c - Internet Checksum 函数是否移动 8bits