Thread: output.....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    output.....

    I am new to c and i have been trying to understand the fundas. i tested a program on arrays which goes like this:

    insert
    Code:
    void main(){
    
    	int n[3][3]= {2,4,3,6,8,5,3,5,1};
    	printf("\n%d \n%d \n%d \n%d \n%d",n, *n, &n, *(*(n+1)+0),n[2][2]);
    }
    
    
    The output that i get is this:
    1244692
    1244692
    1244692
    6
    1

    I had a question that n contains the base address of the array and *n should contain the value at address contained in n which should be the first element in the array.

    Also &n is also the same value is this value not supposed to be different?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When you refer to an array by name and not to a specific element, it "decomposes" into a pointer to the first element. So n, *n, &n would all return &n[0].

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so the address of the first element is (apparently) 1244692. The address of n is the same as the address of the first element. And *n is the same as n[0] - which is the first 3 element array in the second dimension. Again it has the same address as n[0][0].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    I am new to c and i have been trying to understand the fundas. i tested a program on arrays which goes like this:

    insert
    Code:
    void main(){
    
    	int n[3][3]= {2,4,3,6,8,5,3,5,1};
    	printf("\n%d \n%d \n%d \n%d \n%d",n, *n, &n, *(*(n+1)+0),n[2][2]);
    }
    
    
    The output that i get is this:
    1244692
    1244692
    1244692
    6
    1

    I had a question that n contains the base address of the array and *n should contain the value at address contained in n which should be the first element in the array.

    Also &n is also the same value is this value not supposed to be different?
    n - the name of an array gives the base address of the array.
    *n - Look it like this (*(n+0)+0) which is nothing but the address of n[0][0] if it would have been **n, it would have printed the very first element.
    &n - The array 'n' is itself stored at some place which is the same as from where the first element starts so &n and n print the same thing but it'll be better for you to check it by printing "n+1" and "&n+1", you will know what I mean and what's the differnce between "n" and "&n".
    By the way dont use void main.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM