Need help with this code on pointers

This is a discussion on Need help with this code on pointers within the C Programming forums, part of the General Programming Boards category; Hi All, Please help me with the following piece of code. I do not understand why it prints the same ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14

    Question Need help with this code on pointers

    Hi All,

    Please help me with the following piece of code. I do not understand why it prints the same value for a, *a and also **a. Could someone explain me how it works?
    Code:
    #include <conio.h>
    main()
    {
     int a[2][3][4] = {
                            {
                             1,2,3,4,
                             5,6,7,8,
                             9,1,1,2
                            },
                            {
                             2,1,4,7,
                             6,7,8,9,
                             0,0,0,0
                            }
                      };
     clrscr();
     printf("\n%u\n%u\n%u\n%d",a,*a,**a,***a);
     getch();
    }
    Thanks,
    Babu

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I took out conio.h and all dependencies upon it. When compiled with warnings turned up, the compiler gave me this:

    Code:
    :4: warning: return type defaults to `int'
    : In function `main':
    :7: warning: missing braces around initializer
    :7: warning: (near initialization for `a[0][0]')
    :18: warning: implicit declaration of function `printf'
    :18: warning: unsigned int format, pointer arg (arg 2)
    :18: warning: unsigned int format, pointer arg (arg 3)
    :18: warning: unsigned int format, pointer arg (arg 4)
    :20: warning: control reaches end of non-void function
    Anyway, what I think the idea is behind what you want to know..... Array names, when taken by themselves (like for function calls), are generally pointers to the first element of the array. This means a will be a pointer to the first element of the entire array, which also happens to be what *a points to, which also happens to be what **a points to. The first element of the entire array also happens to be the first element of the first inner array, which also happens to be the pointer to the first innermost array.

    That may be confusing, so work it out on paper.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    72
    &#37;u i have never seen this
    proud to be from aui www.aui.ma and also a great elton john's fan

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Check the man pages or google for the format flags of printf() and scanf() if you want to see a list of them. He's just printing everything as an unsigned int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-16-2008, 06:01 PM
  2. Replies: 3
    Last Post: 09-22-2005, 11:49 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Replies: 16
    Last Post: 11-01-2002, 09:28 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21