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

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 6797
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
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
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
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
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
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
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
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
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
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
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.