arrays in c programming

Status
Not open for further replies.

vikas_r_s

Broken In
hi everyone,
i am a newbi to programming.
can any one tell me in c language a two dimensional array name is of what type?
how come in arr[3][3], arr and *arr give same value(address)?
 

plsoft

Journeyman
when we use the '*' we are referring to the value of that variable (array in this case) which the pointer is pointing to, not the value of the pointer itself. However, if you use the 'address of' (&) operator, it will give the memory address of the variable it points to.
 

tuxfan

Technomancer
Study the pointers well and you will understand this. Go for Pointers in C by Yashwant Kanetkar. Its a small book, worth its price. He will explain you pointers better than any of us. :) Pointers are pretty powerful (and desctructive if misused)!
 
OP
V

vikas_r_s

Broken In
plsoft waht you say is true in a uni dimensional array.
but try the following code
int main()
{
int a[3][3];
printf("%d\n",a);
printf("%d\n",*a);
system("PAUSE");
return 0;
}

both printfs give same value i.e. memory address.HOW?
 

bukaida

In the zone
The name of the array is the pointer to the first element of that array. Like wise a[5] and 5[a] will give you the same value. Go through the Kanitkar book(study the array and pointer chapter).
 
Last edited:

casanova

The Frozen Nova
Pointers is a very vast concept, there are dedicated books for it and yashwant kanetkar says "The no. of times your comp will hang is directly proportional to the no. of pointers you use".
BTW * mans value at
and & means address of.
So by using * u get the value.
 

ilugd

Beware of the innocent
your array has 9 elements like if the first element is at memory loc 0 then the elements are there upto address 8.
a is from 0 to 8
a[0] is elements at 0,1,2
a[1] is elements at 3,4,5
a[2] is elements at 6,7,8

value of a is 0 and *a that is a[0] is 0 again. That is how i understand it. But my c is a bit rusty around pointers. Am I right anyone?
 

hitesh_hg

Journeyman
vikas_r_s said:
hi everyone,
i am a newbi to programming.
can any one tell me in c language a two dimensional array name is of what type?
how come in arr[3][3], arr and *arr give same value(address)?

Sorry but i couldn't bear to read all the above responses and dunno if it has already been answered but here is how it is..

Since arr[3][3] is a 2D array you are basically creating a 2nd level pointer.
hence arr & *arr point to same address a[0][0]. to refer to the value use **arr .

And yes over use of pointer is lethal (for the brain:D)
 
Status
Not open for further replies.
Top Bottom