by itsuart · 9y ago · 0 comments
char *foo
instead of char* foo
foo is obviously a pointer to the char, not a char. It has different size and behavior. char* foo, bar;
doesn't mean what one would expect it to mean.[1] as per Rule of Maximum Astonishment.
char* foo = NULL;
char bar = 0;
are preferable. Is there any reason one would want to smash N declarations into one?
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.
*foo
is obviously a char. Do you put a space between dereference operator and its operand? *foo = 'a';
will write value of 'a' into memory at address that is in foo. type* foo;
is declaration and *foo
is dereferencing.