Casting items to end of linked list in C

随声附和 提交于 2019-12-11 23:08:30

问题


EDIT*(8:14 PM) - Sorry I corrected my code and made this instead a method so it can be more easily understood.

I am not sure how to properly cast a struct when adding to the end of a linked list. Compiling this code gives me an cast warning at the very last line. This may be the reason why the rest of my code does not properly function.

For example:

#include <stdlib.h>

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

node *HEAD = NULL;

node *addNode(int num)
{
    if (HEAD == NULL) {
        HEAD = (node *)malloc(sizeof(node));
        HEAD->next = NULL;
        HEAD->data = num;
    }
    else {
        node *newNode;
        newNode = (node *)malloc(sizeof(node));
        newNode->data = num;
        newNode->next = NULL;

        node *iter;
        iter = (node *)malloc(sizeof(node));
        iter = (node *)HEAD;

        while(iter->next != NULL)
            iter = (node *)iter->next;

        iter->next = newNode; //warning : warning: assignment from incompatible pointer type
    } 
    return HEAD;
}

回答1:


  • Make sure to include stdlib.h -- needed to use malloc
  • fix all occurance of wordNode to be node -- wordNode is undefined in your program
  • create a struct and typedef both named node -- standard trick for self referential structs

and then all your warnings goes away;

#include <stdlib.h>
struct node{
  int data;
  struct node *next;
};
typedef struct node node;


node *HEAD = NULL;

int main(int argc, char*argv[]) {

  int x = 1;
  int y = 2;

  if(HEAD == NULL)
    {
      HEAD = (node *)malloc(sizeof(node));
      HEAD->next = NULL;
      HEAD->data = x;
    }
  else
    {
      node *newNode;
      newNode = (node *)malloc(sizeof(node));
      newNode->data = y;
      newNode->next = NULL;

      node *iter;
      iter = (node *)malloc(sizeof(node));
      iter = (node *)HEAD;

      while(iter->next != NULL)
    iter = (node *)iter->next;

      iter->next = newNode; //warning : warning: assignment from incompatible pointer type
      return 0;
    }
}



回答2:


The problem is that you declare "next" to be a pointer to "struct node" before the struct is completely defined, so "next" points to an undefined structure. If you change "typedef struct{" to "typedef struct node{", that error will be gone.




回答3:


There is a number of problems with your code. The first one would be casting the return value of malloc and improperly referring to the size of the type for which you want some space to be allocated :

HEAD = (node *)malloc(sizeof(node));

should be replaced by

HEAD = malloc(sizeof(*HEAD))

Since the conversion from void* to any other type is always defined and implicit in C, you don't get any warnings about a needed cast. Specifying sizeof(*HEAD) makes the compiler automatically choose the type of HEAD at compile time, thus reducing the needed work should the type ever change.

You should also remember that some compilers don't like anonymous structures (i.e. structures without a name declared). Therefore, the code

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

should be replaced by

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

Which declares a structure called _node, typedefed to the type called node. And also fixes the circular reference.

On top of all that, you don't need to malloc any space for the iter.



来源:https://stackoverflow.com/questions/9057779/casting-items-to-end-of-linked-list-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!