473,386 Members | 1,752 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,386 software developers and data experts.

looking for a pre-built function to compute the num of elements in an array

momotaro
357 100+
N.B my array is of a user defined type containing multiple fields

thx
May 7 '09 #1
3 1251
Banfa
9,065 Expert Mod 8TB
There isn't one in C. A macro I have seen used quite a lot is

Expand|Select|Wrap|Line Numbers
  1. #define ARRAY_LEN(a)  ((sizeof (a))/(sizeof (a)[0]))
  2.  
But that only works on actual arrays not pointers to arrays.

Of course if you are using C++ you should not be using an array but a vector which would give the result through the size() member function.
May 7 '09 #2
scruggsy
147 100+
Assuming the array is not dynamically allocated, use the sizeof operator.
Expand|Select|Wrap|Line Numbers
  1. struct Thing
  2. {
  3.   int a;
  4.   int b;
  5. };
  6.  
  7. Thing things[5];
Assuming a Thing occupies 8 bytes, sizeof(Thing) is 8.
sizeof(things) is the size of each element times the number of elements: 5 * 8.
You can't pass the array to a function and use sizeof on it. You can however use a macro.
Expand|Select|Wrap|Line Numbers
  1. #define ARR_SIZE(ARR, ELEM) (sizeof( (ARR) ) / (sizeof( ( ELEM ) ) ) )
  2.  
May 7 '09 #3
RRick
463 Expert 256MB
I'm with Banfa, you should be using a STL vector.

The main reason is that dynamically allocated data is the norm, not the exception. With that data, there is no runtime way to calculate the size unless you yourself have defined some last element delimiter. In a char *, this is the null char, and usually in arrays, a null pointer follows the last valid pointer.

Its easier to let the vector object deal with these issues.
May 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: BlackHawke | last post by:
Hello! My name is Nick Soutter, I am the owner of a small game company in San Diego, CA called Aepox Games (www.aepoxgames.net). Our first product, Andromeda Online...
9
by: crispy | last post by:
I am looking for a free (or inexpensive) script to list the book and journal publications created within a college department and be able to sort them by author and year of publication. I've...
38
by: Jukka K. Korpela | last post by:
As well all know, valid markup is important... but when trying to find a convincing modern argument in favor of this, I found pages like http://www.htmlhelp.com/tools/validator/reasons.html which...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
3
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
8
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
1
by: Jason Walsh | last post by:
I'm a pre-university student with about 7 months Python experience looking for an open source project to get involved in (learn the code, do bug fixes, etc). I've searched freshmeat.net,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.