创建具有阵列单链表时警告 [英] Warnings when creating a singly linked list with arrays

查看:82
本文介绍了创建具有阵列单链表时警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;typedef结构
{
  int数据;
  结构节点*接下来的;
}节点;无效打印(节点*头)
{
  节点* TMP =头;
  而(TMP)
  {
    的printf(%d个,tmp->数据);
    TMP = tmp->接下来,
  }
}诠释的main()
{
  节点改编[5] = {
                  {1,&安培;常用3 [1]},
                  {2,&安培;常用3 [2]},
                  {3,&放大器;常用3 [3]},
                  {4,&放大器;常用3 [4]},
                  {5,NULL}
                };  打印(ARR);
  返回0;
}

为什么我得到这些警告,同时用gcc编译-Wall? (即使没有-Wall,GCC产生相同的警告)

  list.c:在功能打印:
list.c:15:7:警告:分配从兼容的指针类型[默认启用]
list.c:在函数'主':
list.c:22:18:警告:初始化从兼容的指针类型[默认启用]
list.c:22:18:警告:(近初始化改编[0]。接下来')[默认启用]
list.c:23:18:警告:初始化从兼容的指针类型[默认启用]
list.c:23:18:警告:(近初始化改编[1]。接下来')[默认启用]
list.c:24:18:警告:初始化从兼容的指针类型[默认启用]
list.c:24:18:警告:(近初始化为'ARR [2]。接下来')[默认启用]
list.c:25:18:警告:初始化从兼容的指针类型[默认启用]
list.c:25:18:警告:(近初始化改编[3]。接下来')[默认启用]


解决方案

您尝试使用结构节点 节点的定义中因此这个编译器不知道你的意思是他们是同样的事情。尝试着先声明结构:

 结构节点;
结构节点
{
  int数据;
  结构节点*接下来的;
};

#include <stdio.h>

typedef struct
{
  int data;
  struct node *next;
}node;

void print(node *head)
{
  node *tmp = head;
  while (tmp)
  {
    printf ("%d ", tmp->data);
    tmp = tmp->next;
  }
}

int main()
{
  node arr[5] = {
                  {1, &arr[1]},
                  {2, &arr[2]},
                  {3, &arr[3]},
                  {4, &arr[4]},
                  {5, NULL}
                };

  print(arr);
  return 0;
}

Why do i get these warnings while compiling with gcc -Wall ? (even without -Wall, gcc produces the same warnings)

list.c: In function ‘print’:
list.c:15:7: warning: assignment from incompatible pointer type [enabled by default]
list.c: In function ‘main’:
list.c:22:18: warning: initialization from incompatible pointer type [enabled by     default]
list.c:22:18: warning: (near initialization for ‘arr[0].next’) [enabled by default]
list.c:23:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:23:18: warning: (near initialization for ‘arr[1].next’) [enabled by default]
list.c:24:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:24:18: warning: (near initialization for ‘arr[2].next’) [enabled by default]
list.c:25:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:25:18: warning: (near initialization for ‘arr[3].next’) [enabled by default]

解决方案

You are trying to use struct node inside the definition of node so therefore the compiler doesn't know you mean them to be the same thing. Try forward declaring the struct first:

struct node;
struct node
{
  int data;
  struct node *next;
};

这篇关于创建具有阵列单链表时警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆