array size

G

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
 
M

Mike Wahler

No Spam said:
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
 
J

Jens.Toerring

#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.
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
A

Allin Cottrell

Mike said:
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;
}
 
M

Martijn

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 */
 
I

Irrwahn Grausewitz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top