0

i want to print the value which contain in the multidimensional array.I know that if we write something like arr[3][4] then arr[0] is the pointer to the first element in the that multidimensional array. I want to ask why this code give me garbage value after the program print the number containing in the multidimensional array?

this is the code:

    #include <stdio.h>

    #define ROW 3
    #define COLL 4

    int main(void) {
    int arr[ROW][COLL]={{1,2,3,4},
                  {5,6,7,8},
                  {9,10,11,12}};
    int *ptr;

    for(ptr=arr[0];ptr<arr[ROW]+COLL;ptr++){
        printf("%d ",*ptr);
    }
    printf("\n");
    return 0;
    }

this is the result when i compile the above code:

1 2 3 4 5 6 7 8 9 10 11 12 -1079481492 134513936 0 -1079481352 

but after changing the for loop to the following:

for(ptr=arr[0];ptr<=arr[ROW-1]+COLL-1;ptr++)

the code work and give the exact number which contain in the multidimensional array.

2
  • array access row from 0..1..and 2 and here you want to access 3 means fourth raw's first element which is not there. So it invokes undefined behavior. Commented Apr 23, 2014 at 13:47
  • for(ptr=&arr[0][0];ptr<&arr[ROW-1][COLL];ptr++){ Commented Apr 23, 2014 at 23:04

4 Answers 4

2

Because arr[ROW] is an out-of-bounds access. The last valid position in arr is arr[ROW-1]. Thus the first version of your code invokes undefined behaviour.

Sign up to request clarification or add additional context in comments.

2 Comments

what is out-of-bound acces? can you explain that?
@harianja: He means that you're trying to access something past the end of the array.
0

The pointer to the last element is

arr[ROW-1] + COLL - 1

So you loop should be

    for (ptr = arr; ptr < arr[ROW-1] + COLL; ptr++) {

Comments

0

Elements of an array are indexed from 0 so the last element in your matrix is arr[ROW-1][COLL-1].

When your pointer reaches arr[2] + COLL there is no such element because the second coordinate of the last element in last row is [COLL-1].

Elements of an array are stored in 1D so arr[0] + 4 is acctualy arr[1][0]. When you increment ptr++ it tells the compiler to access the next memory location after the current. So, when ptr points to a[0] + 4 it works because the compiler access the fifth value after a[0][0] which is actually the first value in the next row, arr[1][0], because their memory locations are next to each other.

When your ptr points to arr[4] + i, i = 0, 1, 2, 3 you get bad output because those elements are not in your array.

Comments

0

Because your first element, 1, is at arr[0]+0,

5 is at arr[1]+0,

9 is at arr[2]+0,

10 is at arr[2]+1,

11 is at arr[2]+2,

and 12, which is the last element of your array is at arr[2]+3.

but your for loop keeps moving even after the array ends, that is, until the memory location arr[3]+4 is reached. Giving you four garbage values because we have stored nothing in the memory after arr[2]+3.

so the for loop should be like:

for (ptr=arr; ptr <= arr[ROW-1]+COLL-1; ptr++);

which is equivalent to:

for (ptr=arr[0]; ptr <= arr[ROW-1]+COLL-1; ptr++);

as arr[0] and arr point to the same location in memory. NOTE- try reading how memory is allocated in arrays of multiple dimensions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.