开发者

Making a linked list and the problem is it keeps saying the pointer type is incompatible

开发者 https://www.devze.com 2023-02-28 14:20 出处:网络
struct _st开发者_开发百科data{ int stid; int sttype; struct stdata *nextptr; }; typedef struct _stdata stdata;
struct _st开发者_开发百科data{
    int stid;
    int sttype;
    struct stdata *nextptr;
};

typedef struct _stdata stdata;
stdata *new = malloc(sizeof(stdata));
new->nextptr = new;


In your struct your should have

struct _stdata *nextptr;


It's how your definition is set up. The following should work for you:

typedef struct stdata stdata;
struct stdata
{
    int stid;
    int sttype;
    stdata *nextptr;
};

Alternatively, you can do what Doug suggested, but typedefing your struct like this makes your code cleaner.


Is the error/warning on this line?

stdata *new = malloc(sizeof(stdata));

You just need to include a cast:

stdata *new = (stdata*) malloc(sizeof(stdata));

By the way "new" is a terrible variable name, as it's a reserved word in C++ and it looks very confusing to most people!


There are a few problems with your code but to get it to compile, you must change the pointer type in the struct to stdata, struct _stdata is causing you problems. Also place the typedef at the top of file, and include stdlib.h for malloc.


in your struct you've used stdata before defining it. you should use _stdata instead

0

精彩评论

暂无评论...
验证码 换一张
取 消