cancel
Showing results for 
Search instead for 
Did you mean: 

Newbie C programmer having problems with pointers

gosforth
Grafter
Posts: 109
Registered: 11-10-2007

Newbie C programmer having problems with pointers

I am trying to teach myself C (using gcc 3.3.1). I am trying
to write a simple example that does a double linked list.
I seem to be falling at the first hurdle when I try and
create the pointer. My program is:

/*look at the use of structures and pointers to produce a
double linked list*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>


int main (void)
{
typedef struct
{
int time;
struct event *fptr,*bptr;
} event;

event *baseptr;

baseptr = (struct event *) malloc ( sizeof (struct event));

}

The compiler error is:

doublelinked.c:18: error invalid application of 'sizeof' to an incomplete type
doublelinked.c:18: warning assignment from incompatible pointer type

I have obviously done something daft, but would be grateful if some kind
person could tell me what is wrong.

Thanks,
Chris
2 REPLIES
N/A

Re: Newbie C programmer having problems with pointers

A few comments.

In your typedef statement, you seem to be defining a pointer to the type you are defining inside the definition.

typedef Struct {
int time;
struct event *fptr,*bptr; <<<-----------
} event;

How can the compile know what event is? It has not been defined yet.

Lets assume you define a structure

typedef struct {
// Some stuff here.

} eventT;

eventT *baseptr;

then,

baseptr = (eventT*) malloc ( sizeof (eventT));

[Moderators note (by Thomas): Quote removed, as it was a duplicate of the entire of the previous post, deeming it unnecessary.]
N/A

Newbie C programmer having problems with pointers

/*look at the use of structures and pointers to produce a
double linked list*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>


int main (void)
{

struct event
{
int time;
struct event *fptr,*bptr;
};

struct event *baseptr;

baseptr = (struct event *) malloc ( sizeof (struct event));

}


This one compiles cleanly.