Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > syntax error with pointer to pointer

Reply
Thread Tools

syntax error with pointer to pointer

 
 
Ray Dillinger
Guest
Posts: n/a
 
      04-28-2007


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





 
Reply With Quote
 
 
 
 
klaushuotari@gmail.com
Guest
Posts: n/a
 
      04-28-2007

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

 
Reply With Quote
 
 
 
 
klaushuotari@gmail.com
Guest
Posts: n/a
 
      04-28-2007
On 28 huhti, 03:39, (E-Mail Removed) 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?

 
Reply With Quote
 
Ray Dillinger
Guest
Posts: n/a
 
      04-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Leo
Guest
Posts: n/a
 
      04-28-2007
> if ((bucket != NULL) && (bucket* != NULL)) /*syntax error?!*/
bucket is a variable, not a type name. bucket* behavior undefined.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-28-2007
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.
 
Reply With Quote
 
Ray Dillinger
Guest
Posts: n/a
 
      04-28-2007
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

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-29-2007
On Apr 28, 2:42 pm, Leo <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Complier error!! error C2059: syntax error : '(' Balaji C++ 3 12-03-2004 11:19 PM



Advertisments