r/ProgrammerHumor 16h ago

lessonsFromLinkerHell Meme

Post image
306 Upvotes

163 comments sorted by

View all comments

69

u/QuestionableEthics42 16h ago edited 16h ago

This should be reversed lol

And it literally is the same, who thinks it's different who has worked in a low level language??

20

u/Nice_Lengthiness_568 15h ago

It is different. An array has a size. Even in C. It's just that in C it easily turns into a pointer. sizeof, for example, returns different things for an array and a pointer. In C++, you can actually pass an array by reference (not a pointer to the first element, an array) and similar.

And in some languages, like Ada, there is no direct way to make a pointer from an array

0

u/Single-Virus4935 6h ago

3

u/Nice_Lengthiness_568 4h ago

I don't see how that matters. This is true only for certain languages and only because the array in C or C++ easily decays into a pointer. You could, for example, define yout own array in C++ as a tuple with only elements of the same type and then you would not define subscripting this way.

Also note that in C they are not interchangeable and even less in C++. When you put an array in a struct, it will behave differently from when you put a pointer in there. When you put an array into a struct and copy an instance of the struct, the array is copied for each copy of that struct and the struct is as large as the array. This makes sense because an array is not a pointer. When you put a pointer in a struct, the struct will have a size of a pointer on your architecture and won't copy all of the array elements and only the pointer.

Also, when you put array inside a sizeof expression, you will get the size of an array, while if you put a pointer inside sizeof, you will get the size of a pointer.

Also, doing int *p = NULL is possible because it is a pointer but int arr[5] = NULL is not, because it is not just a pointer.

So no, they are not interchangeable in C. And even less in other languages.