How to access an element of a two dimensional array without using the index?
How to access an element of a two dimensional array without using the index?
If i got ur Question right...i guess..u can use pointers...Let me know wat exactly u wan...
H Kshama
Suppose i have an array a[4][4] i want to know what is the value at the position a[2][3] but i dont want to use these indexes. How can i do this ?
regards
Vikram
Hi Vikram,
As i told earlier...by using pointer concept u can do it..but ther again u need to mention the indexes..2, and 3 but not a direct access as a[2][3].
jus go through the code
main()
{
int arr[4][4] = {
{1, 2, 3, 4},
{6, 7, 8, 9},
{3, 2, 8, 0},
{5, 6, 7, 8}
};
// to access and print the elements without using the indexes.
int *p; // declare a pointer to the type int.
p = (int *)arr; // make the pointer point to the begining of the array arr
printf("the beginnig addr of the array is %d\n", p);
printf("the element being accessed is %d\n", *(*(arr + 2) + 3)); // to access the value at a[2][3]
}
as i mentioned to get the value at a[2][3] we make use of pointer as *(*(arr + 2) + 3),
According to the storage concept of arrays it prints the value stored at the position [2][3], i.e 0 in our example.
As per my knowledge goes, this is the method to access array elements without using the indexes.
Hope u r convinced..let me know
Regards
Kshama V
Last edited by kshama.v; 07-21-2007 at 06:00 AM.
Hi
try the following code
int arr[4][4] = {
{1, 2, 3, 4},
{6, 7, 8, 9},
{3, 2, 8, 10},
{5, 6, 7, 8}
};
// to access and print the elements without using the indexes.
int *p; // declare a pointer to the type int.
p = (int *)arr; // make the pointer point to the begining of the array arr
printf("the beginnig addr of the array is %d\n", p);
printf("the first element of the array is %d\n", *(p));
printf("the second element of the array is %d\n", *(p+1));
printf("the third element of the array is %d\n", *(p+2));
printf("the fourth element of the array is %d\n", *(p+3));
printf("the fifth element of the array is %d\n", *(p+4));
printf("the sixth element of the array is %d\n", *(p+5));
printf("the seventh element of the array is %d\n", *(p+6));
Last edited by shine_babu; 07-25-2007 at 05:59 AM.
Hi Kshama,
This was the question asked to me in one of the interview. I told the same pointer concept. But, they didnt agreed for that. So i thought it is better to ask here. Is there any other way to access it?
thanks and regards
Vikram