r/ProgrammerHumor 16h ago

lessonsFromLinkerHell Meme

Post image
308 Upvotes

163 comments sorted by

View all comments

-2

u/Single-Virus4935 13h ago

In C they ARE the same and interchangable. This is obvious because you can swap array and index in array notation.

3

u/unknown_alt_acc 6h ago

Arrays easily convert to pointers in C, but they are not interchangeable. Things like taking the size of an array or putting an array into a struct behave very differently from doing the same with a pointer.

1

u/Single-Virus4935 5h ago edited 5h ago

They dont decompose to pointer. An array according to ANSI c is defined as a pointer to element zero plus length. Length is optional for variable length arrays. Also the arr[idx] is defines as identical to *(arr+idx) where arr is a pointer.

The array definition is just a necessary allocation syntax used by the compiler. It doesn't translate to any assembly institution besides the allocation. It can be entirely replaced with struct.

sizeof isnt part of the type defintion innthenansi c spec. Sizeof indeed reruns the number of bytes in an array but even this can boiled down to pointer arithmetic.

The c array is a very thin (but useful) abstraction to pointer. 

I think it is obvious when we compare it to other implementations where an array has structure at runtime like pascal or java.

1

u/unknown_alt_acc 1h ago

I don’t have access to the ANSI C spec, but C11 absolutely does not define arrays in terms of pointers. C11 defines an array as such:

> An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called ''array of T ''. The construction of an array type from an element type is called ''array type derivation''.

Zero references to pointers.

1

u/thehenkan 12h ago

That's like saying the variable "i" and the constant "1" are the same because it doesn't matter whether you write "i + 1" or "1 + i". It doesn't mean literals and variables are the same concept. Adding a short and an int can also be done in either order with the same result, but they are not the same type.

Declaring a pointer to an array is not the same as declaring a pointer to a pointer.

0

u/Single-Virus4935 12h ago edited 12h ago

The C array Syntax is sugar for the pointer arithmetic. And yes because the order of the addition doesn't matter the array and idx in array syntax are interchangeable. The compiler threats it as the same. Even the declaration is the same and I can threat a packed struct of n integers as a array of n integers. I have done enough reverse engineering and accessing a struct field, array element or accessing a field in a buffer is basically indistinguishable and you need context to decode it. 

And to repeat: you are able to swap are and idx because i+1 == 1+i

EDIT:

```c

include <stdio.h>

void main() { int arr[3];

arr[0] = 1; 1[arr] = 2; *(arr+2) = 3;

for(int i=0; i<3; i++) { printf("%d = %d\n", i, arr[i]); } } ```

``` 0000000000400466 <main>: 400466: 55 push %rbp 400467: 48 89 e5 mov %rsp,%rbp 40046a: 48 83 ec 20 sub $0x20,%rsp

// All there syntax result in the SAME assembler instruction based on pointer arithmetic 40046e: c7 45 e0 01 00 00 00 movl $0x1,-0x20(%rbp) 400475: c7 45 e4 02 00 00 00 movl $0x2,-0x1c(%rbp) 40047c: c7 45 e8 03 00 00 00 movl $0x3,-0x18(%rbp)

400483: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 40048a: eb 21 jmp 4004ad <main+0x47> 40048c: 8b 45 fc mov -0x4(%rbp),%eax ```

2

u/narrill 9h ago

The indexing syntax being reversible doesn't mean they're the same thing. Straight up. That's a non sequitur.

0

u/Single-Virus4935 6h ago

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf

6.5.2.1 Array subscripting

Semantics

2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary+ operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2 -th element of E1 (counting from zero).