Velocity Reviews > array size

# array size

No Spam
Guest
Posts: n/a

 10-24-2003
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

Mike Wahler
Guest
Posts: n/a

 10-24-2003
"No Spam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a

 10-24-2003
Mike Wahler <(E-Mail Removed)> wrote:
> "No Spam" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
--
_ _____ _____
| ||_ _||_ _| http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring

Allin Cottrell
Guest
Posts: n/a

 10-25-2003
Mike Wahler wrote:
> "No Spam" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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

Martijn
Guest
Posts: n/a

 11-02-2003
> 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

Irrwahn Grausewitz
Guest
Posts: n/a

 11-02-2003
"Martijn" <(E-Mail Removed)> 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
((E-Mail Removed))