output value not as expected @beginner

This is a discussion on output value not as expected @beginner within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    nmn
    nmn is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    nmn
    nmn is offline
    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
    289
    *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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    *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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  7. #7
    nmn
    nmn is offline
    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
    ZuK
    ZuK is offline
    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
    nmn
    nmn is offline
    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
    ZuK
    ZuK is offline
    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
    nmn
    nmn is offline
    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, 03: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

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