LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2003, 01:10 PM   #1
chris.hicks
Member
 
Registered: Sep 2003
Location: Newcastle upon Tyne
Distribution: Red Hat 9
Posts: 42

Rep: Reputation: 15
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
 
Old 11-08-2003, 01:52 PM   #2
ludwig
Member
 
Registered: Jun 2002
Location: Orange County, CA
Distribution: Debian (squeeze), kernel 2.6.30-2-amd64
Posts: 32

Rep: Reputation: 15
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
 
Old 11-08-2003, 02:00 PM   #3
chris.hicks
Member
 
Registered: Sep 2003
Location: Newcastle upon Tyne
Distribution: Red Hat 9
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks Ludwig. You are right.

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

Best regards,
Chris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 05:14 AM
Yet another pointers problem in C chris.hicks Programming 3 11-10-2003 08:35 AM
simple problem with pointers Jo_Nak Programming 2 06-17-2003 04:46 PM
Need some pointers in this problem (inside...) Vlad_M Linux - General 8 09-04-2002 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration