4 ms·
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 si
by itsuart 10y 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.
- gpderetta 10y agoThat's because [1] char* foo, bar; doesn't mean what one would expect it to mean. [1] as per Rule of Maximum Astonishment.
- itsuart 10y agoI find that char* foo = NULL; char bar = 0; are preferable. Is there any reason one would want to smash N declarations into one?
- gpderetta 10y agoI 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.
- wruza 10y agoOtoh, *foo is obviously a char. Do you put a space between dereference operator and its operand?
- itsuart 10y agoNo, 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.
- cessor 10y agoI 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.