pointers and array

#include<stdio.h>
#include<conio.h>

int main()
{
int arr1[2][3]={1,2,3,4,5,6};
printf("%d\n",*(arr1+0));
printf("%d",arr1);
getch();
}

arr1 is the address of arr1[0][0]...right??
so *(arr1) should be arr1[0][0] i.e. equal to 1...right??
but when i output both printf statements..i get address as outpt in both..why??
A two-dimensional array is an array of arrays. That is an element of a two-dimensional array is in turn a one-dimensional array.

So in expressions a two-dimensional array is converted to the pointer to its first element. In your example arr1 is converted to a pointer to one-dimensional array of three elements. Let name this pointer as p. When its declaration will look the following way

int ( *p )[3];

It correcponds to arr1. arr1 + 0 is corresponds to p + 0 and is again a pointter to one dimensional array. *( arr1 + ) that corresponds to *( p + 0 ) is one-dimensional array. So

printf("%d\n",*(arr1+0));

prints an address of the first element of this one-dimensional array because in expressions array is converted to an address of its first element.
Last edited on
u said arr1 is the address of the first element of 2-D array...This means *arr1 should print the value of the first element because it points to that element only..so it should print 1 ..hope u r able to get my doubt.!!
The best way to look at this is by looking at what a 1D array is:
1
2
int myArray[3] = {1,2,3};
int *p = myArray;

In the previous code, p will point to the first element. This will be the memory address. To print out
1
you need to dereference the pointer, *p

I believe you understand that much. Now, let's look at a 2D array:
1
2
int myArray[2][3] = { {1,2,3}, {4,5,6} };
int *p = myArray[0];

p will have the same value as above. It points to the first value. But in this code:
1
2
int myArray[2][3] = { {1,2,3}, {4,5,6} };
int *p = myArray;

p will point to the first array in the 2D array, that is, it will point to the array {1,2,3}. If you dereference it, it will have a reference to the first element of that array.

I haven't gotten a chance to test it yet, but I believe this code works for what you want it to do:
1
2
3
int myArray[2][3] = { {1,2,3}, {4,5,6} };
int *p[2] = myArray;
int *pp = p;

pp should now point to the first value, 1, in the 2D array. If you increment that pointer, it should display 1,2,3,4,5,6.

I believe you can even shorten up the code as so:
1
2
int myArray[2][3] = { {1,2,3}, {4,5,6} };
int **p = myArray;

p should be the same as pp in the previous example.

I hope this helps.
Its because this works only with single dimention array.
you should use printf("%d\n",**(arr1)); and output will be one
@vgoel38 (12)

u said arr1 is the address of the first element of 2-D array...This means *arr1 should print the value of the first element because it points to that element only..so it should print 1 ..hope u r able to get my doubt.!!


The first element of arr1 is

arr1[0] that is an one-dimensional array that is initialized as { 1, 2, 3 }. There is no such C/C++ standard function that prints a one-dimensional array of type int. But if you change the declaration of your array the following way

char arr1[2][3] = { "12", "34" };

then after the executing of the statement

printf("%s\n",*(arr1+0));

string literal "12" will be printed that corresponds to the first element of your two-dimensional array.
Topic archived. No new replies allowed.