LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Newbie problem with pointers in C. (https://www.linuxquestions.org/questions/programming-9/newbie-problem-with-pointers-in-c-113811/)

chris.hicks 11-08-2003 01:10 PM

Newbie problem with pointers in C.
 
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

ludwig 11-08-2003 01:52 PM

Hi Chris,

The problem is that you're attempting to use the word "struct" in your call to malloc. The typedef for your structure creates an alias tagname for the unnamed structure, making the "struct" unecessary. So if you get rid of the "struct" words in your malloc line, it should compile clean.

Good luck,

-- ludwig

chris.hicks 11-08-2003 02:00 PM

Thanks Ludwig. You are right.

I copied the offending line straight out of Dennis Alcock's book
'Illustrating C'!

Best regards,
Chris


All times are GMT -5. The time now is 12:36 PM.