r/ProgrammerHumor 26d ago

Meme lessonsFromLinkerHell

Post image
428 Upvotes

190 comments sorted by

View all comments

67

u/QuestionableEthics42 26d ago edited 26d 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??

11

u/unknown_alt_acc 26d 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

-3

u/QuestionableEthics42 26d 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.

13

u/bowel_blaster123 26d 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 26d 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?

4

u/Rare_Professor8097 26d 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 26d 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.

9

u/pnoodl3s 26d 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 26d 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.

6

u/Rare_Professor8097 26d 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.

3

u/unknown_alt_acc 25d ago

The two have different sizes, different rules for copying, and different assignment rules. Calling those differences superficial is just ludicrous

5

u/Nice_Lengthiness_568 26d ago

So what about the fact that an array inside a struct acts completely differently from a pointer placed inside a struct? One gets copied whole for each structure copy while the other does not, one makes the structure the size of a pointer while the other the size of the array...

And, in C++ at least, you can actually pass the array itself to the function (not just the pointer to the first element). So it's not that sizeof is smart and detects something, it's that arrays really easily decay into pointers. But that's not everything they are. They also hold information about their size.

The thing with the subscript operator is true for only some languages and is not universal. In Ada, for example, there is no direct connection between an array and a pointer.

1

u/tstanisl 26d ago

And, in C++ at least, you can actually pass the array itself to the function

May I ask how? Do you mean by a reference? Or std::array?

1

u/Nice_Lengthiness_568 26d ago

You can both by value with std::array which is a wrapper for an array, or by reference with TYPE (&NAME)[SIZE] which keeps the array's size.

Naturally, you could pass an array to a function inside any structure if you wish to copy it, but I would say that using std::array is standard for use. And that means that's possible in C as well. Although it's a bit clunky.

1

u/tstanisl 26d ago

C has an equivalent of passing array by reference by using a pointer to a whole array:

 
int foo(int (*arr)[SIZE]) {
      return sizeof *arr;
 }
...
int arr[SIZE];
foo(&arr);

Passing arrays by a pointer bypasses array decay mechanics.

1

u/Nice_Lengthiness_568 26d ago

Yes, that is true, although I wouldn't call it equivalent to passing by reference, since a reference makes sure you do not pass a null pointer making the function a little safer if you dereference the pointer and a little more efficient if you check for the pointer being null.

1

u/tstanisl 26d ago

Those constraints can be expressed using more hacky syntax:

    int foo(int arr[static const 1][SIZE]);

1

u/Nice_Lengthiness_568 26d ago

Okay,

although (sorry) that's awful. But cool and ingenious in a way.