Velocity Reviews > How do I get a count of the values in a array?

How do I get a count of the values in a array?

gk245
Guest
Posts: n/a

 03-16-2006
For instance, if i had a array like this:

array[] = {1, 2, 3, 5, 6, ......};

How do i tell C go get me the number of values in the array? Not the
value itself, but how MANY of them there are.

Thanks, please don't give me anything fancy. Since i can't use other
functions to give that answer, just for loops and such.

Ben Pfaff
Guest
Posts: n/a

 03-16-2006
gk245 <(E-Mail Removed)> writes:

> For instance, if i had a array like this:
>
> array[] = {1, 2, 3, 5, 6, ......};
>
> How do i tell C go get me the number of values in the array? Not the
> value itself, but how MANY of them there are.

sizeof array / sizeof *array

(This will work only for an actual array, not for a pointer into
an array.)
--
"...deficient support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup

gk245
Guest
Posts: n/a

 03-16-2006
Ben Pfaff wrote on 3/16/2006 :
> gk245 <(E-Mail Removed)> writes:
>
>> For instance, if i had a array like this:
>>
>> array[] = {1, 2, 3, 5, 6, ......};
>>
>> How do i tell C go get me the number of values in the array? Not the
>> value itself, but how MANY of them there are.

>
> sizeof array / sizeof *array
>
> (This will work only for an actual array, not for a pointer into
> an array.)

Thx, but like i said, i can't use a function to do this. sizeof is a
function, right?

Ben Pfaff
Guest
Posts: n/a

 03-16-2006
gk245 <(E-Mail Removed)> writes:

> sizeof is a function, right?

No. sizeof is an operator.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

gk245
Guest
Posts: n/a

 03-16-2006
Ben Pfaff wrote on 3/16/2006 :
> gk245 <(E-Mail Removed)> writes:
>
>> sizeof is a function, right?

>
> No. sizeof is an operator.

lol, sorry. Yeah, unfortunately can't use that either. Haven't
covered it yet.

David Resnick
Guest
Posts: n/a

 03-16-2006

gk245 wrote:
> Ben Pfaff wrote on 3/16/2006 :
> > gk245 <(E-Mail Removed)> writes:
> >
> >> sizeof is a function, right?

> >
> > No. sizeof is an operator.

>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet.

Put a sentinel value at the end that isn't a legal value. For example,
if your
array can only contain non-negative integers, put -1 at the end. If it
can
only contain non-null pointers, put NULL at the end. etc.

-David

Kenneth Brody
Guest
Posts: n/a

 03-16-2006
gk245 wrote:
>
> Ben Pfaff wrote on 3/16/2006 :
> > gk245 <(E-Mail Removed)> writes:
> >
> >> sizeof is a function, right?

> >
> > No. sizeof is an operator.

>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet.

Translation:

This is a homework assignment, and we aren't allowed to use anything
we haven't covered yet in class.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

Ben Pfaff
Guest
Posts: n/a

 03-16-2006
gk245 <(E-Mail Removed)> writes:

> Ben Pfaff wrote on 3/16/2006 :
>> gk245 <(E-Mail Removed)> writes:
>>
>>> sizeof is a function, right?

>>
>> No. sizeof is an operator.

>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet.

Then use a macro:
#define ELEM_CNT 5
int array[ELEM_CNT] = {1, 2, 3, 4, 5};
Thereafter, just use ELEM_CNT as the number of elements in the
array.

--
--Richard Heathfield

Martin Ambuhl
Guest
Posts: n/a

 03-16-2006
gk245 wrote:
> For instance, if i had a array like this:
>
> array[] = {1, 2, 3, 5, 6, ......};
>
> How do i tell C go get me the number of values in the array? Not the
> value itself, but how MANY of them there are.

int main(void)
{
int array[] = {1, 2, 3, 5, 6};
size_t nelements = sizeof array / sizeof *array;
return 0;
}

Martin Ambuhl
Guest
Posts: n/a

 03-16-2006
gk245 wrote:

> Thx, but like i said, i can't use a function to do this. sizeof is a
> function, right?