Home > Archive > C > March 2004 > find the length of an array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
find the length of an array
|
|
|
| i want to confirm there is no C function to find the length
of an array.
For example,
int a[] = {1,2,3,...}
So we need to go through the array to determine the number of
elements in the array?
| |
| Ben Pfaff 2004-03-29, 12:30 am |
| jrefactors@hotmail.com (Matt) writes:
> i want to confirm there is no C function to find the length
> of an array.
>
> For example,
>
> int a[] = {1,2,3,...}
The length of an array a[] is sizeof a / sizeof *a. However,
function parameters declared as "arrays" are actually passed as
pointers, so this won't work for function parameters.
> So we need to go through the array to determine the number of
> elements in the array?
You can't even do that unless the array has some kind of sentinel
at the end.
--
"...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
in the uncertainty principle. Which of course makes the above equivalent to
Schrodinger's pointer..."
--Anthony McDonald
| |
| Jon Willeke 2004-03-29, 12:30 am |
| Matt wrote:
> i want to confirm there is no C function to find the length
> of an array.
>
> For example,
>
> int a[] = {1,2,3,...}
>
> So we need to go through the array to determine the number of
> elements in the array?
If a is in scope as an array, then its length is
sizeof a / sizeof a[0]
However, an array name in an expression or function parameter is treated
as a pointer. See section 6 of the FAQ:
<http://www.eskimo.com/~scs/C-faq/s6.html>
See _Expert C Programming_ for a tutorial discussion of this topic.
| |
| Barry Schwarz 2004-03-29, 12:30 am |
| On 28 Mar 2004 19:50:36 -0800, jrefactors@hotmail.com (Matt) wrote:
>i want to confirm there is no C function to find the length
>of an array.
>
>For example,
>
>int a[] = {1,2,3,...}
>
>So we need to go through the array to determine the number of
>elements in the array?
If the array is in scope, then
sizeof array_name
will evaluate to the number bytes consumed by the array and
sizeof array_name / sizeof *array_name
will evaluate to the number of elements in the array.
sizeof is not a function but an operator, just like + and -.
If the array is a parameter, then it is passed via a pointer and the
receiving function cannot determine the size without some additional
help. The two common forms of this additional help are using a
sentinel value in the array and passing a second argument containing
the dimension.
Stepping through the array to determine the number of elements is only
an option if the last used element contains a sentinel value, similar
to a string which is an array of char with a terminal '\0'. However,
this does not determine the size of the array, only the number of
elements currently in use. For example:
char c[20] = "ab";
defines an array of 20 char but only three are in use. Similarly, if
-1 is the sentinel for an array of int:
int i[12] = {0,1,2,3,4,5,6,7,8,-1};
defines an array of 12 int but only 10 are in use.
<<Remove the del for email>>
| |
| Dean Edis 2004-03-29, 3:30 am |
| Hi Matt,
There is no C function which will explicitly return the length of an
array. (Arguably strlen() is the exception, but that'll only work for
byte arrays)
If you have an array of pointers, one solution is to ensure it is NULL
terminated.
In the example you mention below you could use something like this:
int nElements = sizeof(a) / sizeof(a[0]);
where sizeof(a) will return the size (in bytes) of the entire array,
and sizeof(a[0]) will return the size of the first item.
Hope that helps !
-Dean
jrefactors@hotmail.com (Matt) wrote in message news:<ba8a039e.0403281950.34e71935@posting.google.com>...
> i want to confirm there is no C function to find the length
> of an array.
>
> For example,
>
> int a[] = {1,2,3,...}
>
> So we need to go through the array to determine the number of
> elements in the array?
| |
| Martin Dickopp 2004-03-29, 3:30 am |
| dean.edis@globalgraphics.com (Dean Edis) writes:
> jrefactors@hotmail.com (Matt) wrote in message news:<ba8a039e.0403281950.34e71935@posting.google.com>...
>
> There is no C function which will explicitly return the length of an
> array. (Arguably strlen() is the exception, but that'll only work for
> byte arrays)
Please don't top-post. Fixed.
`strlen' does not return the length of a byte array. Consider the
following byte array of length 3:
char c[] = {4, 0, 2};
`strlen (c)' would return 1.
Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
| |
| Mike Wahler 2004-03-29, 1:30 pm |
|
"Matt" <jrefactors@hotmail.com> wrote in message
news:ba8a039e.0403281950.34e71935@posting.google.com...
> i want to confirm there is no C function to find the length
> of an array.
>
> For example,
>
> int a[] = {1,2,3,...}
>
> So we need to go through the array to determine the number of
> elements in the array?
size_t array_size = sizoef a / sizeof *a;
Array definition must be in scope (e.g.. as
a function parameter, the array is converted
to a pointer to its first element, and 'sizeof'
would return the pointer's size).
-Mike
| |
|
|
| Neil Cerutti 2004-03-29, 5:30 pm |
| In article <rarg60l9ncirp2s7cnrr8eqji96a7ihc2r@4ax.com>, Irrwahn Grausewitz wrote:
> "Mike Wahler" <mkwahler@mkwahler.net> wrote:
><snip>
> ^^^^^^
> ITYM sozeif ;)
No, clearly he means zefisvo.
--
Neil Cerutti
"Captain, every day I grow more and more convinced that the
wisest and the best is to fix your eyes on the good and the beautiful."
"You're some guy McKonan." -_The Phantom Planet_
|
|
|
|
|