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-10-2003, 05:02 AM   #1
chris.hicks
Member
 
Registered: Sep 2003
Location: Newcastle upon Tyne
Distribution: Red Hat 9
Posts: 42

Rep: Reputation: 15
Another C pointers question from a newbie


I have now completed a small example that uses a double linked list to place events in a list in the correct order. The program works fine. However, when I compile the program I get some warnings and I don't know why. (I am using gcc 3.2.1 on RH9, the warnings are listed after my code).

My program is:
/*look at the use of structures and pointers to produce a
double linked list that lists events in time order*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>


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

event *baseptr,*newptr,*tempptr,*tf,*tb;

/* create the base element for the queue */
baseptr = (event *) malloc (sizeof (event));

/* set up the base of the queue to point to itself with time = 0 */
baseptr->time = 0;
baseptr->fptr = baseptr;
baseptr->bptr = baseptr;

/*enter a loop to collect events */
time = 999;
while (time > 0)
{
printf("Enter the time for the next event (t = 0 for quit) ");
scanf("%i",&time);

if (time == 0) printf("quiting\n"); else
{

/*create a new pointer */
newptr = (event *) malloc (sizeof (event));
newptr->time = time;
newptr->bptr = newptr; /* initialise */
newptr->fptr = newptr;

/* place the event into the double linked list in the correct place */
tempptr = baseptr; /*start off at the root */
cont = 1;
do /* loop to work back until correct entry point found */
{
tempptr = tempptr->bptr;
if (newptr->time > tempptr->time) cont = 0;
if (tempptr == baseptr) cont = 0;
} while (cont == 1);

/* rearrange the pointers to enter the new event */
newptr->fptr = tempptr->fptr;
newptr->bptr = tempptr;
tb = tempptr->fptr;
tb->bptr = newptr;
tempptr->fptr = newptr;
}
} /* end of entering new events */

/* now we will write the events out in order */
tempptr = baseptr->bptr;
printf("Event pointer %p time = %i\n",tempptr,tempptr->time);
while (tempptr != baseptr)
{
tempptr = tempptr->bptr;
printf("Event pointer %p time = %i\n",tempptr,tempptr->time);
}
}

The warning messages are:
[nmansys@mmme1 c_examples]$ gcc doublelinkedlist.c
doublelinkedlist.c: In function `main':
doublelinkedlist.c:24: warning: assignment from incompatible pointer type
doublelinkedlist.c:25: warning: assignment from incompatible pointer type
doublelinkedlist.c:40: warning: assignment from incompatible pointer type
doublelinkedlist.c:41: warning: assignment from incompatible pointer type
doublelinkedlist.c:48: warning: assignment from incompatible pointer type
doublelinkedlist.c:55: warning: assignment from incompatible pointer type
doublelinkedlist.c:56: warning: assignment from incompatible pointer type
doublelinkedlist.c:57: warning: assignment from incompatible pointer type
doublelinkedlist.c:58: warning: assignment from incompatible pointer type
doublelinkedlist.c:63: warning: assignment from incompatible pointer type
doublelinkedlist.c:67: warning: assignment from incompatible pointer type

I can't see anything wrong with my code. Has anybody got any ideas please?

Best regards,
Chris
 
Old 11-10-2003, 06:35 AM   #2
Quis
Member
 
Registered: May 2001
Location: Germany, Süd-Baden
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
You should try to link your pointers, like this:

24: baseptr->fptr = baseptr->bptr; //forwarptr=forwardptr or forwardptr=backwardptr
25: baseptr->bptr = baseptr->fptr; // see above (make a loop)
..
40: and 41: try the same

..
 
Old 11-10-2003, 06:52 AM   #3
chris.hicks
Member
 
Registered: Sep 2003
Location: Newcastle upon Tyne
Distribution: Red Hat 9
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks for the response. I'm not sure I understand the suggestion. If I print out baseptr, baseptr->fptr and baseptr->bptr they are all have the same memory address, which is what I am wanting. On these lines the pointers are of the same type, namely event, so why is the compiler producing the warning "warning: assignment from incompatible pointer type"?

Any further help would be very gratefully received. Thanks. Chris.
 
Old 11-10-2003, 07:33 AM   #4
Quis
Member
 
Registered: May 2001
Location: Germany, Süd-Baden
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
ok, forget my post, i tried to debug the program with my suggestion, the warnings are gone, but the address also. Sorry, for sure this is not what a linked list should do.
 
Old 11-10-2003, 08:01 AM   #5
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Your declaration of the event type should look something like this:

typedef struct eventnode *eventptr;

typedef struct eventnode
{
int time;
eventptr fptr,bptr;
} event;

Regards
Martin
 
Old 11-11-2003, 03:12 AM   #6
chris.hicks
Member
 
Registered: Sep 2003
Location: Newcastle upon Tyne
Distribution: Red Hat 9
Posts: 42

Original Poster
Rep: Reputation: 15
I have checked with a book "C a Software Engineering Approach", Darnell and Margolis (1996) and think that my definition of the type event is OK. The program also works fine. However, I am clearly missing something, because my next example to process trees is also coming up with similar warnings. (I have raised this issue in another thread).
 
Old 11-11-2003, 05:14 AM   #7
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Ok I'm a C++ programmer and am not 100% sure this is correct since it has been many years since I have done any C programming (I know, I know, lame excuse ) So correct me if I'm wrong.

I think that the code:

Code:
typedef struct
{
  int time;
  struct event *fptr,*bptr; 
} event;
only creates an instance (called event) of an unnamed struct.

The syntax (from memory) for definining a struct:
Code:
typedef struct <typename>
{
// body
} <instance>;
so to create a new type called myNewType but no instances, you would use the syntax:

Code:
typedef struct myNewType
{
  int uselessStructBody;
};
Now to create an instance of this use:

Code:
myNewType myInstance;
The previous two code segments are equivalent to:

Code:
typedef struct myNewType
{
  int uselessStructBody;
} myInstance;

note: Please learn to use the [C0DE] tags.. as it makes it soooooo much easier to read your code..

Cheers..
 
  


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
question about pointers vijeesh_ep Programming 1 09-13-2004 03:13 PM
simple c++ question (pointers) true_atlantis Programming 1 02-05-2004 06:34 PM
Newbie problem with pointers in C. chris.hicks Programming 2 11-08-2003 02:00 PM
question on pointers h/w Programming 1 10-26-2003 02:54 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

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

All times are GMT -5. The time now is 12:18 AM.

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