The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Determine the size of an array of strings
Discuss Determine the size of an array of strings in the C Programming forum on Dev Shed. Determine the size of an array of strings C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 17th, 2004, 05:58 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Determine the size of an array of strings
Suppose I have a string such as the one defined below. Is there a function that can determine how many items are located in the array?? Like java has A.length, does c++ have something like this?? Thanks
Code:
string A [] = {"bogus", "bogusness", "wacky", "weasel", "calypso", "hibernate"};
|

February 17th, 2004, 07:26 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
Code:
int arrayLength = ( sizeof( A ) / sizeof( string ) );
|

February 17th, 2004, 08:11 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
I replied to this post, then deleted it because I thought maybe I spoke too soon. If there's an actual string type of fixed length, I'm unaware of it, but I'm not current on a lot of things.
I don't think the sizeof thing will work unless all items are same size. I may learn something here :-)
|

February 17th, 2004, 08:22 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
I believe this has been covered here before just today.
The number of elements in any array of any type can be calculated like this:
Code:
sizeof(array) / sizeof(array[0])
for example, an array of strings
Code:
string A[] = { ... }
int nElements = sizeof(A) / sizeof(A[0]);
Note that the above for strings does NOT calculate the number of characters that the strings point to. It only tells you how many different strings are in the array.
|

February 17th, 2004, 08:26 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
Quote: | Originally Posted by Ancient Dragon I believe this has been covered here before just today.
The number of elements in any array of any type can be calculated like this:
Code:
sizeof(array) / sizeof(array[0])
for example, an array of strings
Code:
string A[] = { ... }
int nElements = sizeof(A) / sizeof(A[0]);
Note that the above for strings does NOT calculate the number of characters that the strings point to. It only tells you how many different strings are in the array. | That only works if each string in the array is the same length. For instance, if A = {"aaaaaaaaaaaaaaaaaaaaaaaa","foo","bar","baz","a","a"}, then your method would not work as intended, afaik.
|

February 17th, 2004, 08:34 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
Quote: | Originally Posted by php4geek That only works if each string in the array is the same length. For instance, if A = {"aaaaaaaaaaaaaaaaaaaaaaaa","foo","bar","baz","a","a"}, then your method would not work as intended, afaik. |
You just flunked C++ 101 course. sizeof(string) does not include the characters. Try this simple example
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string A[] = {"Hello","World","One","Two","Three"};
int nElements = sizeof(A) / sizeof(A[0]);
cout << "nElements: " << nElements << endl;
cin.ignore();
return 0;
}
the output if 5 regardless of the number of characters in the
strings.
|

February 17th, 2004, 08:36 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
I know you think you answered this, Dragon, but you didn't. I'm asking for the definition of type string or the name of a header file or other document where it can be found, so I can learn.
Picking at a non-existent pimple in the pursuit of guruness is counterproductive and helps no one. If your solution works, then the type definition has to produce a fixed-length item, whether it's an integer or fixed-length string, or whatever.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

February 17th, 2004, 08:48 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
Quote: | Originally Posted by DaWei_M I know you think you answered this, Dragon, but you didn't. I'm asking for the definition of type string or the name of a header file or other document where it can be found, so I can learn.
Picking at a non-existent pimple in the pursuit of guruness is counterproductive and helps no one. If your solution works, then the type definition has to produce a fixed-length item, whether it's an integer or fixed-length string, or whatever. |
I am sorry if I misunderstood your original question. string is a c++ STL (Standard Template Library) class, and can be found in <string> header file -- note that current version does not have an extension, but some very old c++ compilers may require the .h extension.
I wouldn't spend too much time studing the source code because from what I saw it is almost unreadable. Better to use a tutorial, or read the book about its functions.
|

February 17th, 2004, 08:58 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
I'm sorry. I never had the need to do sizeof(string), only size of the array itself and other pointers. I did not realize that it would work that way. 
|

February 17th, 2004, 08:59 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
Quote: | Originally Posted by DaWei_M IPicking at a non-existent pimple in the pursuit of guruness is counterproductive and helps no one. If your solution works, then the type definition has to produce a fixed-length item, whether it's an integer or fixed-length string, or whatever. |
Also please note that the correct use of the sizeof operator is not a trival matter. It is use in many many situations. If one does not know how to use it correctly, it will produce unexpected, and probably unwanted results.
One just has to remember that the string class is a fixed-length object regardless of how may characters it points to. This is not the same an a char array, which may include all the characters.
Code:
char array[] = "Hello World";
string str = "Hello World";
the sizeof operator used on each of the two above arrays will produce significantly different results. The sizeof(array) includes all the characters in "Hello World", while the sizeof(str) does not.
|

February 17th, 2004, 09:00 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
|
Excellent. Thank you for pointing that out to me. I think you just made my coding life a /lot/ easier.
|

February 17th, 2004, 09:05 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
Quote: | Originally Posted by php4geek Excellent. Thank you for pointing that out to me. I think you just made my coding life a /lot/ easier. |
You're welcome. They don't call me "Ancient" for nothing! 
|

February 17th, 2004, 09:09 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Thanks. That's pretty gross, alright, and I used to think the Win API macros were bad...
|

February 17th, 2004, 10:08 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
I don't want to beat this to death, but catz's problem is solved and maybe it'll be interesting. It is to a guy that's been out of the trenches for 10 years.
string is a typedef of basic_string, which is associated with a number of things, including a traits structure. You don't wanna know about the 'E's, 'T's, and 'A's unless you're trying to develop a headache.
The item stored in each element of string A[] isn't the string and it isn't just a simple pointer, but the effect is the same: they're all the same size, 16 bytes, as revealed by the sizeof operator. The actual size of the individual character entities may be retrieved by A[0].size() and so forth. (You can resize them without actually changing their textual value; they get padded with nulls or whatever's specified.)
Regards, old Bubba David, got 3 years on Ancient :-)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|