Hacker News new | ask | show | jobs
by itsuart 3467 days ago
It was always puzzling to me why people stick to

  char *foo 
instead of

  char* foo
foo is obviously a pointer to the char, not a char. It has different size and behavior.
3 comments

That's because [1]

  char* foo, bar;
doesn't mean what one would expect it to mean.

[1] as per Rule of Maximum Astonishment.

I find that

char* foo = NULL;

char bar = 0;

are preferable. Is there any reason one would want to smash N declarations into one?

I prefer that as well, but because the other option is possible and sometimes used, a few prefer to put the star close to the name as to avoid mistakes.
I agree.

Simply put, it would sense to provide

    #define CharPointer char*
and then declare

    char c = 'a';
    CharCointer p = &c;
To me this became clear when I did some C# interop, where IntPtr is a common class, such as:

    [DllImport("user32.dll")]
    static extern IntPtr CallWindowProc(WndProcDelegate lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
It's just an object oriented pointer interface. It looks different. I agree:

> It has different size and behavior.

Otoh,

  *foo
is obviously a char. Do you put a space between dereference operator and its operand?
No, I don't. If foo is a (storage of) pointer to something, to get that something from memory I use dereference operator `* so

  *foo = 'a';
will write value of 'a' into memory at address that is in foo.

  type* foo;
is declaration and

  *foo
is dereferencing.