473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array size

In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained

Thanks
Ben
Jul 22 '05 #1
7 6803
BrianJones wrote:
In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained


Not for built in arrays. You must use standard library containers like
vector that have a size() member function. Also for regular applications
you should use standard library containers anyway and do not bother
yourself with low level stuff.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
BrianJones wrote:
In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained

If you have the definition of the array handy you can do this:
template <typename T, unsigned N>
inline unsigned Length( T (&x)[N] )
{
return N;
}

int Array[ 230 ];
extern int ExternArray[]; // don't know the length

#include <iostream>

int main()
{
std::cout << Length( Array ) << "\n";
std::cout << Length( ExternArray ) << "\n"; // error
}

If you pass around arrays and you need the length, then std::vector is
the way to go.
Jul 22 '05 #3
BrianJones wrote:
[...]
I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained


Perhaps you have the character '\0' inside your array of chars.
It servers as an end-of-string marker for strlen().

hth,
- J.
Jul 22 '05 #4
"BrianJones" <br***@jones1611.fsnet.co.uk> wrote in message news:<cc**********@news6.svr.pol.co.uk>...
In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained

Thanks
Ben


C style strings are terminated by a null character,'\0'. strlen
actually checks the position of this '\0'. If your array contains
no '\0',using strlen can actually cause your program to crash. As a
hack, you might consider placing a sentinel '\0' at the end of the array.
Then strlen will return the size of the array - 1. But you should really
consider using a vector.

-Arijit
Jul 22 '05 #5
"BrianJones" <br***@jones1611.fsnet.co.uk> wrote in message news:<cc**********@news6.svr.pol.co.uk>...
In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained

Thanks
Ben


C style strings are terminated by a null character,'\0'. strlen
actually checks the position of this '\0'. If your array contains
no '\0',using strlen can actually cause your program to crash. As a
hack, you might consider placing a sentinel '\0' at the end of the array.
Then strlen will return the size of the array - 1. But you should really
consider using a vector.

-Arijit
Jul 22 '05 #6

You should be using vector in those cases. Your program will work as per the
suggestions, but when you have store other objects, there wont be any way for
you to get the size unless you keep a track of the things put in the array.
Using vector is more "C++ way",compared to the "C" way you are doing.

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #7
BrianJones wrote:
In java, to work out the size of an array, say it was called a, you would do
a.length which might return 10 as the number of elements.

Is there a similar thing you can do in C++? I'd initially thought of using
strlen(a) since the array I am working with is of type char. However, I
found this to return a value drastically smaller than the number of elements
it actually contained

Thanks
Ben


Here are some general guidelines:
1. When working with a collection of characters,
prefer std::string to char * or char [].

2. Prefer std::vector to an array.

3. Length of a string is different than the length
of an array (or the capacity of the container).
I could have the string "Hello" in a container
that has a capacity of 64. The length of the
string is much smaller than the capacity of
the container.

4. The language does not know the length of an
array that a pointer points to.

5. When storing strings to a file, decide whether
you want to specify length then text or text
terminated by a sentinel value.

6. Read the FAQ. Understand the FAQ. Read the
FAQ for the C language as well.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #8

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

Similar topics

13
34341
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
58
10046
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
2456
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
8
4591
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
6
4116
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
22
2425
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
7
7373
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
12
112069
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
20
2294
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
28
4542
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
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
7328
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...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.