I am trying to learn C,but I got stuck over the following piece of codes. What I understood from my basic knowledge about C is that the array name itself is a pointer pointing to the first element of the array. And elements in a 2D array is stored one row after the other.So I am trying print the values of elements in a 1D array and 2D array using array name as a pointer. But for the Code 1
it is working as I expected. But Code 2
is giving unexpected result (It is giving the starting addresses of each row). What is the reason for this to happen? I think something is wrong with my understanding. Can anybody clarify the mistake.
// Code 1
//-------
#include <stdio.h>
int main () {
int array[2][3] = {{5, 7, 9}, {2, 5, 77}};
int j;
for (j=0; j < 6; j++)
printf("%d\t", *(array+j));
return 0;
}
//Code 2
//------
// #include <stdio.h>
// int main () {
// int array[6] = {5, 7, 9, 2, 5, 77};
// int j;
// for (j=0; j < 6; j++)
// printf("%d\t", *(array+j));
// return 0;
// }