2 D Array Prob Please Help

Discussion in 'C Programming' started by sachinv1821@gmail.com, Feb 20, 2008.

  1. Guest

    hi all i have simple Problem please tell me the Solution if u know??
    main()
    {
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    printf("%u %u %u",a[0],a[1],a[2]);
    }
    when i execute this program i am getting a Fixed address Value(multi
    digit value)
    can any body explain me ahy this is giving the same value for
    different index

    Thanks In Advance
    -Sachin
     
    , Feb 20, 2008
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > hi all i have simple Problem please tell me the Solution if u know??
    > main()
    > {
    > int a[3][3]={1,2,3,4,5,6,7,8,9};
    > printf("%u %u %u",a[0],a[1],a[2]);
    > }
    > when i execute this program i am getting a Fixed address Value(multi
    > digit value)
    > can any body explain me ahy this is giving the same value for
    > different index


    You probably want:

    #include <stdio.h>

    int main(void) {
    int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    printf("%d, %d, %d\n", a[0][0], a[1][0], a[2][0]);
    return 0;
    }
     
    santosh, Feb 20, 2008
    #2
    1. Advertising

  3. said:

    > hi all i have simple Problem please tell me the Solution if u know??
    > main()
    > {
    > int a[3][3]={1,2,3,4,5,6,7,8,9};
    > printf("%u %u %u",a[0],a[1],a[2]);
    > }


    You forgot to #include <stdio.h> - you'll need to fix that if you want to
    use printf in your program, because its behaviour if you don't is allowed
    to be arbitrary. You'll also want to use int main(void) rather than just
    main(), and add a return statement to your function, e.g. return 0;

    The problem with your program is that you are expecting printf to do more
    than it can in fact do. It knows about chars, and unsigned chars, and
    short ints, and unsigned short ints, and ints, and unsigned ints, and long
    ints, and unsigned long ints, and doubles, and long doubles. It even knows
    about pointers to void. But that's all it knows about, in the way of data
    types.

    a[0] is equivalent to *(a + 0), i.e. *a. (C doesn't let you use array
    values, so in a value context like this, the value you actually get is a
    pointer to the array's first element, i.e. a pointer to an array of three
    int.) Dereferencing gives the array itself, i.e. an int[3], which decays
    to an int *. But printf doesn't understand about pointers-to-int (except
    in the pathological case of %n, which doesn't do what you want). And even
    if it did, it wouldn't understand them in the context of %u, which is used
    for printing unsigned ints, not pointers-to-int.

    The proper course is to use %p as the format specifier and (void *)a[0] as
    the argument.

    Unfortunately, converting a pointer to some type (or an array of some
    type!) into a void * is a bit like converting a place into GPS
    co-ordinates. Contextual information is lost. Consider a house, a room, a
    wall, a brick, and a "brick atom". All could have the same GPS co-ords,
    even though they are very different things.

    The right way to think about this is to work out exactly *why* you need to
    know the information you're trying to display. The chances are that you
    don't actually need it, and are merely curious - in which case, the proper
    answer is that it doesn't /matter/ what glyphs are scribbled on your
    screen when you print an address, provided only that you understand the C
    type system. If you genuinely *do* need the information, it will either be
    for a spurious reason (e.g. you're a student, and your teacher is stupid
    enough to require you to find out this information) or for a real
    technical reason. If the latter, you will be sufficiently experienced to
    realise that you're trying to step outside the bounds of behaviour that C
    can guarantee, and into the realms of architecture-specific details.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 20, 2008
    #3
  4. Bartc Guest

    santosh wrote:
    > wrote:
    >
    >> hi all i have simple Problem please tell me the Solution if u know??
    >> main()
    >> {
    >> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >> printf("%u %u %u",a[0],a[1],a[2]);
    >> }
    >> when i execute this program i am getting a Fixed address Value(multi
    >> digit value)
    >> can any body explain me ahy this is giving the same value for
    >> different index

    >
    > You probably want:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    > printf("%d, %d, %d\n", a[0][0], a[1][0], a[2][0]);
    > return 0;
    > }


    Or more likely:

    printf("%d %d %d", a[0][0], a[0][1], a[0][2]);

    Which gives the output 1 2 3

    --
    Bart
     
    Bartc, Feb 20, 2008
    #4
  5. Bartc Guest

    Richard Heathfield wrote:
    > said:
    >
    >> hi all i have simple Problem please tell me the Solution if u know??
    >> main()
    >> {
    >> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >> printf("%u %u %u",a[0],a[1],a[2]);
    >> }

    >

    <snip>

    You haven't explained why a linear list of 9 values is acceptable to
    initialise a 3x3 array. Even if (yet another) quirk of the language allows
    this, it must make more sense to write:

    int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};


    --
    Bart
     
    Bartc, Feb 20, 2008
    #5
  6. Bartc said:

    > Richard Heathfield wrote:
    >> said:
    >>
    >>> hi all i have simple Problem please tell me the Solution if u know??
    >>> main()
    >>> {
    >>> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >>> printf("%u %u %u",a[0],a[1],a[2]);
    >>> }

    >>

    > <snip>
    >
    > You haven't explained why a linear list of 9 values is acceptable to
    > initialise a 3x3 array. Even if (yet another) quirk of the language
    > allows this, it must make more sense to write:
    >
    > int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};


    Yes, it does make more sense to write it like that.

    Alas, none of us has infinite time, care, or patience. Whilst it would be
    wonderful to think that every article we post here explains absolutely
    everything the OP needs to know (whether or not they realise the need), in
    practice life doesn't work like that. Very few replies are as complete as
    we would like them to be.

    Perhaps you yourself would care to explain to the OP why such a linear
    initialiser list is, or is not, acceptable.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 20, 2008
    #6
  7. Bartc Guest

    "Richard Heathfield" <> wrote in message
    news:...
    > Bartc said:
    >
    >> Richard Heathfield wrote:
    >>> said:
    >>>
    >>>> hi all i have simple Problem please tell me the Solution if u know??
    >>>> main()
    >>>> {
    >>>> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >>>> printf("%u %u %u",a[0],a[1],a[2]);
    >>>> }
    >>>

    >> <snip>
    >>
    >> You haven't explained why a linear list of 9 values is acceptable to
    >> initialise a 3x3 array. Even if (yet another) quirk of the language
    >> allows this, it must make more sense to write:
    >>
    >> int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};

    >
    > Yes, it does make more sense to write it like that.


    > Perhaps you yourself would care to explain to the OP why such a linear
    > initialiser list is, or is not, acceptable.


    Actually, I thought you might explain it to /me/..

    But never mind, it's clearly a hangover from the early days of C, and must
    now be common and accepted practice if it's use elicits no comments.

    --
    Bart
     
    Bartc, Feb 20, 2008
    #7
  8. Bartc said:

    >
    > "Richard Heathfield" <> wrote in message
    > news:...
    >> Bartc said:
    >>
    >>> Richard Heathfield wrote:
    >>>> said:
    >>>>
    >>>>> hi all i have simple Problem please tell me the Solution if u know??
    >>>>> main()
    >>>>> {
    >>>>> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >>>>> printf("%u %u %u",a[0],a[1],a[2]);
    >>>>> }
    >>>>
    >>> <snip>
    >>>
    >>> You haven't explained why a linear list of 9 values is acceptable to
    >>> initialise a 3x3 array. Even if (yet another) quirk of the language
    >>> allows this, it must make more sense to write:
    >>>
    >>> int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};

    >>
    >> Yes, it does make more sense to write it like that.

    >
    >> Perhaps you yourself would care to explain to the OP why such a linear
    >> initialiser list is, or is not, acceptable.

    >
    > Actually, I thought you might explain it to /me/..
    >
    > But never mind, it's clearly a hangover from the early days of C, and
    > must now be common and accepted practice if it's use elicits no comments.


    It's perfectly legal. Read the Initialization section of the Standard, and
    you'll even see an example:

    *** example from 3.5.7 of C89 (draft) ***

    float y[4][3] = {
    { 1, 3, 5 },
    { 2, 4, 6 },
    { 3, 5, 7 },
    };

    is a definition with a fully bracketed initialization: 1, 3, and 5
    initialize the first row of the array object y[0] , namely y[0][0] ,
    y[0][1] , and y[0][2] . Likewise the next two lines initialize y[1]
    and y[2] . The initializer ends early, so y[3] is initialized with
    zeros. Precisely the same effect could have been achieved by

    float y[4][3] = {
    1, 3, 5, 2, 4, 6, 3, 5, 7
    };

    The initializer for y[0] does not begin with a left brace, so three
    items from the list are used. Likewise the next three are taken
    successively for y[1] and y[2] .

    *** end of example ***

    Examples are not normative, of course, but I think the intent of the
    normative text is better expressed here than in the normative text itself!

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 20, 2008
    #8
  9. wrote:
    > hi all i have simple Problem please tell me the Solution if u know??
    > main()
    > {
    > int a[3][3]={1,2,3,4,5,6,7,8,9};
    > printf("%u %u %u",a[0],a[1],a[2]);
    > }
    > when i execute this program i am getting a Fixed address Value(multi
    > digit value)
    > can any body explain me ahy this is giving the same value for
    > different index


    I doubt that that's true, but on the off-chance that it is, try this
    _legal_ C program and see what you get:

    #include <stdio.h>

    int main(void)
    {
    int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    printf("%p %p %p\n", (void *) a[0], (void *) a[1], (void *) a[2]);
    return 0;
    }
     
    Martin Ambuhl, Feb 20, 2008
    #9
  10. Martin Ambuhl <> writes:
    > wrote:
    >> hi all i have simple Problem please tell me the Solution if u know??
    >> main()
    >> {
    >> int a[3][3]={1,2,3,4,5,6,7,8,9};
    >> printf("%u %u %u",a[0],a[1],a[2]);
    >> }
    >> when i execute this program i am getting a Fixed address Value(multi
    >> digit value)
    >> can any body explain me ahy this is giving the same value for
    >> different index

    >
    > I doubt that that's true, but on the off-chance that it is, try this
    > _legal_ C program and see what you get:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    > printf("%p %p %p\n", (void *) a[0], (void *) a[1], (void *) a[2]);
    > return 0;
    > }


    Getting the same output three times in the OP's program isn't entirely
    implausible. If, for example, unsigned int is 32 bits and pointers
    are 64 bits, the "%u" format might be causing printf to print just the
    "high-order" 32 bits of each pointer value, which is very likely to be
    the same for all three. (I put the phrase "high-order" in quotation
    marks because C doesn't define any such concept for pointers.)

    On the other hand, the byte ordering of the x86 systems that are all
    the rage these days is such that you'd probably get the "low-order"
    bits of the pointer values, which would almost certainly differ. But
    it could also depend on the vagaries of the parameter passing
    mechanisms used by the particular compiler.

    Of course, the real problems are that (a) the program exhibits
    undefined behavior, and the output may not be entirely meaningless,
    but it's entirely system-specific and the C standard says nothing
    about what the program should do, and (b) the original poster didn't
    show us his actual output (perhaps he didn't really get the same
    result each time). In principle, *any* output is possible, including
    what the OP said he saw.

    I'd use "%p\n%p\n%p\n" rather than "%p %p %p\n"; it makes it easier to
    compare the successive outputs visually.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 20, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Wouters

    array prob

    Tom Wouters, Sep 28, 2003, in forum: Java
    Replies:
    15
    Views:
    709
    Herman Timmermans
    Sep 29, 2003
  2. Replies:
    4
    Views:
    541
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  4. Henry

    Newbie Multi-Dimensional Array Prob

    Henry, Aug 29, 2003, in forum: C Programming
    Replies:
    9
    Views:
    355
    Henry
    Aug 30, 2003
  5. Replies:
    1
    Views:
    111
    Randy Webb
    Mar 28, 2005
Loading...

Share This Page