Help getting this to compile

Discussion in 'C Programming' started by Allan M. Bruce, Jul 20, 2006.

  1. I have a small implementation of a queue which I am trying to get to compile
    but cant figure out why it doesnt work. I have copied the minimum
    compilable code below. On gcc I get

    temp2.c: In function `pop':
    temp2.c:24: warning: initialization from incompatible pointer type
    temp2.c:25: warning: assignment from incompatible pointer type
    temp2.c: In function `destroy':
    temp2.c:38: error: dereferencing pointer to incomplete type

    In line 24, I am trying to intialise e which is an (entry *) to *xiQueue
    which is a *(entry **) which should be an (entry *), no?
    In line 38, I am dereferencing xiQueue which should give me an (entry *) so
    I can access the members of the struct with ->, no?

    In Visual C I get even more errors:

    queue.c(24): error C2275: 'entry' : illegal use of this type as an
    expression
    queue.c(24): error C2065: 'e' : undeclared identifier
    queue.c(25): error C2223: left of '->nextNode' must point to struct/union
    queue.c(26): error C2223: left of '->data' must point to struct/union
    queue.c(27): warning C4022: 'free' : pointer mismatch for actual parameter 1
    queue.c(38): error C2037: left of 'nextNode' specifies undefined
    struct/union 'entry'

    Perhaps I am misunderstanding how to use typedef to make the queue
    effectively an entry *. Can anybody help?
    Thanks
    Allan



    #include <stdio.h>
    #include <stdlib.h>

    /*****************************************/
    enum ERROR_CODES {ERROR, SUCCESS};

    typedef struct entry *queue;

    int pop(queue *, int *);
    void destroy(queue *);
    /*****************************************/

    typedef struct tagEntry
    {
    int data;
    struct tagEntry *nextNode;
    } entry;

    int pop(queue *xiQueue, int *xoData)
    {
    if (xiQueue == NULL)
    return ERROR;

    entry *e = *xiQueue;
    *xiQueue = e->nextNode;
    *xoData = e->data;
    free(e);

    return SUCCESS;
    }

    void destroy(queue *xiQueue)
    {
    if (xiQueue == NULL)
    return;

    int lDummy;
    while ((*xiQueue)->nextNode != NULL)
    pop(xiQueue, &lDummy);
    xiQueue = NULL;
    }

    int main(void)
    {
    return 0;
    }
    Allan M. Bruce, Jul 20, 2006
    #1
    1. Advertising

  2. Allan M. Bruce

    Richard Bos Guest

    "Allan M. Bruce" <> wrote:

    > In Visual C I get even more errors:
    >
    > queue.c(24): error C2275: 'entry' : illegal use of this type as an
    > expression
    > queue.c(24): error C2065: 'e' : undeclared identifier
    > queue.c(25): error C2223: left of '->nextNode' must point to struct/union
    > queue.c(26): error C2223: left of '->data' must point to struct/union
    > queue.c(27): warning C4022: 'free' : pointer mismatch for actual parameter 1


    Compiling as C++, are you?

    > queue.c(38): error C2037: left of 'nextNode' specifies undefined
    > struct/union 'entry'


    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > /*****************************************/
    > enum ERROR_CODES {ERROR, SUCCESS};
    >
    > typedef struct entry *queue;

    ^^^^^^^^^^^^

    > int pop(queue *, int *);
    > void destroy(queue *);
    > /*****************************************/
    >
    > typedef struct tagEntry

    ^^^^^^^^^^^^^^^
    > {
    > int data;
    > struct tagEntry *nextNode;
    > } entry;

    ^^^^^

    That's your problem. There's no such thing as a struct entry. It's
    either an entry, but you can't use that before it's defined; or it's a
    struct tagEntry. It should be

    typedef struct tagEntry *queue;

    Alternatively, you could replace struct tagEntry with struct entry in
    the struct definition. Structure tags live in a namespace of their own,
    so struct entry and typedef entry do not clash.

    Richard
    Richard Bos, Jul 20, 2006
    #2
    1. Advertising

  3. "Richard Bos" <> wrote in message
    news:4all.nl...
    > "Allan M. Bruce" <> wrote:
    >
    >> In Visual C I get even more errors:
    >>
    >> queue.c(24): error C2275: 'entry' : illegal use of this type as an
    >> expression
    >> queue.c(24): error C2065: 'e' : undeclared identifier
    >> queue.c(25): error C2223: left of '->nextNode' must point to struct/union
    >> queue.c(26): error C2223: left of '->data' must point to struct/union
    >> queue.c(27): warning C4022: 'free' : pointer mismatch for actual
    >> parameter 1

    >
    > Compiling as C++, are you?
    >
    >> queue.c(38): error C2037: left of 'nextNode' specifies undefined
    >> struct/union 'entry'

    >
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> /*****************************************/
    >> enum ERROR_CODES {ERROR, SUCCESS};
    >>
    >> typedef struct entry *queue;

    > ^^^^^^^^^^^^
    >
    >> int pop(queue *, int *);
    >> void destroy(queue *);
    >> /*****************************************/
    >>
    >> typedef struct tagEntry

    > ^^^^^^^^^^^^^^^
    >> {
    >> int data;
    >> struct tagEntry *nextNode;
    >> } entry;

    > ^^^^^
    >
    > That's your problem. There's no such thing as a struct entry. It's
    > either an entry, but you can't use that before it's defined; or it's a
    > struct tagEntry. It should be
    >
    > typedef struct tagEntry *queue;
    >
    > Alternatively, you could replace struct tagEntry with struct entry in
    > the struct definition. Structure tags live in a namespace of their own,
    > so struct entry and typedef entry do not clash.
    >
    > Richard


    Hehe, as easy as that - I thought it might be!
    Thanks Richard
    Allan
    Allan M. Bruce, Jul 20, 2006
    #3
  4. Allan M. Bruce wrote:
    >
    > Perhaps I am misunderstanding how to use typedef to make the queue
    > effectively an entry *.


    Yes, you are:

    > typedef struct entry *queue;

    The above is wrong. 'struct entry' is meaningless. Try, if you _must_
    use promiscuous typedefs,

    typedef struct tagEntry *queue;


    > typedef struct tagEntry
    > {
    > int data;
    > struct tagEntry *nextNode;
    > } entry;
    Martin Ambuhl, Jul 20, 2006
    #4
  5. Groovy hepcat Allan M. Bruce was jivin' on Thu, 20 Jul 2006 11:31:31
    +0100 in comp.lang.c.
    Help getting this to compile's a cool scene! Dig it!

    >In Visual C I get even more errors:
    >
    >queue.c(24): error C2275: 'entry' : illegal use of this type as an
    >expression
    >queue.c(24): error C2065: 'e' : undeclared identifier
    >queue.c(25): error C2223: left of '->nextNode' must point to struct/union
    >queue.c(26): error C2223: left of '->data' must point to struct/union


    These errors result from the fact that you are declaring a variable
    after executing statements. Move declarations to the top of the block
    in which they are declared.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Jul 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nagaraj
    Replies:
    1
    Views:
    852
    Lionel B
    Mar 1, 2007
  2. timjowers
    Replies:
    7
    Views:
    4,336
    heyjude
    Feb 2, 2011
  3. miller587

    getting compile error, please help

    miller587, Oct 5, 2007, in forum: Java
    Replies:
    0
    Views:
    279
    miller587
    Oct 5, 2007
  4. Replies:
    1
    Views:
    445
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  5. Carter
    Replies:
    2
    Views:
    499
    Carter
    Mar 4, 2009
Loading...

Share This Page