ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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);
/* 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?
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
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.
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.
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).
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.