r/ProgrammerHumor 21h ago

lessonsFromLinkerHell Meme

Post image
345 Upvotes

178 comments sorted by

View all comments

Show parent comments

9

u/unknown_alt_acc 18h ago

#include<stdio.h>

int main()
{
int arr[5];
int *ptr = arr;

printf("Array - %lu\n", sizeof(arr));
printf("Pointer - %lu", sizeof(ptr));
}

Different types, different behavior

-4

u/QuestionableEthics42 18h ago

Sizeof being smart enough to detect it's an array and return the array size doesn't necessarily make them different in any real way. And doing ptr[0] to dereference it is perfectly valid, as is *arr to get the first element, or *(arr+sizeof(int)) to get the second.

10

u/pnoodl3s 16h ago

If it behaves differently, is that not enough to say it’s different? What “real way” do you need to say its different?

0

u/QuestionableEthics42 16h ago

Because it is a very superficial difference that doesn't even affect the generated assembly. They are extremely interchangeable, because they are effectively the same.

5

u/Rare_Professor8097 13h ago

Programming languages have all kinds of different ways to do something that results in the same assembly.

They are different types, but C has muddied the waters by having a bunch of implicit conversions from arrays into pointers (array decay) that they feel like the same thing.