Size of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kreagan
    New Member
    • Aug 2007
    • 153

    Size of an array

    I know one method of finding the size of an array is:

    [CODE=c]int x[5] = {0};
    (sizeof (x) / sizeof *(x) )[/CODE]

    But is there better looking way? For example, java can use array.length() or array.length. Is there anything like that in C?
  • minkkp
    New Member
    • Sep 2007
    • 3

    #2
    Originally posted by kreagan
    I know one method of finding the size of an array is:

    [CODE=c]int x[5] = {0};
    (sizeof (x) / sizeof *(x) )[/CODE]

    But is there better looking way? For example, java can use array.length() or array.length. Is there anything like that in C?

    in C.....say your string is lastName...i think you would enter lastName.Length
    in C....
    in c++ in think you would enter: cout <<lastName.leng th()<<endl;

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by minkkp
      in C.....say your string is lastName...i think you would enter lastName.Length
      in C....
      in c++ in think you would enter: cout <<lastName.leng th()<<endl;
      C doesn't have string class.
      And kreagen wonts to know for every type of array,not only of char type.

      Kreagen,I'm not sure that there is another way.

      Any specific reason to ask?


      Savage

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by Savage
        C doesn't have string class.
        And kreagen wonts to know for every type of array,not only of char type.

        Kreagen,I'm not sure that there is another way.

        Any specific reason to ask?
        Savage
        Ah, I thought so. I come from a java background. So when I write for loops, I always write it like:

        [CODE=c]for ( i = 0; i < sizeof(x); i++)[/CODE]

        Then I get that annoying "array out of bounds" fault. Old habits are hard to break. Plus

        [CODE=c](sizeof (x) / sizeof *(x) )[/CODE]

        just looks really ugly to me.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I simply pass an additional int value to any function that uses the array, and use that to indicate the size. Alternatively, you could write a function to determine the size of any array passed to it, and use that. But I agree, Java is much nicer in allowing array.length to be used.

          Comment

          • kreagan
            New Member
            • Aug 2007
            • 153

            #6
            Originally posted by Ganon11
            I simply pass an additional int value to any function that uses the array, and use that to indicate the size. Alternatively, you could write a function to determine the size of any array passed to it, and use that. But I agree, Java is much nicer in allowing array.length to be used.
            Oooh! I can use a macro instead of a function! Thanks for the idea!

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by kreagan
              Oooh! I can use a macro instead of a function! Thanks for the idea!
              Bad idea.

              The number of elements in an array is determined by the number in the first index:
              [code=c]
              int array[5]; //5 elements
              int array[5][6]; //5 elements
              int array[5][6][7]; //5 elements
              [/code]

              The other indexes describe the elements. You will always be better off using a variable for the first index and then passing that variable and the array address to your functions.

              The trick:
              Originally posted by kreagan
              (sizeof (x) / sizeof *(x) )
              does not work. The number of elements is the size of the array divided by the size of element 0 and not the size of the type stored in the array.

              This is correct:
              [code=c]
              (sizeof (x) / sizeof (x[0]);
              [/code]

              And be aware that when this array is passed to a function, only the address is passed so sizeof(x) becomes the size of the address and you get 4.

              Stick with a variable containing the number of elements.

              Comment

              Working...