Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    char* str[10]; /* Better */


I disagree, the following:

  int* foo, bar;
could be interpreted as declaring two pointers, whereas:

  int *foo, bar;
makes it clear that only the first variable is a pointer.


I don't think you should be declaring mixed types with one statement. Declare a pointer, then declare the `int` separately -- and the mystery is gone.


I believe the point is that, with that syntax, you can't declare multiple same-type pointers on the same line


I find that problem goes away if you only declare a single variable per line. Generally, I find people who do

  int* foo;
Started with C++ and people who do

  int *foo;
Started with C. I started with C++.


I have made the same observation and as someone who started with C++, I think it makes more sense that

    int* i;
is an int pointer named i.

    int *i;
just looks strange.. an int named pointer i umm?

Besides, declaring only one variable per line seems less cluttered to me, so I never get the

    int* a, b;
confusion.


I never put the asterisk next to the type, because I wanted to train myself the way the grammar actually works rather than the way almost everyone wished it worked.


But the compiler recognizes the type of the variable as int*.


When used in an expression, I read p as "what's pointed by p". In a declaration, this applies as well:

    int *i;
means "what's pointed by i is an int".


That makes more sense alright, but I still think it would make a lot more sense if * was bound to the type.


Declaring two different types on the same line is fraught with confusion, so it is to be avoided.


I've always found putting the * next to the type instead of the variable, like this, more intuitive. Does anyone know why the other way is used more often? Is it just historic, or is there a more practical reason?


As said elsewhere: "Declaration follows use".

    int *foo;
means "foo is a pointer to int". But that's explained different from how it's written. More clearly, you can say that "foo" is an int. Then, doing

    int *foo;
    foo = 50;
is obviously wrong because "foo" is not an int; however

    int *foo;
    *foo = 50;
is correct.

Similarly for arrays:

    int *foo[50];
means "foo is an array of pointers to int", or "
foo[5] is an int".

From there follows that & is the antithesis of , and they negate each other:

    int *foo; //foo is a pointer to int.
    &*foo; //the address of the contents of foo
    foo; // same as above, but shorter.


Sorry, that didn't make much sense to me - in your first example, you can't say "foo is an int", because it's not, it's a pointer to an int. And in your last example, you can't say "foo[5] is an int", because it's not, it's a pointer to int.

In response to the original question, I think the answer is that

  int *foo;
is a common way of writing it because that's the way the compiler resolves it. As mentioned elsewhere,

  int* foo, bar;
is equivalent to

  int *foo; int bar
so treating the star as part of the type can cause problems.

But I agree fully - it makes a lot more sense to me to consider the pointer star as a flag on the type, not a modifier to the name. Just one of the many warts on C and C++ that make me happy I have to use them so infrequently...

[Edit: formatting, stars were getting swallowed when put inline]


> [Edit: formatting, stars were getting swallowed when put inline]

The same thing happened to the post you're responding to, and that's why it's not making sense to you. The author really meant to say star-foo and star-foo[5], but instead italicized a bunch of text in between.


D'oh.

Don't know how I didn't realize that, given that my response got messed up so that my "corrected" version read exactly like the one I was confused about...

Please disregard everything I said, in that case. :)




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: