syntax error with pointer to pointer

R

Ray Dillinger

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
 
K

klaushuotari

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...
 
K

klaushuotari

Ray Dillinger kirjoitti:











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?
 
R

Ray Dillinger

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
 
L

Leo

if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
bucket is a variable, not a type name. bucket* behavior undefined.
 
R

Richard Heathfield

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.
 
R

Ray Dillinger

Leo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top