syntax error with pointer to pointer

Discussion in 'C Programming' started by Ray Dillinger, Apr 28, 2007.

  1. Hi. I'm having a problem and I really want to understand it.
    Here's the situation: I have an array of pointers, and each
    pointer is the head of a linked list of structs. The structs
    are typedef'd to have the type name 'event.' It's a simple
    open hash table used to implement events in a game.

    So, I have a routine that returns the address of one of the
    pointers in the array, or returns NULL for a key out of
    range (meaning no bucket for that key exists in the table).

    Then I try to write this routine:

    void test (int key)
    {
    event **bucket = getbucket(key);
    if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
    {
    ...do stuff ...
    }
    } /* unmatched '}' at top level?! */

    and bizarre stuff happens. First, the compiler (gcc)
    reports a 'syntax error before the '*' token' on the
    indicated line. Second, it reports an unmatched bracket
    at top level at the next indicated line.

    I'm baffled and I don't understand. My editor (emacs, in
    c mode) reads the brackets the same way I do - indents them
    in a way I agree with and reports that they match what it
    looks to me like they match. The compiler loses the level
    of bracketing somewhere, and since I also am baffled by
    the syntax error of the ** pointer, I suspect they may have
    the same cause.

    But no clue as to what the cause is. Anybody got any insight?

    Bear
     
    Ray Dillinger, Apr 28, 2007
    #1
    1. Advertising

  2. Ray Dillinger

    Guest

    Ray Dillinger kirjoitti:
    > Hi. I'm having a problem and I really want to understand it.
    > Here's the situation: I have an array of pointers, and each
    > pointer is the head of a linked list of structs. The structs
    > are typedef'd to have the type name 'event.' It's a simple
    > open hash table used to implement events in a game.
    >
    > So, I have a routine that returns the address of one of the
    > pointers in the array, or returns NULL for a key out of
    > range (meaning no bucket for that key exists in the table).
    >
    > Then I try to write this routine:
    >
    > void test (int key)
    > {
    > event **bucket = getbucket(key);
    > if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
    > {
    > ...do stuff ...
    > }
    > } /* unmatched '}' at top level?! */
    >
    > and bizarre stuff happens. First, the compiler (gcc)
    > reports a 'syntax error before the '*' token' on the
    > indicated line. Second, it reports an unmatched bracket
    > at top level at the next indicated line.
    >
    > I'm baffled and I don't understand. My editor (emacs, in
    > c mode) reads the brackets the same way I do - indents them
    > in a way I agree with and reports that they match what it
    > looks to me like they match. The compiler loses the level
    > of bracketing somewhere, and since I also am baffled by
    > the syntax error of the ** pointer, I suspect they may have
    > the same cause.
    >
    > But no clue as to what the cause is. Anybody got any insight?
    >
    > Bear


    What about:

    if ((bucket != NULL) && (*bucket != NULL)) /**bucket instead of
    bucket* ?*/

    Otherwise I don't know...
     
    , Apr 28, 2007
    #2
    1. Advertising

  3. Ray Dillinger

    Guest

    On 28 huhti, 03:39, wrote:
    > Ray Dillinger kirjoitti:
    >
    >
    >
    > > Hi. I'm having a problem and I really want to understand it.
    > > Here's the situation: I have an array of pointers, and each
    > > pointer is the head of a linked list of structs. The structs
    > > are typedef'd to have the type name 'event.' It's a simple
    > > open hash table used to implement events in a game.

    >
    > > So, I have a routine that returns the address of one of the
    > > pointers in the array, or returns NULL for a key out of
    > > range (meaning no bucket for that key exists in the table).

    >
    > > Then I try to write this routine:

    >
    > > void test (int key)
    > > {
    > > event **bucket = getbucket(key);
    > > if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
    > > {
    > > ...do stuff ...
    > > }
    > > } /* unmatched '}' at top level?! */

    >
    > > and bizarre stuff happens. First, the compiler (gcc)
    > > reports a 'syntax error before the '*' token' on the
    > > indicated line. Second, it reports an unmatched bracket
    > > at top level at the next indicated line.

    >
    > > I'm baffled and I don't understand. My editor (emacs, in
    > > c mode) reads the brackets the same way I do - indents them
    > > in a way I agree with and reports that they match what it
    > > looks to me like they match. The compiler loses the level
    > > of bracketing somewhere, and since I also am baffled by
    > > the syntax error of the ** pointer, I suspect they may have
    > > the same cause.

    >
    > > But no clue as to what the cause is. Anybody got any insight?

    >
    > > Bear

    >
    > What about:
    >
    > if ((bucket != NULL) && (*bucket != NULL)) /**bucket instead of
    > bucket* ?*/
    >
    > Otherwise I don't know...


    And also:

    event **bucket = getbucket(key);

    depending what getbucket() returns, it seems redundant to check the
    value of bucket so many times. Either getbucket() returns something,
    or then it doesn't. No need to check if ((bucket != NULL) && (bucket* !
    = NULL))

    Actually when I think about it, what do you think you would find in
    *bucket?
     
    , Apr 28, 2007
    #3
  4. wrote:

    >>What about:
    >>
    >>if ((bucket != NULL) && (*bucket != NULL)) /**bucket instead of
    >>bucket* ?*/


    > And also:
    >
    > event **bucket = getbucket(key);
    >
    > depending what getbucket() returns, it seems redundant to check the
    > value of bucket so many times. Either getbucket() returns something,
    > or then it doesn't. No need to check if ((bucket != NULL) && (bucket* !
    > = NULL))
    >
    > Actually when I think about it, what do you think you would find in
    > *bucket?


    If bucket is not NULL, then the key was a valid key for
    a time tick currently indexed in the table and bucket
    is the address of an event pointer somewhere in the array.
    The pointer that bucket /points/ to, on the other hand,
    should point at an event scheduled to happen in that time
    tick (with a 'next' field for more events in the same
    bucket). I get the pointer's address in bucket rather
    than its value, because I will need to change its value
    when "popping" an event out of the time-tick bucket.

    The if statement above should translate as "if the bucket
    exists and is nonempty then..." Because if bucket is NULL
    then the list doesn't exist, and if the pointer bucket points
    at is NULL then the list exists (ie, the time tick is in
    the current range covered by the table) but no events are
    scheduled in it.

    Anyway, you have to check if bucket is NULL first. If it
    is, you really don't want to check the value of *bucket.

    Bear
     
    Ray Dillinger, Apr 28, 2007
    #4
  5. Ray Dillinger

    Leo Guest

    > if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
    bucket is a variable, not a type name. bucket* behavior undefined.
     
    Leo, Apr 28, 2007
    #5
  6. Ray Dillinger said:

    >
    >
    > Hi. I'm having a problem and I really want to understand it.
    > Here's the situation: I have an array of pointers, and each
    > pointer is the head of a linked list of structs. The structs
    > are typedef'd to have the type name 'event.' It's a simple
    > open hash table used to implement events in a game.
    >
    > So, I have a routine that returns the address of one of the
    > pointers in the array, or returns NULL for a key out of
    > range (meaning no bucket for that key exists in the table).
    >
    > Then I try to write this routine:
    >
    > void test (int key)
    > {
    > event **bucket = getbucket(key);
    > if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/


    *bucket, not bucket*

    If all involved pointers are valid and bucket is an event **, then
    *bucket is an event *, and **bucket is an event.

    > {
    > ...do stuff ...
    > }
    > } /* unmatched '}' at top level?! */
    >
    > and bizarre stuff happens. First, the compiler (gcc)
    > reports a 'syntax error before the '*' token' on the
    > indicated line. Second, it reports an unmatched bracket
    > at top level at the next indicated line.


    Fix the first syntax error, and you'll often find that the second
    vanishes. Syntax errors confuse compilers.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Apr 28, 2007
    #6
  7. Leo wrote:
    >> if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/

    >
    > bucket is a variable, not a type name. bucket* behavior undefined.
    >


    Argh. Yep, that was it, and man it makes me feel stupid. I get
    these mixed up.

    And every time I start writing C code again after a hiatus of months
    or a prototype run using other languages, I trip over it.

    Thanks for your responses....

    Bear
     
    Ray Dillinger, Apr 28, 2007
    #7
  8. Ray Dillinger

    Old Wolf Guest

    On Apr 28, 2:42 pm, Leo <> wrote:
    > > if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/

    >
    > bucket is a variable, not a type name. bucket* behavior undefined.


    Actually it is a syntax error; the compiler must issue a diagnostic.
     
    Old Wolf, Apr 30, 2007
    #8
    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. Balaji
    Replies:
    3
    Views:
    10,195
  2. gabriele renzi
    Replies:
    2
    Views:
    238
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    242
  4. Good Night Moon
    Replies:
    9
    Views:
    315
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    346
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page