array size

Discussion in 'C Programming' started by No Spam, Oct 24, 2003.

  1. No Spam

    No Spam Guest

    This is probably an easy question, but I have not used C for quite some
    time: How do I determine the size of an array?
    More specifically, how do I determine the number of incoming parameters?

    Thanks.
    T
     
    No Spam, Oct 24, 2003
    #1
    1. Advertising

  2. No Spam

    Mike Wahler Guest

    "No Spam" <> wrote in message
    news:...
    > This is probably an easy question, but I have not used C for quite some
    > time: How do I determine the size of an array?


    #include <stdio.h>

    int main()
    {
    int array[10] = {0};
    printf("%ul\n", (unsigned long)sizeof array);
    return 0;
    }


    > More specifically, how do I determine the number of incoming parameters?


    Arrays do not have parameters, but functions do. You can determine
    how many each has by counting them when you create them.

    -Mike
     
    Mike Wahler, Oct 24, 2003
    #2
    1. Advertising

  3. No Spam

    -berlin.de Guest

    Mike Wahler <> wrote:
    > "No Spam" <> wrote in message
    > news:...
    >> This is probably an easy question, but I have not used C for quite some
    >> time: How do I determine the size of an array?


    > #include <stdio.h>


    > int main()
    > {
    > int array[10] = {0};
    > printf("%ul\n", (unsigned long)sizeof array);
    > return 0;
    > }


    And if you don't want the size of the array (in bytes) but the number
    of its elements use

    sizeof array / sizeof array[0]

    Please note that this only works when array is a real array, not a
    pointer to an array that got e.g. passed to a function, if you need
    the size of an array within a function you must pass its size to the
    function beside the pointer to the array.

    >> More specifically, how do I determine the number of incoming parameters?


    > Arrays do not have parameters, but functions do. You can determine
    > how many each has by counting them when you create them.


    But if you should mean the number of command line arguments you need

    int main( int argc, char *argv[] )

    and then 'argc' ist the number of arguments (including the name the
    program was called with, which is argv[0]).

    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
     
    -berlin.de, Oct 25, 2003
    #3
  4. Mike Wahler wrote:
    > "No Spam" <> wrote in message
    > news:...
    >
    >>This is probably an easy question, but I have not used C for quite some
    >>time: How do I determine the size of an array?

    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int array[10] = {0};
    > printf("%ul\n", (unsigned long)sizeof array);
    > return 0;
    > }


    s/ul/lu/

    If the OP wants the number of elements in the array, rather than the
    size in bytes, a modification is needed:

    int main()
    {
    int array[10] = {0};
    printf("%lu\n", (unsigned long) (sizeof array / sizeof array[0]));
    return 0;
    }

    --
    Allin Cottrell
    Department of Economics
    Wake Forest University, NC
     
    Allin Cottrell, Oct 25, 2003
    #4
  5. No Spam

    Martijn Guest

    > This is probably an easy question, but I have not used C for quite
    > some time: How do I determine the size of an array?
    > More specifically, how do I determine the number of incoming
    > parameters?


    You usually specify an extra argument with a count or "terminate" the array
    somehow (take a special value that indicates the end of an array, like a
    NUL-terminated string does).

    If a specific variable is within scope and has been given a size at
    compile/declaration time, you may use the sizeof operator. eg:

    int i[10];
    int n;

    n = sizeof(i);
    /* n is now 10 */

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Nov 2, 2003
    #5
  6. "Martijn" <> wrote:

    <snip>
    > int i[10];
    > int n;
    >
    > n = sizeof(i);
    > /* n is now 10 */


    But only if sizeof (int) == 1 on a given implementation.

    n = sizeof i; /* n == size of array in bytes */

    n = sizeof i / sizeof *i; /* n == number of elements in array */

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Nov 2, 2003
    #6
    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. Zygmunt Krynicki
    Replies:
    1
    Views:
    642
    Ivan Vecerina
    Oct 11, 2003
  2. Denis Palas
    Replies:
    2
    Views:
    725
    Daniel Pitts
    Dec 19, 2006
  3. Replies:
    5
    Views:
    582
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    529
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,734
    Michael Jung
    May 25, 2008
Loading...

Share This Page