2 D Array Prob Please Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sachinv1821@gmail.com

    2 D Array Prob Please Help

    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
  • santosh

    #2
    Re: 2 D Array Prob Please Help

    sachinv1821@gma il.com 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;
    }

    Comment

    • Richard Heathfield

      #3
      Re: 2 D Array Prob Please Help

      sachinv1821@gma il.com 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

      Comment

      • Bartc

        #4
        Re: 2 D Array Prob Please Help

        santosh wrote:
        sachinv1821@gma il.com 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



        Comment

        • Bartc

          #5
          Re: 2 D Array Prob Please Help

          Richard Heathfield wrote:
          sachinv1821@gma il.com 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


          Comment

          • Richard Heathfield

            #6
            Re: 2 D Array Prob Please Help

            Bartc said:
            Richard Heathfield wrote:
            >sachinv1821@gma il.com 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

            Comment

            • Bartc

              #7
              Re: 2 D Array Prob Please Help


              "Richard Heathfield" <rjh@see.sig.in validwrote in message
              news:yuKdnVyTO7 MhjSHanZ2dnUVZ8 uednZ2d@bt.com. ..
              Bartc said:
              >
              >Richard Heathfield wrote:
              >>sachinv1821@gma il.com 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


              Comment

              • Richard Heathfield

                #8
                Re: 2 D Array Prob Please Help

                Bartc said:
                >
                "Richard Heathfield" <rjh@see.sig.in validwrote in message
                news:yuKdnVyTO7 MhjSHanZ2dnUVZ8 uednZ2d@bt.com. ..
                >Bartc said:
                >>
                >>Richard Heathfield wrote:
                >>>sachinv1821@gma il.com 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

                Comment

                • Martin Ambuhl

                  #9
                  Re: 2 D Array Prob Please Help

                  sachinv1821@gma il.com 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;
                  }

                  Comment

                  Working...