What is output of this program ?

what is output of this program and please explain in detail..


#include
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}

Replies

  • [Prototype]
    [Prototype]
    p=&a[2][2][2];

    Only if you're trying to take the address of the last element in the array. If you actually want the start address of the array, then it should be only &a.

    *q=***a;

    How come the "a" became pointer suddenly?

    So basically, the output of the program is error because "a" is not a pointer. Not a compilation error but a logical error.
  • hare singh nayak
    hare singh nayak
    actually sir there is no error
    when i am running this code the output is like that
    5641496----10

    and i am not able to understand what is happending here so please explain in lit bit detail..
  • silverscorpion
    silverscorpion
    When you declare the array, it's actually stored as follows:

    ___________________________
    | 10 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
    -------------------------------

    But, since it's declared as a 3-d array, it should be visualized a little differently. You can think of it as an array containing 2 elements, each of which is an array containing 2 elements each of which is a 2-element array again.

    So, if you just specify a, it points to the address of the first element of the array. Specifically, 'a' points to the address of the array
    _______
    |10 | 2 |
    | 3 | 4 |
    --------

    (Note that a only has 2 elements, one is the 2x2 array shown above. The other is the 2x2 array from the other 4 elements 5,6,7 and 8)

    Now, *a refers to the value in the 0[sup]th[/sup] position of the array... so, when you put *a, that returns the same 2x2 array shown above.

    Now comes **a. As already said, *(some_array) returns the value of the first element in that array. So, **a returns the first element of the 2x2 array, which is a 1x2 array. And that is
    _______
    |10 | 2 |
    --------

    So, when you do it 3 times, ie., when you use ***a, it returns the first value of the 1x2 array, which is 10. Voila!!

    Hope it's clear! 😀
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Hats off, #-Link-Snipped-#! Great explanation!

    Correct me if I am wrong, but if "a" is declared as array then it can be used as pointer.

    The answer to the question #-Link-Snipped-# asked.
  • hare singh nayak
    hare singh nayak
    silverscorpion
    When you declare the array, it's actually stored as follows:

    ___________________________
    | 10 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
    -------------------------------

    But, since it's declared as a 3-d array, it should be visualized a little differently. You can think of it as an array containing 2 elements, each of which is an array containing 2 elements each of which is a 2-element array again.

    So, if you just specify a, it points to the address of the first element of the array. Specifically, 'a' points to the address of the array
    _______
    |10 | 2 |
    | 3 | 4 |
    --------

    (Note that a only has 2 elements, one is the 2x2 array shown above. The other is the 2x2 array from the other 4 elements 5,6,7 and 8)

    Now, *a refers to the value in the 0[sup]th[/sup] position of the array... so, when you put *a, that returns the same 2x2 array shown above.

    Now comes **a. As already said, *(some_array) returns the value of the first element in that array. So, **a returns the first element of the 2x2 array, which is a 1x2 array. And that is
    _______
    |10 | 2 |
    --------

    So, when you do it 3 times, ie., when you use ***a, it returns the first value of the 1x2 array, which is 10. Voila!!

    Hope it's clear! 😀


    Tank you silverscorpion very nice explanation..
  • silverscorpion
    silverscorpion
    English-Scared
    Hats off, #-Link-Snipped-#! Great explanation!

    Correct me if I am wrong, but if "a" is declared as array then it can be used as pointer.

    The answer to the question #-Link-Snipped-# asked.
    We are not actually using a as a pointer. A pointer stores the address of another variable. Here, we are merely trying to get the value stored in a given address, and that happens to be the address where the array is stored..

    The * operator gets the value stored in whatever address we use it with. And here, we use it with a. And when a is declared as an array, the name 'a' alone, without any index, 'points' to the first element of the array. So, saying just a and saying &a[0] are same.

    We can declare another variable, say b, as an int. And then get the value stored in that variable by using *(&b). In this array, we're doing the same, just that, instead of *(&a[0]), we use *a

You are reading an archived discussion.

Related Posts

Facebook's Developer World HACK 2012 is happening around the globe in 12 cities. It will run for two months and software developers will develop amazing Facebook apps at the HACK...
i'm so much afraid of dbms..how to handle this subject??
I wish to be a member in eBIZ.com is it a worth or not, i came to know about the terms and conditions ,membership in that, can any one lend...
hi dude, my name is niranjan and i am little confused by the colleges some offering vehicle engineering as a masters degree....and other automotive...which one is better and what is...
sir,in 2014 i will complete my btech...n my age will only 20 year that time..n i am not able to attempt IES exam..i want govt job? what can i do?...