469,309 Members | 2,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

syntax error with pointer to pointer



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

Apr 28 '07 #1
7 1935

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 '07 #2
On 28 huhti, 03:39, klaushuot...@gmail.com 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 '07 #3
kl**********@gmail.com 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

Apr 28 '07 #4
Leo
if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
bucket is a variable, not a type name. bucket* behavior undefined.

Apr 28 '07 #5
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.
Apr 28 '07 #6
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

Apr 28 '07 #7
On Apr 28, 2:42 pm, Leo <fireman...@gmail.comwrote:
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.

Apr 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Donald Canton | last post: by
5 posts views Thread by damian birchler | last post: by
27 posts views Thread by Adam Warner | last post: by
2 posts views Thread by bor_kev | last post: by
16 posts views Thread by danu | last post: by
11 posts views Thread by deppy_3 | last post: by
6 posts views Thread by Pixel.to.life | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.