3 ms·
This was probably the most confusing thing about C when I first started learning programming back in the day. When you call a function you pass the value, excep
by franciscop 6y ago
This was probably the most confusing thing about C when I first started learning programming back in the day. When you call a function you pass the value, except in arrays where it gets converted as a pointer. It was explained back then to me that the reason is because copying the whole array was not efficient so it was better to pass the reference.
- quelsolaar 6y agoI think a better way to think about is to say that when you type: int a[10]; you allocate 10 integers and "a" is the pointer to the first one of them. Arrays are just memory, just like what you get wen calling malloc, and memory is accessed using pointers in C.
- rightbyte 6y agoThat mindset doesn't cover sizeof(a) properly.
- quelsolaar 6y agoI agree, that's an inconsistency I would rather fix, then the one OP suggests.
- Cyph0n 6y agoMinor nitpick: there is no allocation going on here - you’re simply reserving a fixed-size buffer on the stack (assuming the array is local to a function).
- quelsolaar 6y agoI would call that a stack allocation, but yes they are slightly different. In my mind its a feature that arrays allocated on the stack and heap can interchangeably be given as an argument to a function.
- jstimpfle 6y agoTo allocate means to get or reserve something (for lack of an even better word, as a non-native). This is a perfectly valid use of allocation. What you mean is maybe more aptly named "dynamic (memory) allocation".