473,407 Members | 2,598 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,407 software developers and data experts.

arrays

Is there a function that will tell me the lenght of an array
ie
string array[]={"a", "b", "c", "d", "e"};
and array.size(); would return 5
Sep 28 '05 #1
10 3247
Greg wrote:
Is there a function that will tell me the lenght of an array
ie
string array[]={"a", "b", "c", "d", "e"};
and array.size(); would return 5


Arrays are not objects, so they don't have members, so you can't use
the dot operator. You could, however, use

template<class T, size_t s> size_t size_of(T (&a)[s]) { return s; }
...
string array[]={"a", "b", "c", "d", "e"};
size_t s = size_of(array); // 's' should be 5

Or you could try

size_t s = sizeof(array)/sizeof(array[0]);

Note, that it won't work with pointers.

V
Sep 28 '05 #2
>string array[]={"a", "b", "c", "d", "e"};
size_t s = size_of(array); // 's' should be 5
This method only works if each member within array is same size.
Ex.: This wouldn't work if you declare array as
string array[]={"a", "b1", "c22", "d", "e"};

I suggest you use array of pointers so you will get identical 4byte size
of it.
Let me write it down.

char* array[] = {"a", "bc", "df" };
int count = sizeof(array)/sizeof(char*);

This will return you true count.
Victor Bazarov wrote: Greg wrote:
Is there a function that will tell me the lenght of an array
ie
string array[]={"a", "b", "c", "d", "e"};
and array.size(); would return 5

Arrays are not objects, so they don't have members, so you can't use
the dot operator. You could, however, use

template<class T, size_t s> size_t size_of(T (&a)[s]) { return s; }
...
string array[]={"a", "b", "c", "d", "e"};
size_t s = size_of(array); // 's' should be 5

Or you could try

size_t s = sizeof(array)/sizeof(array[0]);

Note, that it won't work with pointers.

V

Sep 28 '05 #3
* Mufe:
[top-posting, possibly trolling]
Don't top-post in this group, please. See the FAQ.
* Mufe:
>string array[]={"a", "b", "c", "d", "e"};
>size_t s = size_of(array); // 's' should be 5


This method only works if each member within array is same size.


That's incorrect.

There are other limitations, though.

Ex.: This wouldn't work if you declare array as
string array[]={"a", "b1", "c22", "d", "e"};
That's incorrect.

I suggest you use array of pointers so you will get identical 4byte size
of it.


That's incorrect.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 28 '05 #4
Alf P. Steinbach wrote:
>string array[]={"a", "b", "c", "d", "e"};
>size_t s = size_of(array); // 's' should be 5


This method only works if each member within array is same size.

That's incorrect.


It's correct, but it's trivial: all members of an array are the same
type, hence they are all the same size.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Sep 28 '05 #5
* Pete Becker:
Alf P. Steinbach wrote:
>string array[]={"a", "b", "c", "d", "e"};
>size_t s = size_of(array); // 's' should be 5

This method only works if each member within array is same size.


That's incorrect.


It's correct, but it's trivial: all members of an array are the same
type, hence they are all the same size.


"Mufe" gave an example of what he meant, and that was inconsistent with the
meaning you read into his words. "Mufe"'s statement was incorrect. And so
your statement is also incorrect when referring to that meaning; out of
context, on the other hand, your statement is trivially true.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 28 '05 #6
Alf P. Steinbach wrote:
* Pete Becker:
Alf P. Steinbach wrote:
>string array[]={"a", "b", "c", "d", "e"};
>size_t s = size_of(array); // 's' should be 5

This method only works if each member within array is same size.

That's incorrect.


It's correct, but it's trivial: all members of an array are the same
type, hence they are all the same size.

"Mufe" gave an example of what he meant, and that was inconsistent with the
meaning you read into his words. "Mufe"'s statement was incorrect. And so
your statement is also incorrect when referring to that meaning; out of
context, on the other hand, your statement is trivially true.


My mistake. I forgot that your goal is usually to obscure rather than to
clarify.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Sep 28 '05 #7
* Pete Becker:
Alf P. Steinbach wrote:
* Pete Becker:
Alf P. Steinbach wrote:

>>string array[]={"a", "b", "c", "d", "e"};
>>size_t s = size_of(array); // 's' should be 5
>
>This method only works if each member within array is same size.

That's incorrect.

It's correct, but it's trivial: all members of an array are the same
type, hence they are all the same size.


"Mufe" gave an example of what he meant, and that was inconsistent with the
meaning you read into his words. "Mufe"'s statement was incorrect. And so
your statement is also incorrect when referring to that meaning; out of
context, on the other hand, your statement is trivially true.


My mistake. I forgot that your goal is usually to obscure rather than to
clarify.


Yeah, I'm such a bastard that just by mentioning some -- any -- aspect of
my imagined personality you've offloaded so much Bad Karma that what's left is
so greatly improved that what you wrote earlier magically becomes correct.

Anyway, the quoting in your posting is rather incomprehensible.

Did you attempt to fix it manually?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 28 '05 #8

"Mufe" <mu**@nospam.net> wrote in message news:43********@news.s5.net...
string array[]={"a", "b", "c", "d", "e"};
size_t s = size_of(array); // 's' should be 5


This method only works if each member within array is same size.
Ex.: This wouldn't work if you declare array as
string array[]={"a", "b1", "c22", "d", "e"};


Array elements are ALWAYS the same size. You can't have 2 different size
elements in the same array.

I.E.
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::string MyString1 = "Testing";
std::string MyString2 = "X";
std::cout << sizeof( MyString1 ) << ":" << sizeof ( MyString2 ) <<
std::endl;
char x;
std::cin >> x;
return 0;
}

output in Microsoft Visual C++ .net 2003 is
28:28

All strings in MS VC++ .net 2003 then would be 28 bytes. Their data can be
any length, but is stored elsewhere, not in the object itself.
Sep 29 '05 #9
"Greg" <NULL> wrote:
Is there a function that will tell me the lenght of an array
ie
string array[]={"a", "b", "c", "d", "e"};
and array.size(); would return 5


Is there something in your code which forces you to use arrays instead of,
for example, a std::vector ? The vector container has that covered and it
also has many more methods which will help you a lot.
Rui Maciel
--
Running Kubuntu 5.04 with KDE 3.4.2 and proud of it.
jabber:ru********@jabber.org
Sep 29 '05 #10
"Greg" <NULL> wrote:
Is there a function that will tell me the lenght of an array
ie
string array[]={"a", "b", "c", "d", "e"};
and array.size(); would return 5


Is there something in your code which forces you to use arrays instead of,
for example, a std::vector ? The vector container has that covered and it
also has many more methods which will help you a lot.
Rui Maciel
--
Running Kubuntu 5.04 with KDE 3.4.2 and proud of it.
jabber:ru********@jabber.org
Sep 30 '05 #11

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.