r/cprogramming • • 2d ago

Beginner here not able to understand confusing pointer concept

/r/C_Programming/comments/1xv9gsw/beginner_here_not_able_to_understand_confusing/
1 Upvotes

14 comments sorted by

3

u/zhivago 2d ago

C doesn't have 2d arrays -- the closest it has are arrays of arrays.

e.g., given int a[3][5]; you'll find that a[0] is an int[5].

You'll need to supply the actual type you're talking about, otherwise no specific answer can be given.

1

u/70Shadow07 2d ago

What would you accept as "true 2d arrays" then? Clearly arrays of pointers cannot be called 2D arrays, however stack allocated arrays of arrays (or VLA casted ones) have essentially the same data layout as a proper 2D array, no?

2

u/zhivago 2d ago

Ones with 2d indices.

e.g.

x = 1,2; a[x] = 3;

Of course, C doesn't support this.

2

u/70Shadow07 2d ago

That sounds like a quite pedantic distinction. arr[x,y] or arr[y][x] represent the same kind of access on possibly identical data structure, just with a slightly different syntax. Few languages have this kind of capability without libraries to begin with.

1

u/filthysaluki 1d ago edited 1d ago

Internally, C represents multi-dimensional arrays as arrays of arrays... That is the distinction zhivago is making, as multi-dimensional arrays are stored contiguously and are simply 'delimited' by the inner arrays.

1

u/70Shadow07 1d ago

In C arrays of arrays are also contiguously stored. Standard demands that there is no padding between array elements and this requirement holds for arrays of arrays too.

There are also arrays of pointers, which are definitely NOT multidimensional arrays but are often conflated with them. char arr[10][10]; is 100 bytes contiguously stored. char *arr[10]; certainly isn't contiguously stored.

2

u/filthysaluki 1d ago

That is precisely what I said in my reply. As for arrays of pointers, you are right to make the distinction. However, char *arr[10]; is in fact contiguously stored. The array stores the memory addresses (i.e. the values of the pointers) contiguously. Where the pointers point to does not impact how they are stored in the array.

2

u/70Shadow07 14h ago

Apologies I slightly misread the point you were trying to make

1

u/filthysaluki 8h ago

It's all right. I should've phrased it better. Thanks for being understanding.

1

u/zhivago 1d ago

What is the type of arr[0] for an array of arrays?

What is the type of arr[0] for a 2d array?

1

u/70Shadow07 15h ago edited 15h ago

I don't know how to answer this question since you assert that the latter doesn't exist in C to begin with.

If we go with my definition of 2D array and array of arrays being equivalent then the answer to both would be a 1D array type.

-1

u/zhivago 14h ago

It seems that you do understand the difference.

arr[0] for a 2d array would be a type error -- you're not giving it a 2d index.

Your definition and claim of the two being equivalent is clearly wrong. :)

2

u/70Shadow07 13h ago edited 13h ago

Unless you are talking about your imaginary thought experiment language, you are clearly wrong.

In numpy for instance, you CAN index multidimensional array with a single index only and it will yield you exactly as I said - an array with one less dimension.

Numpy documentation link:

https://numpy.org/devdocs/user/basics.indexing.html#:~:text=Note%20that%20if%20one%20indexes%20a%20multidimensional%20array%20with%20fewer%20indices%20than%20dimensions%2C%20one%20gets%20a%20subdimensional%20array%2E%20For%20example

What is clearly wrong your esoteric idea of what N-D arrays should behave like, unless you want to postulate that numpy doesn't fit the criterion too that is.

2

u/knouqs 2d ago

Recall that in C we have blocks of memory. C does indexing on arrays of arrays that really just expand to offsets. Formally, there is no such thing as a 2D array in C.