Sometimes, beeing a GNU compiler manufacturer is difficult! People blame you for stuff that actually is implemented correctly. A popular mistake involves multiple pointer declarations!


char a;
char b;
char c;

The statement above can be merged into a single instruction easily:

char a, b, c;

Now take a look at this:

char* a;
char* b;
char* c;

Simplifying this with common sense leads to:

char* a, b, c;

But be careful-this does not do what you want it to. The star operator ties to the variable name and not to the type. So, you would end up with a char pointer a and two char variables b and c.

The correct way of simplification looks like this:

char *a, *b, *c;

This mistake happens often. It gets even more complicated when macros/typedefs are involved. Thus, if you ever see multiple pointers defined in a single instruction-be careful!
Did you ever have such a problem?

Related posts:

  1. Globals in DAs
  2. On foo, bar and baz
  3. The many faces of ‘static’
  4. Fix padding issues in structs
  5. PODS and variables or standards vs. comfort

4 Responses to “On defining multiple pointers in one line”

  1. Havent done enough programming to make that mistake, but it looks like something I might do in CS class.

  2. This is the normal behaviour of C, not GCC bug! It’s different from C#…

  3. This isn’t really a big issue in my opinion, most compilers will most like catch the message when you try to either assign a pointer to or try to dereference the char.

  4. Hi,
    thats just what I said-it is C behaviour like it should be.

    Just wanted to highlight this to show the trap to people who may fall into it(like me)!

    Best regards
    Tam Hanna

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

© 2012 TamsPalm - the Palm OS / web OS Blog Suffusion theme by Sayontan Sinha