Array length

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronny Mandal

    #1

    Array length

    I could not find anything about this in the FAQ, so:

    I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
    return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
    the array?


    Thanks.

    Ronny Mandal


  • Karthik Kumar

    #2
    Re: Array length

    Ronny Mandal wrote:[color=blue]
    > I could not find anything about this in the FAQ, so:
    >
    > I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
    > return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
    > the array?
    >
    >
    > Thanks.
    >
    > Ronny Mandal
    >
    >[/color]

    The answer is not possible. While dealing with 2-D arrays like that,
    generally people tend to encapsulate them in a struct with the
    dimensions in it and return a variable of that struct.

    Comment

    • Michael Mair

      #3
      Re: Array length

      Ronny Mandal wrote:[color=blue]
      > I could not find anything about this in the FAQ, so:
      >
      > I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
      > return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
      > the array?[/color]

      If you are really talking about arrays of arrays of "type", consider:


      #include <stdio.h>

      int main (void)
      {
      int a[2][3];

      printf("a[2][*]: %lu\na[*][3]: %lu\na[*][*]: %lu\n",
      (unsigned long) (sizeof a[0]/sizeof a[0][0]),
      (unsigned long) (sizeof a /sizeof a[0]),
      (unsigned long) (sizeof a /sizeof a[0][0]));

      return 0;
      }

      Note: With a C99 library, you can use the length modifier
      z instead of l in the printf format string and do no longer
      need the cast.

      Apart from that, the FAQ section about arrays and pointers
      may be helpful for you. Start from



      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • Erik de Castro Lopo

        #4
        Re: Array length

        Ronny Mandal wrote:[color=blue]
        >
        > I could not find anything about this in the FAQ, so:
        >
        > I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
        > return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
        > the array?[/color]

        #define ARRAY_LEN(x) (sizeof(x) / sizeof ((x) [0]))

        which can then be used as:

        len1 = ARRAY_LEN (foo) ;
        len2 = ARRAY_LEN (foo [0])

        Erik
        --
        +-----------------------------------------------------------+
        Erik de Castro Lopo nospam@mega-nerd.com (Yes it's valid)
        +-----------------------------------------------------------+
        Open Source and Free Software means that you never sacrifice quality
        of the code for meeting deadlines set up by people not participating
        directly in the software development process.

        Comment

        • Lawrence Kirby

          #5
          Re: Array length

          On Wed, 22 Dec 2004 18:14:03 +1100, Erik de Castro Lopo wrote:
          [color=blue]
          > Ronny Mandal wrote:[color=green]
          >>
          >> I could not find anything about this in the FAQ, so:
          >>
          >> I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
          >> return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
          >> the array?[/color]
          >
          > #define ARRAY_LEN(x) (sizeof(x) / sizeof ((x) [0]))
          >
          > which can then be used as:
          >
          > len1 = ARRAY_LEN (foo) ;
          > len2 = ARRAY_LEN (foo [0])[/color]

          But note that this only works when foo is the name of an array with a full
          definition in scope. It doesn't work if foo is a pointer, at least at the
          top level. Specifically this mechanism gets information from the TYPE of
          foo, it doesn't look at the size of actual runtime objects (even with C99
          VLAs it is still effectively a type thing). So given

          int array[4][5];
          int (*ptr)[5] = array;

          I can use ARRAY_LEN(array ) to get 4, ARRAY_LEN(array[0]) to get 5 and
          ARRAY_LEN(ptr[0]) to get 5, because this is all information that is in the
          types of array and ptr. However ARRAY_LEN(ptr) is nonsense because ptr
          itself is a pointer not an array and provides no top level size
          information (array, array[0] and ptr[0] are expressions with array type).
          sizeof(ptr) provides information about the pointer, not the array it
          points into.

          C provides no way to get the number of elements of the top level array
          from a pointer. If you need that information you have to deal with
          that yourself, e.g. by recording it and passing it around.

          Also be careful of function parameters:

          void bar(int array[4][5])
          {
          size_t asize = ARRAY_LEN(array ); /* Wrong */
          size_t bsize = ARRAY_LEN(*arra y); /* Good */
          }

          Despite appearances array here is not an array, it is a pointer with type
          int (*)[5]. So the ARRAY_LEN(array ) expression is invalid as in the case
          of ptr above.

          Lawrence

          Comment

          • Joe Wright

            #6
            Re: Array length

            Ronny Mandal wrote:[color=blue]
            > I could not find anything about this in the FAQ, so:
            >
            > I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
            > return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
            > the array?
            >
            >
            > Thanks.
            >
            > Ronny Mandal
            >
            >[/color]
            #include <stdio.h>

            #define R 4
            #define C 5

            int main(void) {
            char arr[R][C];
            printf("Size of arr is %2d\n"
            "Size of arr[0] is %2d\n"
            "Size of arr[0][0] is %2d\n",
            (int)sizeof arr,
            (int)sizeof arr[0],
            (int)sizeof arr[0][0]);
            return 0;
            }


            --
            Joe Wright mailto:joewwrig ht@comcast.net
            "Everything should be made as simple as possible, but not simpler."
            --- Albert Einstein ---

            Comment

            • Barry Schwarz

              #7
              Re: Array length

              On Tue, 21 Dec 2004 23:34:58 +0100, "Ronny Mandal"
              <ronnyma@math.u io.no> wrote:
              [color=blue]
              >I could not find anything about this in the FAQ, so:
              >
              >I have a two-dim array, foo[a][b] (a and b are arbitrary). How do I get C to
              >return the length of e.g length( foo[a] ) = 4, i.e the number of x and y in
              >the array?
              >
              >[/color]
              You can get the value of a particular dimension by dividing the size
              of the array without that dimension by the size of the array element.

              In your example

              sizeof foo / sizeof foo[0] will evaluate to a. Instead of foo[0]
              you can use *foo.

              sizeof foo[0] / sizeof *foo[0] will evaluate to b.

              This will work regardless of the number of dimensions.


              <<Remove the del for email>>

              Comment

              Working...