Results 1 to 6 of 6

Thread: C - 2 Dimensional Arrays

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Answers
    3

    C - 2 Dimensional Arrays

    How to access an element of a two dimensional array without using the index?


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    5

    Re: C - 2 Dimensional Arrays

    If i got ur Question right...i guess..u can use pointers...Let me know wat exactly u wan...


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    3

    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


  4. #4
    Junior Member
    Join Date
    Jul 2007
    Answers
    5

    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.

  5. #5
    Junior Member
    Join Date
    Jun 2007
    Answers
    3

    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.

  6. #6
    Junior Member
    Join Date
    Jul 2007
    Answers
    3

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact