Geeks Talk

Prepare for your Next Interview




C - 2 Dimensional Arrays

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?...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-18-2007
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vikramNR is on a distinguished road
C - 2 Dimensional Arrays

How to access an element of a two dimensional array without using the index?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-19-2007
Junior Member
 
Join Date: Jul 2007
Location: Bangalore
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
kshama.v is on a distinguished road
Re: C - 2 Dimensional Arrays

If i got ur Question right...i guess..u can use pointers...Let me know wat exactly u wan...
Reply With Quote
  #3 (permalink)  
Old 07-19-2007
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vikramNR is on a distinguished road
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
Reply With Quote
  #4 (permalink)  
Old 07-21-2007
Junior Member
 
Join Date: Jul 2007
Location: Bangalore
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
kshama.v is on a distinguished road
Re: C - 2 Dimensional Arrays

Quote:
Originally Posted by vikramNR View Post
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.
Reply With Quote
  #5 (permalink)  
Old 07-25-2007
Junior Member
 
Join Date: Jun 2007
Location: India
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
shine_babu is on a distinguished road
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.
Reply With Quote
  #6 (permalink)  
Old 07-26-2007
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vikramNR is on a distinguished road
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
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

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


All times are GMT -4. The time now is 09:18 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved