How do I get a count of the values in a array? | | |
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. | | | | re: How do I get a count of the values in a array?
gk245 <topsoil@mail.com> writes:
[color=blue]
> 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.[/color]
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 | | | | re: How do I get a count of the values in a array?
Ben Pfaff wrote on 3/16/2006 :[color=blue]
> gk245 <topsoil@mail.com> writes:
>[color=green]
>> 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.[/color]
>
> sizeof array / sizeof *array
>
> (This will work only for an actual array, not for a pointer into
> an array.)[/color]
Thx, but like i said, i can't use a function to do this. sizeof is a
function, right? | | | | re: How do I get a count of the values in a array?
gk245 <topsoil@mail.com> writes:
[color=blue]
> sizeof is a function, right?[/color]
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 1:putchar(a[i&15]);break;}}} | | | | re: How do I get a count of the values in a array?
Ben Pfaff wrote on 3/16/2006 :[color=blue]
> gk245 <topsoil@mail.com> writes:
>[color=green]
>> sizeof is a function, right?[/color]
>
> No. sizeof is an operator.[/color]
lol, sorry. Yeah, unfortunately can't use that either. Haven't
covered it yet. :( | | | | re: How do I get a count of the values in a array?
gk245 wrote:[color=blue]
> Ben Pfaff wrote on 3/16/2006 :[color=green]
> > gk245 <topsoil@mail.com> writes:
> >[color=darkred]
> >> sizeof is a function, right?[/color]
> >
> > No. sizeof is an operator.[/color]
>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet. :([/color]
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 | | | | re: How do I get a count of the values in a array?
gk245 wrote:[color=blue]
>
> Ben Pfaff wrote on 3/16/2006 :[color=green]
> > gk245 <topsoil@mail.com> writes:
> >[color=darkred]
> >> sizeof is a function, right?[/color]
> >
> > No. sizeof is an operator.[/color]
>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet. :([/color]
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:ThisIsASpamTrap@gmail.com> | | | | re: How do I get a count of the values in a array?
gk245 <topsoil@mail.com> writes:
[color=blue]
> Ben Pfaff wrote on 3/16/2006 :[color=green]
>> gk245 <topsoil@mail.com> writes:
>>[color=darkred]
>>> sizeof is a function, right?[/color]
>>
>> No. sizeof is an operator.[/color]
>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet. :([/color]
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 | | | | re: How do I get a count of the values in a array?
gk245 wrote:[color=blue]
> 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.[/color]
int main(void)
{
int array[] = {1, 2, 3, 5, 6};
size_t nelements = sizeof array / sizeof *array;
return 0;
} | | | | re: How do I get a count of the values in a array?
gk245 wrote:
[color=blue]
> Thx, but like i said, i can't use a function to do this. sizeof is a
> function, right?[/color]
NO. Please open your damn C text. Start at page one. | | | | re: How do I get a count of the values in a array?
Martin Ambuhl wrote :[color=blue]
> gk245 wrote:
>[color=green]
>> Thx, but like i said, i can't use a function to do this. sizeof is a
>> function, right?[/color]
>
> NO. Please open your damn C text. Start at page one.[/color]
take it easy man. I had forgotten since there is quite a lot to learn
in the beginning. | | | | re: How do I get a count of the values in a array?
gk245 wrote:[color=blue]
> Ben Pfaff wrote on 3/16/2006 :[color=green]
>> gk245 <topsoil@mail.com> writes:
>>[color=darkred]
>>> sizeof is a function, right?[/color]
>>
>> No. sizeof is an operator.[/color]
>
> lol, sorry. Yeah, unfortunately can't use that either. Haven't
> covered it yet. :([/color]
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/>
Also see <http://www.safalra.com/special/googlegroupsreply/> | | | | re: How do I get a count of the values in a array?
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]));
} | | | | re: How do I get a count of the values in a array?
CBFalconer <cbfalconer@yahoo.com> wrote:
[color=blue]
> gk245 wrote:[color=green]
> > Ben Pfaff wrote on 3/16/2006 :[color=darkred]
> >> gk245 <topsoil@mail.com> writes:
> >>
> >>> sizeof is a function, right?
> >>
> >> No. sizeof is an operator.[/color]
> >
> > lol, sorry. Yeah, unfortunately can't use that either. Haven't
> > covered it yet. :([/color]
>
> Then you have no way.[/color]
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 | | | | re: How do I get a count of the values in a array?
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:[color=blue]
> 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.[/color] | | | | re: How do I get a count of the values in a array?
"Robben" <devendermarri@hotmail.com> writes:[color=blue]
> gk245 wrote:[color=green]
>> 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.[/color]
>
> 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.[/color]
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) kst-u@mib.org <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. | | | | re: How do I get a count of the values in a array?
On 17 Mar 2006 03:07:48 -0800, "Robben" <devendermarri@hotmail.com>
wrote:
[color=blue]
>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.[/color]
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.
[color=blue]
>
>gk245 wrote:[color=green]
>> 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.[/color][/color]
Remove del for email | | | | re: How do I get a count of the values in a array?
On Thu, 16 Mar 2006 11:36:22 -0800, Ben Pfaff <blp@cs.stanford.edu>
wrote:
[color=blue]
> gk245 <topsoil@mail.com> writes:
>[color=green]
> > 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.[/color]
>
> sizeof array / sizeof *array
>
> (This will work only for an actual array, not for a pointer into
> an array.)[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,411 network members.
|