Prepare for your Next Interview
This is a discussion on C - 2 Dimensional Arrays within the C and C++ forums, part of the Software Development category; How to access an element of a two dimensional array without using the index?...
|
|||
|
Re: C - 2 Dimensional Arrays
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 |
|
|||
|
Re: C - 2 Dimensional Arrays
Quote:
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. |
|
|||
|
Re: C - 2 Dimensional Arrays
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. |
|
|||
|
Re: C - 2 Dimensional Arrays
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 |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| About arrays memory locations where they are storing | BHASKAR CHAUHAN | Java | 1 | 07-17-2007 11:14 PM |
| How Arrays are declared in safari? | TigerElango | JavaScript | 0 | 02-03-2007 08:04 AM |