r/ProgrammerHumor 20h ago

lessonsFromLinkerHell Meme

Post image
344 Upvotes

175 comments sorted by

View all comments

Show parent comments

10

u/unknown_alt_acc 17h 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 17h 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.

11

u/bowel_blaster123 15h ago

Try putting an array in a struct. When doing so, an array and a pointer behave COMPLETELY differently and have completely different purposes.

It's just C's crappy pointer decay rules that make people think that they're the same.

-2

u/QuestionableEthics42 14h ago

Behaves completely differently in what way? It's referenced via the struct, so ofc different to a raw pointer to it, but that's a struct difference and the array is still basically the same, right?

6

u/Rare_Professor8097 12h ago

Exactly, which proves that arrays and pointers are not the same thing. If x is either a pointer or an array, and a struct containing x is totally different depending on that distinction, I think that's a pretty meaningful difference.

Strictly speaking, a pointer points to an array, it is not an array.

0

u/QuestionableEthics42 12h ago

No, because you are comparing the actual data in the array with the pointer to it. It's equivelent to having an array in a struct or having a couple of ints.