Using pointers to get value from multidimensional array - C - c

I am trying to get value from "second row" in multidimensional array. But I have some problems with that.
I thought that numbers are stored sequentialy in memory so tab[2][2] is stored same as tab[4]. But it seems that I was wrong.
This is what I tried:
int b[2][2] = {{111,222},{333,444}};
int i = 0;
for(;i < 100; i++)
printf("%d \n", **(b+i));
The problem is that I get only 111 and 333 as the result. There is no 222 or 444 in other 98 results. Why?

The problem is that **(b+i) doesn't do what you think it does. It evaluates to:
b[i][0]
As Matt McNabb noted,
**(b+i)
is equivalent to:
*(*(b+i)+0)
and since *(b+i) is equivalent to b[i], the expression as a whole can be seen as:
*(b[i]+0)
and hence:
b[i][0]
Since your array has only 2 rows, only the values of i for 0 and 1 are in bounds of the array (that's the 111 and 333). The rest was wildly out of bounds.
What you could do is:
#include <stdio.h>
int main(void)
{
int b[2][2] = { { 111, 222 }, { 333, 444 } };
int *base = &b[0][0];
for (int i = 0; i < 4; i++)
printf("%d: %d\n", i, base[i]);
return 0;
}
Output:
0: 111
1: 222
2: 333
3: 444

You can think of a two-dimensional array as a special form of a one-dimensional array. Your array holds 2 elements (!). (Each element happens to be an array of two elements, but let's ignore that for a sec.) Let's make the dimensions different so that we can distinguish them:
int arr2d[2][3] holds 2 elements (each being an array of 3 elements). The "primary index" is written first, that is if you have a one-dimensional array of 3 like int arr1d[3]and want to have an array of those with three elements like arr2d, you have to write arr2d[2][3]. You could arrange that with a typedef which makes it clear that all arrays in C are essentially 1-dimensional:
typedef int arr3IntT[3];
arr3IntT arr2d[2] = { {0,1,2}, {3,4,5} };
Now what does arr2d+i mean? arr2d, like any array, decays to a pointer to its first element (which is an array of 3 ints). arr2d+1 adds the offset of 1 of those elements to the address, so that like always the expression yields the address of the second element (which is the second array of 3 ints). Dereferencing it once like *(arr2d+1) yields that element, i.e. the one-dimensional sub-array of 3 ints. It decays to a pointer to its first element, i.e. a pointer to the first int in that second sub-array. Dereferencing that in the expression **(arr2d+1) yields that int, like always. To summarize: In your original code you iterate from sub-array to sub-array, always referencing the first of their elements, incidentally going out of bounds for i>1.
But principally you are right, elements of n-dimensional arrays in C are lying contiguously in memory, so you can access them one by one if you like to. You just have to index a pointer to int, not one to int[3]. Here is how: The expression arr2d decays to a pointer to its first element which is an array of 3 ints. Dereferencing that gives that first element, a one-dimensional array of 3 ints. Like all arrays, it decays to a pointer to its first element, an int, which is the very first element in the data:
#include<stdio.h>
int main()
{
int arr2d[2][3] = { {0,1,2}, {3,4,5} };
int *p_el1 = *arr2d;
int i, j;
// Sanity check by indexing 2-dimensionally
for(i=0; i<2; i++) for(j=0; j<3; j++) printf("%d\n", arr2d[i][j]);
// iterate the elements 1 by 1
for(i=0; i<3*2; i++) printf("%d\n", p_el1[i]);
}

A multidimensional array is not a fundamentally new type. It's an array type where the elements are themselves arrays. To quote the C99 standard §6.2.5 ¶20 (Types)
An array type describes a contiguously allocated nonempty set of
objects with a particular member object type, called the element type.
Array types are characterized by their element type and by the number
of elements in the array.
int b[2][2] = {{111, 222}, {333, 444}};
The above statement defines b to be an array of 2 elements where each element is the type int[2] - an array of 2 integers. It also initializes the array with the array initializer list. An array is implicitly converted to a pointer to its first element in some cases.
In the printf call, b decays to a pointer to its first element. Therefore, it's equivalent to &b[0] and has type int (*)[2] - a pointer to an array of 2 integers. Please note that it's undefined behaviour to access elements out of the bound of an array. Therefore, the for loop condition i < 100 is wrong. It should be i < 2. Now, let's try to demystify the expression **(b+i).
b -------------> pointer to a[0]
b + i ---------> pointer to a[i]
*(b + i) -----> a[i]
*(*(b + i)) ---> *(a[i]) ----> *(&(a[i])[0]) ----> a[i][0]
As noted, the elements of the array are themselves arrays. Therefore, a[i] is an array. Again, the array decays to a pointer to its first element, i.e., to &(a[i])[0]. Applying indirection operator * on this pointer gets us the value at that address which is a[i][0].
You can access the elements of the array through a pointer but the pointer type must be a pointer to element type of the array.
#include <stdio.h>
int main(void) {
int b[2][2] = {{111, 222}, {333, 444}};
int (*p)[2] = b;
// (sizeof b / sizeof b[0]) evaluates to
// the size of the array b
for(int i = 0; i < (sizeof b / sizeof b[0]); i++)
// (sizeof *p / sizeof (*p)[0]) evaluates to
// the size of element of the array which is
// itself an array.
for(int j = 0; j < (sizeof *p / sizeof (*p)[0]); j++)
printf("%d\n", *(*(p + i) + j));
return 0;
}
Here, the expression *(*(p + i) + j) can be decoded as
p ---------------> pointer to the first element of b, i.e., &b[0]
(p + i) ----------> pointer to b[i], i.e., &b[i]
*(p + i) ---------> the array element b[i] ---> decays to &(b[i])[0]
*(p + i) + j -----> &(b[i])[j]
*(*(p + i) + j) --> the element b[i][j]
Therefore, the expression *(*(p + i) + j) is equivalent to b[i][j]. In fact, the C99 standard §6.5.2.1 ¶2 says -
The definition of the subscript operator [] is that E1[E2] is
identical to (*((E1)+(E2)))
This means we have the following are equivalent with context to the above program -
*(*(p + i) + j)
// equivalent to
p[i][j]
// equivalent to
b[i][j]
// equivalent to
*(*(b + i) + j)

Related

This program sets every row's 3rd element's value to 20. Why doesn't it work?

Here's the code that I am trying to work with.
int main(void) {
int array[2][5];
for (int ** p = array; p < array + 2; p++) {
*((*p) + 2) = 20;
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 5; col++) {
printf("%d ", array[row][col]);
}
printf("\n");
}
return EXIT_SUCCESS;
}
And this is the output given out by the compiler.
main.c:11:21: warning: initialization of 'int **' from incompatible pointer type 'int (*)[5]' [-Wincompatible-pointer-types]
11 | for (int ** p = array; p < array + 2; p++) {
| ^~~~~
main.c:11:30: warning: comparison of distinct pointer types lacks a cast
11 | for (int ** p = array; p < array + 2; p++) {
| ^
main.exe
make: *** [Makefile:7: main] Error -1073741819
I clearly don't understand type casting when it comes to multidimensional arrays. It would really help if you could explain why this doesn't work.
Omitting the first for loop and doing things manually like this do seem to give out the intended result.
*((*array) + 2) = 20;
*(*(array + 1) + 2) = 20;
I can't get the hang of "int (*)[5]".
The type of array is int [2][5], an array of two arrays of 5 int.
When an array is used in an expression other than as the operand of sizeof, the operand of unary &, or as a string literal used to initialize an array, it is automatically converted to a pointer to its first element.
Since the type of array is int [2][5], the type of its first element is int [5], an array of 5 int. So the type of a pointer to its first element is int (*)[5], a pointer to an array of 5 int.
There is no rule that a pointer to an array is converted to a pointer to a pointer. So no further automatic conversion happens. The result of using array in an expression where it is automatically converted is an int (*)[5].
Therefore, p should be declared to be int (*)[5]:
for (int (*p)[5] = array; p < array + 2; p++)
A multi-dimensional array in C can be seen as an array of arrays. Each of the two top-level elements in array[2][5] is a one-dimensional array of 5 ints. Like any old array, array "decays" in most expressions to a pointer to its first element, that is, a pointer to an int[5]. A declaration for such a pointer would be int (*firstRow)[5] = array; (initializing it with the address to the first top-level element of array). The parentheses are necessary because the indexing operator [] has higher priority than the dereferencing operator *: int *arrayOf5Pointers[5]; would index first, and the result would be a pointer, so that it declares a real array (we can index it) whose elements in turn are pointers (we can dereference the result of the indexing). With (*firstRow)[5]; we indicate that we dereference first (the variable is a pointer) and only then index the result of the dereferencing (which must be an array).
The notation for the type of (*firstRow)[5]; (what you would use in a type cast) is, as always, the notation for a declaration without the identifier, hence int (*)[5]. That looks funny and perhaps ambiguous; but Kernighan/Richie said in their C book that the C grammar guarantees that there is only one valid position for an identifier in such a type notation. I believe them.
Your assignment int ** p = array is wrong: You cannot assign a (pointer to an array of 5 ints) to (a pointer to pointer to int). That is true even though you can legitimately access the first element of the two-dimensional array with e.g. int i = **array;: array decays to a pointer to its first element, that is, to a pointer to an array of 5 ints; the first dereferencing results in the actual array of 5 ints; which in turn decays to a pointer to its first int element; which is dereferenced with the second *.
The general takeaway here is: Pointers to arrays are rare and typically unnecessary. Simply index your 2-dimensional array in the normal fashion:
for(int row = 0; row < 2; row++) { array[row][2] = 20; }
or, better,
for(int row = 0; row < sizeof array/sizeof *array; row++) { array[row][2] = 20; }
The latter determines the number of top-level elements in the array by dividing its overall size in bytes by the size of its first element. The loop stays correct even if you change the size of array.

Why does a 2D array behave like a 1D array of pointers instead of a 1D array of integers?

I have been researching and testing my knowledge in C (I am a new computer engineering student), and ran into a problem I cannot figure out.
When trying to pass a 2D array to a function, I learned that you cannot do so with dynamically allocated arrays, since the compiler needs to know array[][columns]. However, I learned that a 2D array is stored a 1D array, where the elements of each new row just follows the elements of the previous row. When I pass an array name to a function as a pointer to an array, this seems to be the case, and my code works fine. However, in the function where the 2D array is declared, it behaves as an array of pointers instead.
#include <stdio.h>
void printArray(int *A, int* dimA) {
for(int i = 0; i < dimA[0]; ++i) {
for(int j = 0; j < dimA[1]; ++j) {
printf("%3d", A[i*dimA[1] + j]);//This would work if the elements of A[] are the rows of a 2D array mapped into a 1D array
}
printf("\n\n");
}
return;
}
int main(){
int A[2][2] = {{1,2},{3,4}};
int dimA[2] = {2,2};//dimensions of the array
int i, j;
for(i = 0; i < dimA[0]; ++i) {
for(j = 0; j < dimA[1]; ++j) {
printf("%3d", *(A[i] + j)); //This would work if the elements of A[] are pointers
}
printf("\n\n");
}
for(i = 0; i < dimA[0]; ++i) { //Same code as printArray function
for(j = 0; j < dimA[1]; ++j) {
printf("%3d", A[i*dimA[1] + j]);//This would work if the elements of A[] are the rows of a 2D array mapped into a 1D array
}
printf("\n\n");
}
printArray(A, dimA);
return 0;
}
The following code outputs the array correctly in main() when the array is treated as an array of pointers, but not when treated as a 1D array of integers. However, when I pass the same array to the printArray function as a pointer, I can treat it as a 1D array of integers and it works. Any help would be appreciated (I already understand that I can instead use an array of pointers, but I really want to understand what the problem was). Thanks!
According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array
object has register storage class, the behavior is undefined.
Thus in the first for loop
for(i = 0; i < dimA[0]; ++i) {
for(j = 0; j < dimA[1]; ++j) {
printf("%3d", *(A[i] + j)); //This would work if the elements of A[] are pointers
}
printf("\n\n");
}
the expression A[i] has the type int[2]. Being converted to pointer it has the type int *. So for each i the expression A[i] points to the first element of each "row" of the array A.
The expression A[i] + j points to the j-th element of each row. So dereferencing the pointer you get j-th element of the i-th row of the array.
In the second loop
for(i = 0; i < dimA[0]; ++i) { //Same code as printArray function
for(j = 0; j < dimA[1]; ++j) {
printf("%3d", A[i*dimA[1] + j]);//This would work if the elements of A[] are the rows of a 2D array mapped into a 1D array
}
printf("\n\n");
}
the expression A[i*dimA[1] + j] has the type int * and points to i *dimA[1] + j "row" of the array that is it points beyond the array. So the loop does not make sense.
The function declared like
void printArray(int *A, int* dimA);
is called like
printArray(A, dimA);
The second argument that has the type int[2] is indeed converted to pointer of the type int * that points to the first element of the array.
As for the first argument then it is also converted to pointer to its first element. And what is the element of the array? The element of this two-dimensional array is a one-dimensional array of the type int[2]. So pointer to an object of this type will have type int ( * )[2]
Pointers int * and int ( * )[2] are not compatible and by this reason the compiler shall issue a diagnostic message.
the correct declaration of the function should look like
void printArray(int ( *A )[2], int *dimA);
When trying to pass a 2D array to a function, I learned that you cannot do so with dynamically allocated arrays, since the compiler needs to know array[][columns].
This is true, in the sense that you cannot pass any array to a function. You cannot even express such a concept in C, though you can write code that looks like that to the casual eye. In almost every context where an expression evaluating to an array appears -- including function call expressions -- the array value is replaced by a pointer to the first array element.
It is partially true in the sense that a 2D array is an array of arrays, and the dimension of the (array) element type is is part of the overall array's type, part of the type of every element, and part of the type of a pointer to the first element. As such, that dimension must be part of the type of any function parameter to which you want to pass (a pointer to the first element of) the array.
It is most accurately characterized as false, however, even for 2D arrays both of whose dimensions are determined at run time. Since 1999, C has supported variable-length arrays (though in C11 it was made optional), and these play very nicely indeed with dynamically-allocated multi-dimensional arrays and with pointers to arrays of varying dimension:
// Dynamically allocating a 2D array of runtime-determined dimensions:
unsigned rows = calculate_number_of_rows();
unsigned columns = calculate_number_of_columns();
int (*matrix)[columns] = malloc(rows * sizeof(*matrix));
They work well for functions accepting such pointers, too:
void do_something(unsigned rows, unsigned columns, int matrix[rows][columns]);
... or, equivalently ...
void do_something(unsigned rows, unsigned columns, int matrix[][columns]);
... or ...
void do_something(unsigned rows, unsigned columns, int (*matrix)[columns]);
Those three forms are completely equivalent.
However, I learned that a 2D array is stored a 1D array, where the elements of each new row just follows the elements of the previous row.
A 2D array is an array of 1D arrays. The elements of any array are stored contiguously in memory without padding, so the layout of a 2D array of dimensions (r, c) cannot be distinguished from the layout of a 1D array of dimension r * c, but I recommend against thinking of it in the terms
you used.
When I pass an array name to a function as a pointer to an array, this seems to be the case, and my code works fine.
Do not do that. In practice, it is very likely to work exactly as you say, but you should heed the warnings emitted by your compiler -- and it definitely should be emitting warnings about that.
However, in the function where the 2D array is declared, it behaves as an array of pointers instead.
You've not presented an example of a function that would fit your description. Certainly it is possible to pass an array of pointers, but it is quite possible to pass a pointer to an array instead. See above for examples.
Compiling the code gives a warning that is a bit of a clue to what is going on:
main.c:27:27: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("%3d", A[i*dimA[1] + j]);//This would work if the elements of A[] are the rows of a 2D array mapped into a 1D array
~~~ ^~~~~~~~~~~~~~~~
main.c:32:16: warning: incompatible pointer types passing 'int [2][2]' to parameter of type 'int *' [-Wincompatible-pointer-types]
printArray(A, dimA);
^
main.c:3:22: note: passing argument to parameter 'A' here
void printArray(int *A, int* dimA) {
When you declare your array:
int A[2][2] = {{1,2},{3,4}};
this is stored as one contiguous chunk of memory, as you stated. In memory, this is equivalent to:
int A[4] = {1,2,3,4};
However, whenever you go to lookup/dereference the values, depending on the type, the compiler is implicitly doing some bookkeeping for you. For the second case:
int A[4] = {1,2,3,4};
A[0] = *(&A + 0) = 1
A[1] = *(&A + 1) = 2
...
fairly straightforward, the index is simply an offset off the base address. However, for the first case:
y x
int A[2][2] = {{1,2},{3,4}};
y x
A[0][0] = *(&A + 2 * 0 + 0) = *(&A + 0) = 1
A[1][0] = *(&A + 2 * 1 + 0) = *(&A + 2) = 3
...
things start to look a bit confusing.
The first thing to note is that since the type is declared as an int[2][2], you must dereference it twice. That is what the first warning is complaining about. Because it was only dereferenced once, your int ** became an int *, which is not the same as an int.
The second thing to notice is because the type is declared as a multi-dimensional array, the compiler will do some bookkeeping for you. Since the array was being dereferenced on the first dimension, the size of the second dimension to stride to the correct location was already taken into account, so instead of col * j + i, you actually got col * (col * j + i) + i, which is not what you want!
To get the desired effect, you can either:
Cast A into an int *. This is what happened when you called your printArray function, and also why it works.
Access the array from the lowest dimension. Instead of saying A[i*dimA[1] + j], do A[0][i*dimA[1] + j]. This will correctly dereference to an int and also effectively bypass the bookkeeping.

Arrays and pointers in C

I have this piece of program and there are some parts that I don't understand...
‪#‎include‬ <stdio.h>
void increment( int *ptr) {++*ptr; }
int main(){
int a[] = { 5, 10 }, i = 0; // We have: vector a=[5,10] | i=0
increment(a); //This is the same as increment(&a) isn't it?
//So we have: a=[6,10] | i=0
increment(&i); // We increment i so: a=[6,10] | i=1
increment(&a[i]); //We increment the element at the position a[1]: a=[6,11] | i=1
increment(a+i); //OMG! What does it mean? a is an array isn't it? Then how can we
//make "a+1"? and how is this an address?
printf("\nResult: i= %d\n", i);
printf( "a[0] = %d\n" "a[1] = %d\n", a[0], a[1]);
return 0;
}
The printfs return:
i=1
a[0]=6
a[1]=12
How can this be explained?
When you pass an array to a function, the array decays to a pointer to the first element. So in the function, doing e.g. *ptr is dereferencing the first element in the array.
Also, because arrays decays to pointers, doing e.g. a[i] is equal to *(a + i).
Unrelated side-note: Because of the commutative of addition, the expression *(a + i) can be written as *(i + a) which leads to i[a] actually being a valid expression.
Arrays and pointer arithmetic are the same in C. If you've not come across pointer arithmetic here's a tutorial on it:
C Pointer Arithemtic
In essence:
a[3]
is the same as
*(a + 3)
Where a is a pointer.
When we give the name of array in the function call, the address of the arrya's first element is passed
ie; increment(a); is same as increment(&a[0]);
So while giving
incrementa(a+i);
since i and array a[] are both integers effectively that becomes
incrementa ( base address of array a (ie; &a[0]) + value of i )
i just acts as the offset to that array element
That it becomes the address of the second element which is &a[1]
so the 11 is modified to 12 while incrementing the value by the function

Confusion with Two Dimensional Array

Please consider the following 2-D Array:
int array[2][2] = {
{1,2},
{3,4}
};
As per my understanding:
- 'array' represents the base address of the 2-D array (which is the same as address of the first element of the array, i.e array[0][0]).
The actual arrangement of a 2-D Array in memory is like a large 1-D Array only.
Now, I know that base address = array. Hence, I should be able to reach the Memory Block containing the element: array[0][0].
If I forget about the 2-D array thing & try to treat this array as a simple 1-D array:
array[0] = *(array+0) gives the base address of the first array & NOT the element array[0][0]. Why?
A 2-D array does not store any memory address (like an Array of Pointers).
If I know the base address, I must be able to access this memory as a linear 1- Dimensional Array.
Please help me clarify this doubt.
Thanks.
array[0] is a one-dimensional array. Its address is the same as the address of array and the same as the address of array[0][0]:
assert((void*)&array == (void*)&(array[0]));
assert((void*)&array == (void*)&(array[0][0]));
Since array[0] is an array, you can't assign it to a variable, nor pass it to a function (if you try that, you'll be passing a pointer to the first element instead). You can observe that it's an array by looking at (array[0])[0] and (array[0])[1] (the parentheses are redundant).
printf("%d %d\n", (array[0])[0], (array[0])[1]);
You can observe that its size is the size of 2 int objects.
printf("%z %z %z\n", sizeof(array), sizeof(array[0]), sizeof(array[0][0]));
Here's a diagram that represents the memory layout:
+-------------+-------------+-------------+-------------+
| 1 | 2 | 3 | 4 |
+-------------+-------------+-------------+-------------+
`array[0][0]' `array[0][1]' `array[1][0]' `array[1][1]'
`---------array[0]---------' `---------array[1]---------'
`-------------------------array-------------------------'
"Thou shalt not fear poynter arythmethyc"...
int array[2][2] = { { 1, 2}, { 3, 4 } };
int *ptr = (int *)&array[0][0];
int i;
for (i = 0; i < 4; i++) {
printf("%d\n", ptr[i]);
}
Why does this work? The C standard specifies that multidimensional arrays are contigous in memory. That means, how your 2D array is arranged is, with regards to the order of its elements, is something like
array[0][0]
array[0][1]
array[1][0]
array[1][1]
Of course, if you take the address of the array as a pointer-to-int (int *, let's name it ptr), then the addresses of the items are as follows:
ptr + 0 = &array[0][0]
ptr + 1 = &array[0][1]
ptr + 2 = &array[1][0]
ptr + 3 = &array[1][1]
And that's why it finally works.
The actual arrangement of a 2-D Array in memory is like a large 1-D Array only.
yes, the storage area is continuous just like 1D arrary. however the index method is a little different.
2-D[0][0] = 1-D[0]
2-D[0][1] = 1-D[1]
...
2-D[i][j] = 1-D[ i * rowsize + j]
...
If I forget about the 2-D array thing & try to treat this array as a simple 1-D array: array[0] = *(array+0) gives the base address of the first array & NOT the element array[0][0]. Why?
the *(array+0) means a pointer to a array. the first element index in such format should be *((*array+0)+0).
so finally it should be *(*array)
A 2-D array does not store any memory address (like an Array of Pointers).
of course, you can . for example ,
int * array[3][3] ={ null, };
If I know the base address, I must be able to access this memory as a linear 1- Dimensional Array.
use this formal 2-D[i][j] = 1-D[ i * rowsize + j]...
Arrays are not pointers.
In most circumstances1, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.
The type of the expression array is "2-element array of 2-element array of int". Per the rule above, this will decay to "pointer to 2-element array of int (int (*)[2]) in most circumstances. This means that the type of the expression *array (and by extension, *(array + 0) and array[0]) is "2-element array of int", which in turn will decay to type int *.
Thus, *(array + i) gives you the i'th 2-element array of int following array (i.e., the first 2-element array of int is at array[0] (*(array + 0)), and the second 2-element array of int is at array[1] (*(array + 1)).
If you want to treat array as a 1-dimensional array of int, you'll have to do some casting gymnastics along the lines of
int *p = (int *) array;
int x = p[0];
or
int x = *((int *) array + 0);
1. The exceptions are when the array expression is an operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration.
I like H2CO3's answer. But you can also treat the pointer to the array as an incrementable variable like so:
int array[2][2] = { { 1, 2}, { 3, 4 } };
int *ptr = (int *)array;
int i;
for (i = 0; i < 4; i++)
{
printf("%d\n", *ptr);
ptr++;
}
the ++ operator works on pointers just fine. It will increment the pointer by one address of it's type, or size of int in this case.
Care must always be used with arrays in c, the following will compile just fine:
int array[2][2] = { { 1, 2}, { 3, 4 } };
int *ptr = (int *)array;
int i;
for (i = 0; i < 100; i++) //Note the 100
{
printf("%d\n", *ptr);
ptr++;
}
This will overflow the array. If you are writing to this you can corrupt other values in the program, including the i in the for loop and the address in the pointer itself.

Matrix Access Ansi C

Why the last printf in the main function doesn't print to the screen the value 10?
I know that in ANSI C, statically allocated matrix are arranged in memory in this way:
matrix: matrix[0][0], matrix[0][1],...,matrix[0][ColumnsDimension-1],matrix[1][0], etc
#include <stdio.h>
#include <stdlib.h>
#define dimRighe 20
#define dimColonne 30
int main()
{
int matrice[dimRighe][dimColonne];
int i,j;
for(i=0;i<dimRighe;i++)
for(j=0;j<dimColonne;j++)
matrice[i][j] = i+j;
matrice[0][3] = 10;
i = 0; j = 3;
printf("%d",*matrice[i*dimColonne+j]);
return 0;
}
Use *(matrice[i * dimColonne] + j) instead.
Why the last printf in the main function doesn't print to the screen the value 10?
Because matrice is an array of arrays ...
and matrice[whatever] is an array (which in most circunstances "decays" to a pointer to its first element)
and *matrice[whatever] is the contents of the first element of the array matrice[whatever].
In your code you have:
matrice[i*dimColonne+j]
Since i is 0 this evaluates to
matrice[j]
Since j is 3 this means
matrice[3]
When you print *matrice[3] that is equivalent to printing matrice[3][0] because matrice[3] is an array. And an array decays to a pointer to its first element.
But you don't want to do it this way at all. You should simply write matrice[i][j] and let the compiler do the work.
Change
printf("%d",*matrice[i*dimColonne+j]);
to simply be
printf("%d", matrice[i][j]);
if all you're worred about is printing out the right value. After all, that's how you assigned it.
If you're doing this as an exercise to understand how array subscripting works, then there are several things you need to remember.
First, except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be replaced with ("decay to") an expression of type "pointer to T", and its value will be the address of the first element of the array. The expression matrice is an array expression of type "20-element array of 30-element array of int"; in most circumstances, it will be converted to an expression of type "pointer to 30-element array of int", or int (*)[30]. Similarly, the expression matrice[i] is an expression of type "30-element array of int", and in most cirumstances it will be converted to an expression of type "pointer to int", or int *.
Here's a handy table to remember all of this:
Declaration: T a[N];
Expression Type Decays to
---------- ---- ---------
a T [N] T *
&a T (*)[N]
*a T
a[i] T
Declaration: T a[M][N];
Expression Type Decays to
---------- ---- ---------
a T [M][N] T (*)[N]
&a T (*)[M][N]
*a T [N] T *
a[i] T [N] T *
&a[i] T (*)[N]
*a[i] T
a[i][j] T
Second, the subscripting operation a[i] is defined as *(a + i); that is, you compute an address based on i number of elements (NOT BYTES) from the base address of your array and dereference the result. For example, if a is an array of int, then *(a + i) will give you the value of the i'th integer after a. If an is an array of struct foo, then *(a + i) will give you the value of the i'th struct after a. Pointer arithemtic always takes the size of the base type into account, so you don't need to worry about the number of bytes in the offset.
The same logic applies to multidimensional arrays, you just apply the rule recursively for each dimension:
a[i][j] == *(a[i] + j) == *(*(a + i) + j)
a[i][j][k] == *(a[i][j]+ k) == *(*(a[i] + j) + k) == *(*(*(a + i) + j) + k)
Note that you should almost never have to do these dereferences manually; a compiler knows what an array access looks like and can optimize code accordingly. Under the right circumstances, writing out the dereferences manually can result in slower code than using the subscript operator.
You can index into a 2-d array as if it were a 1-d array like so:
a[i*rows + j] = val;
but I wouldn't (the types of the expressions don't match up cleanly). Note that you multiply i by the number of rows, not columns.
You could also print it like this:
char *matrixAsByteArray = (char *) matrice;
char *startIntAddr = matrixAsByteArray + i * dimColonne * sizeof(int) + j * sizeof(int);
int outInt = *startIntAddr | *(startIntAddr + 1) << 8 | *(startIntAddr + 2) << 16 | *(startIntAddr + 3) << 24;
printf("%d", outInt);
What this does is first, it converts your matrix to an array of bytes, then, it gets the starting address of the integer you need, and then it reconstructs the integer from the first four bytes read from that address.
This is a bit overkill, but a fun solution to the problem.

Resources