1
char* string = "hello there";

cout << sizeof( string ); // prints 4, which is the size of pointer
cout << sizeof( *string ); // prints 1, which is the size of char

How do I get the number of characters contained in the string (11)?

SkyPower
  • 103
  • 2
  • 13
  • I don't see either of those as dupes, although I'd be surprised if there wasn't a dupe _somewhere_ on SO (I couldn't find it with a cursory search). The first talks about the _size_ of a dereferenced char*, the second appears to deal with C++ _proper_ strings rather than char pointers. – paxdiablo Jun 09 '13 at 11:55
  • @paxdiablo I am not totally convinced the second one is restricted `std::string`, newcomers to the language often aren't even aware of its existence. – juanchopanza Jun 09 '13 at 12:00
  • @juanchopanza, when I see C++ strings without a qualifier of some sort, I tend to assume the proper ones, though you're right - a lot of the answers cater to both types. – paxdiablo Jun 09 '13 at 12:04

2 Answers2

4

It's strlen you want for that, not sizeof. The first counts the number of characters up to the terminating NUL while the second gives you the size of the type which, in this case, is a pointer rather than the underlying array of characters.

By that last point, I mean:

char *x = "hello there";
char y[] = "hello there";
std::cout << sizeof(x) << ' ' << sizeof(y) << '\n';

will most likely output something like:

4 12

on a system with 32-bit pointers (and 8-bit char). In that case, the 4 is the size of the pointer, the 12 is the number of bytes in the array (including the NUL at the end).

In any case, that's moot, since strlen() is the right way to get the length of a C string (yes, even in C++, though you may want to consider using the C++ strings since they may save you a lot of trouble).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks, that's what I was looking for. By the way, I didn't know that strings in c++ ended in null aswell, I thought that was the case just in c. – SkyPower Jun 09 '13 at 11:58
  • 1
    @SkyPower, C++ strings (`std::string`) are different from C strings and you still have C strings available to you in C++. Those strings in your question (and my answer) are most definitely the C variety. – paxdiablo Jun 09 '13 at 12:02
1

The function sizeof() returns the size of a data type in Byte
For example because you define:

char* string = "hello there";

then type of string is char * and size of mostly all pointer is 4 Bytes ( this function returns 4) But type of *string is char and size of every character is 1 Byte ( This function returns 1)
Solution for you :
Alternative 1: Use function strlen() in library 'string.h'

Alternative 2:(from scratch)

int length = 0;
int index = 0;
while ( string[index] != '\0')
{
length++;
index++;
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 1
    I'd be a little less "certain" that pointers are always four bytes but otherwise a good answer. Though I'm not sure why you have both `index` _and_ `length` in your code, that seems a little wasteful. You could just as easily do something like: `char *tmp = str; while (*tmp != '\0') tmp++; return tmp - str;`. – paxdiablo Jun 09 '13 at 11:58
  • @paxdiablo : Yes, size of all pointer is not 4 bytes but size of most of them is 4 byte / 'index' is used to improve readablity – frogatto Jun 09 '13 at 12:01
  • 1
    The size of most pointers on my platform is 8 bytes. And it is not a very exotic platform, it is just an x86-64. – juanchopanza Jun 09 '13 at 12:07
  • @juanchopanza: My mean is in 32-bit based systems. – frogatto Jun 09 '13 at 12:37
  • If everyone reverted to the mean, we'd all be eating McDonalds instead of real food, or listening to awful, androgynous boy bands, or reading autobiographies from 17-year-old girls who don't seem to have the concept that they haven't actually _lived_ a life worth telling yet :-) – paxdiablo Jun 10 '13 at 00:48