473,378 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.
Mar 16 '06 #1
17 1929
gk245 <to*****@mail.com> 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
Mar 16 '06 #2
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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?
Mar 16 '06 #3
gk245 <to*****@mail.com> 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 1:putchar(a[i&15]);break;}}}
Mar 16 '06 #4
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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. :(
Mar 16 '06 #5

gk245 wrote:
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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

Mar 16 '06 #6
gk245 wrote:

Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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:Th*************@gmail.com>

Mar 16 '06 #7
gk245 <to*****@mail.com> writes:
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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
Mar 16 '06 #8
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;
}
Mar 16 '06 #9
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.
Mar 16 '06 #10
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.
Mar 16 '06 #11
gk245 wrote:
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Mar 16 '06 #12
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]));
}

Mar 17 '06 #13
CBFalconer <cb********@yahoo.com> wrote:
gk245 wrote:
Ben Pfaff wrote on 3/16/2006 :
gk245 <to*****@mail.com> 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
Mar 17 '06 #14
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.


Mar 17 '06 #15
"Robben" <de***********@hotmail.com> 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) ks***@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.
Mar 17 '06 #16
On 17 Mar 2006 03:07:48 -0800, "Robben" <de***********@hotmail.com>
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
Mar 18 '06 #17
On Thu, 16 Mar 2006 11:36:22 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
gk245 <to*****@mail.com> 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
Mar 27 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rafal Konopka | last post by:
How can you caount the number of occurrences of elements in an array? For example var arr1 = ; how can I count how many 1s, 2s, 3s, etc.? Despite the example, I never actually know what...
3
by: Zb Bornemann | last post by:
Hi All, Ok. Here's what my table looks like: Field1 Field2 Field3 Field4... Record1 1 3 2 9 Record2 9 1 9 ...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
9
by: Akhenaten | last post by:
The following snippet (for whatever reason) returns no value for the count. Suggestions? $arr = array ("A", "B", "C", "D", "E"); foreach ($arr as $client) { $count = mysql_query('SELECT...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
5
by: Larry | last post by:
Dear all, I'm new to Python. I have a file (an image file actually) that I need to read pixel by pixel. It's an 8-bit integer type. I need to get the statistics like mean, standard deviation,...
12
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than...
4
by: clrockwell | last post by:
Hey all, I could use some help with solving problem. Any suggestions are welcome and very appreciated. I have two arrays, $feeds and $feedDays. If they both have the same count, or $feeds...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.