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

G

gk245

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.
 
B

Ben Pfaff

gk245 said:
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.)
 
G

gk245

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

David Resnick

gk245 said:
Ben Pfaff wrote on 3/16/2006 :

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
 
K

Kenneth Brody

gk245 said:
Ben Pfaff wrote on 3/16/2006 :

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:[email protected]>
 
B

Ben Pfaff

gk245 said:
Ben Pfaff wrote on 3/16/2006 :

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.
 
M

Martin Ambuhl

gk245 said:
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;
}
 
G

gk245

Martin Ambuhl wrote :
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.
 
C

CBFalconer

gk245 said:
Ben Pfaff wrote on 3/16/2006 :

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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
G

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]));
}
 
R

Richard Bos

CBFalconer said:
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
 
R

Robben

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.
 
K

Keith Thompson

Robben said:
gk245 said:
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.
 
B

Barry Schwarz

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.
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
 
D

Dave Thompson

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

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top