Evan
Evan

Reputation: 3

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

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

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

Upvotes: 0

Views: 70

Answers (5)

atoMerz
atoMerz

Reputation: 7672

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

Upvotes: 0

rubixibuc
rubixibuc

Reputation: 7427

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.

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

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!

Upvotes: 0

Doug Currie
Doug Currie

Reputation: 41220

In your struct your should have

struct _stdata *nextptr;

Upvotes: 2

user142162
user142162

Reputation:

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.

Upvotes: 1

Related Questions