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

Discussion in 'C Programming' started by gk245, Mar 16, 2006.

1. gk245Guest

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.

gk245, Mar 16, 2006

2. Ben PfaffGuest

gk245 <> 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

Ben Pfaff, Mar 16, 2006

3. gk245Guest

Ben Pfaff wrote on 3/16/2006 :
> gk245 <> 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?

gk245, Mar 16, 2006
4. Ben PfaffGuest

gk245 <> 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,0xa67f6aaa,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;}}}

Ben Pfaff, Mar 16, 2006
5. gk245Guest

Ben Pfaff wrote on 3/16/2006 :
> gk245 <> 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.

gk245, Mar 16, 2006
6. David ResnickGuest

gk245 wrote:
> Ben Pfaff wrote on 3/16/2006 :
> > gk245 <> 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

David Resnick, Mar 16, 2006
7. Kenneth BrodyGuest

gk245 wrote:
>
> Ben Pfaff wrote on 3/16/2006 :
> > gk245 <> 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: <mailto:>

Kenneth Brody, Mar 16, 2006
8. Ben PfaffGuest

gk245 <> writes:

> Ben Pfaff wrote on 3/16/2006 :
>> gk245 <> 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.

--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield

Ben Pfaff, Mar 16, 2006
9. Martin AmbuhlGuest

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, Mar 16, 2006
10. Martin AmbuhlGuest

gk245 wrote:

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

NO. Please open your damn C text. Start at page one.

Martin Ambuhl, Mar 16, 2006
11. gk245Guest

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

>
> NO. Please open your damn C text. Start at page one.

take it easy man. I had forgotten since there is quite a lot to learn
in the beginning.

gk245, Mar 16, 2006
12. CBFalconerGuest

gk245 wrote:
> Ben Pfaff wrote on 3/16/2006 :
>> gk245 <> 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 you have no way. So go ahead and use it. That's why it is in
the language.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

CBFalconer, Mar 16, 2006
13. =?utf-8?B?6Zuo6Iqx55+zYw==?=Guest

the numbers in an array is equal to:

sizeof(array)/sizeof(array[0])

for example:

main()
{
float array[]={3.14,-9.21,1.7e2,-99,0.12};

printf("%d\n",sizeof(array)/sizeof(array[0]));
}

=?utf-8?B?6Zuo6Iqx55+zYw==?=, Mar 17, 2006
14. Richard BosGuest

CBFalconer <> wrote:

> gk245 wrote:
> > Ben Pfaff wrote on 3/16/2006 :
> >> gk245 <> 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 you have no way.

Actually, I believe that since we already have a well-defined array to
apply this to, as well as well-defined members of this array, there is a
way to do this. It is, however, not something a beginning student should
even attempt, since it's likely to teach bad habits.

Richard

Richard Bos, Mar 17, 2006
15. RobbenGuest

I guess this could be an interview question to test your knowledge of
C. For Arrays in C there is no way to find the count of the number of
elements of an array without using sizeof operator.

If you use a for loop you need to know the size earlier as C allows to
access the elements beyond the maximum array length; and it can fall
into an infinite loop.

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.
>
> Thanks, please don't give me anything fancy. Since i can't use other
> functions to give that answer, just for loops and such.

Robben, Mar 17, 2006
16. Keith ThompsonGuest

"Robben" <> writes:
> 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.
>>
>> Thanks, please don't give me anything fancy. Since i can't use other
>> functions to give that answer, just for loops and such.

>
> I guess this could be an interview question to test your knowledge of
> C. For Arrays in C there is no way to find the count of the number of
> elements of an array without using sizeof operator.
>
> If you use a for loop you need to know the size earlier as C allows to
> access the elements beyond the maximum array length; and it can fall
> into an infinite loop.

Robben, please don't top-post. I've corrected it here.

Actually, there are ways to compute the size of an array without using
sizeof. They involve some ugly pointer arithmetic and casts to char*.
I'm not going to go into details because (a) the OP didn't want
anything fancy, and (b) there's no good reason not to use sizeof.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Mar 17, 2006
17. Barry SchwarzGuest

On 17 Mar 2006 03:07:48 -0800, "Robben" <>
wrote:

>I guess this could be an interview question to test your knowledge of
>C. For Arrays in C there is no way to find the count of the number of
>elements of an array without using sizeof operator.
>
>If you use a for loop you need to know the size earlier as C allows to
>access the elements beyond the maximum array length; and it can fall
>into an infinite loop.

While accessing an element beyond the maximum array length need not be
a syntax error, C does NOT allow it. Any attempt to do so invokes
undefined behavior and you have left the C universe for parts unknown.

>
>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.
>>
>> Thanks, please don't give me anything fancy. Since i can't use other
>> functions to give that answer, just for loops and such.

Remove del for email

Barry Schwarz, Mar 18, 2006
18. Dave ThompsonGuest

On Thu, 16 Mar 2006 11:36:22 -0800, Ben Pfaff <>
wrote:

> gk245 <> 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.)

Correct, but I think it is worth noting explicitly a consequence of
this that people (too) often miss:

And if you (try to) declare function's (formal) parameter as an array,
it is actually 'adjusted' to a pointer, and this method doesn't work.

- David.Thompson1 at worldnet.att.net

Dave Thompson, Mar 27, 2006