Thread: output value not as expected @beginner

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    18

    output value not as expected @beginner

    Code:
    #include<stdio.h>
    void main()
    {
    int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    
    
    printf("\n%d %d %d", *n , n[0][0], n[2][2]);
    }
    why am i getting garbage values in the output

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What did you expect to get from *n?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    isn't *n same as n[0][0]??

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    *n is NOT the same as anything....

    and neither is
    Code:
    void main()
    your programs should always look like the following unless your main is taking in command line arguments

    Code:
    int main(void)   //void in the () is optional
    {
    .
    .
    .
    return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    *n is going to be the same as n[0] (which is the same as &n[0][0]), which is a pointer to the first element of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    thats his prob, *n is not pointing to the array. hence the garbage output from the printf for the *n position

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    this code:
    Code:
    #include<stdio.h>
    int main()
    {
    int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    
    
    printf("\n %d %d %d", *n , n[0][0], n[2][2]);
    return 0;
    }
    and this code:
    Code:
    #include<stdio.h>
    int main()
    {
    int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    
    
    printf("\n %d %d %d", n[0][0] , n[0][0], n[2][2]);
    return 0;
    }
    are giving different outputs!! any explanations??

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    guess you want that
    Code:
    #include<stdio.h>
    int main()
    {
    int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    
    
    printf("\n %d %d %d", **n , n[0][0], n[2][2]);
    return 0;
    }
    Kurt

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    thanks a lot kurt !!
    though can you or someone provide a little explanation since n is a double-dim array and *n should denote the value at its base address
    Last edited by nmn; 03-20-2013 at 03:30 PM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    *n is a pointer to the first element in the array and that is an array of 3 integers and if you dereference that (**n) you get n[0][0]
    Kurt

  11. #11
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    ok...got it!!
    thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-03-2012, 09:24 AM
  2. Not getting expected Output
    By ldysmann in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2012, 04:40 AM
  3. Replies: 9
    Last Post: 09-11-2011, 08:28 PM
  4. Array output not as expected
    By Futomara in forum C Programming
    Replies: 26
    Last Post: 11-04-2010, 11:24 AM
  5. non expected output
    By c++.prog.newbie in forum C Programming
    Replies: 2
    Last Post: 09-27-2004, 05:41 PM