CrazyEngineers
  • What is output of this program ?

    Updated: Oct 6, 2024
    Views: 971
    what is output of this program and please explain in detail..


    #include<stdio.h>
    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);
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • [Prototype]

    MemberAug 13, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • hare singh nayak

    MemberAug 13, 2012

    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..
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberAug 13, 2012

    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! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberAug 13, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • hare singh nayak

    MemberAug 13, 2012

    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..
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberAug 13, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register