DEV Community

Discussion on: #001 DS&A - Operators and Arrays

Collapse
 
pentacular profile image
pentacular

address of A[i] = i * sizeof(element) + base_address

An address is a pointer value, so when adding to it, the size is already factored in.

Which means that &A[i] is equivalent to A + i.

The only way that your formula works if it you're working with char *, in which case you'll get the wrong kind of pointer back at the end.

Beyond that, C doesn't have a flat memory model, so you can't just pretend that pointers are integers.

Two Dimensional Arrays

C does not support two dimensional arrays -- but it does support arrays of arrays.

So there's no need to transform it to a one dimensional array -- it already is one dimensional.

let's take this example of an 2D array with 3 rows and 4 columns

// An array of 3 int[4]s.
int array[3][4];

Indexing an array of arrays is simple pointer arithmetic.

Just as array[i] is (array + i), so is array[i][j] is *((array + i) + j).

It's a bit unclear to me why you're going out of your way to make this slower and more complicated. :)

Collapse
 
omarkhatib profile image
Omar • Edited

Hi thanks for your comment ,
When you put A+i the compiler will convert it to A + (i*sizeof(element)) ,
for multi dimensional arrays yes they exist in C you can look at page 99 , section 5.7 of the original Ainsi C written by the Author of C.
But they are not the only way to do it, if you open in page 101 there is a section for Multi arrays vs pointers.
Tomorrow I will write about pointers :)
I am assuming that we don't know what is a pointer yet.

Collapse
 
pentacular profile image
pentacular

What he refers to as a multidimensional array is a one dimensional array of arrays.

Provide an example of something that you think is not a one dimensional array in C, and I will happily demonstrate to you why it is a one dimensional array. :)

If you don't know what a pointer is, you cannot meaningfully talk about arrays (since pointers are how you index arrays) or addresses (which are pointer values).

Thread Thread
 
omarkhatib profile image
Omar

Can you give me an academic resource to read it?
Also check this one lysator.liu.se/c/c-faq/c-2.html

Thread Thread
 
pentacular profile image
pentacular

Sure: open-std.org/jtc1/sc22/wg14/www/do... 6.5.2.1 Array subscripting

Lysator is generally confused about arrays -- consider the mess it makes of 2.14 which is a pointer into an array of int *, rather than being multidimensional.

The simplest proof is this.

int array[3][4];
int (*p)[4] = &array[0];
sizeof array[0] == sizeof (int[4]);

Therefore array is an array of length 3 with an element type of int[4].

Note the single dimension used to access it here.

Of course, having accessed array[i], you can now access that one dimensional array via array[i][j], and so on.

Probably the easiest way to think about it is that C provides arrays of arrays, which is one way to implement multi-dimensional arrays, without any of the arrays actually being multi-dimensional.

Thread Thread
 
omarkhatib profile image
Omar

it's all semantics there is no such thing as truly multidimensional arrays as all arrays are one dimensional
you can use a 2 or n dimensional accessor but that's about it.
there is no need to layer a simple thing to look complex.

Thread Thread
 
pentacular profile image
pentacular

There are such things are truly multi-dimensional arrays, just not in C.

For example, in pascal

type
  StatusType = (X, O, Blank);
  BoardType = array[1..3,1..3] of StatusType;
var
  Board : BoardType;

for count1 := 1 to 3 do
  for count2 := 1 to 3 do
    Board[count1, count2] := Blank;

Or in Common Lisp

(let ((a (make-array '(4 3))))
  (aref a i j))

This is not a criticism of C -- deciding to use arrays of arrays rather multi-dimensional arrays is a perfectly reasonable design choice.

It's just a design choice that should be understood clearly. :)