There are plenty of similar inquires, but in my case I don't understand what isn't working:
int mysize = 0;
mysize = sizeof(samplestring) / sizeof(*samplestring);
std::cout << mysize << '\n' << samplestring;
This outputs:
4
Press 'q' to quit.
How is it possible? 4 definitely isn't the size of this string. I even tried the following, with the same result:
mysize = sizeof(samplestring) / sizeof(samplestring[0]);
EDIT: Ok, this is the declaration:
char *samplestring = "Start.";
I'm on C++, but I need to use functions that only accept char *. Later in the code I assign new strings to that variable, like:
samplestring = "Press 'r' for red text.";
Yes, the compiler gives me warnings, but I have no idea how can I use different strings if I can't overwrite them...
There are two ways of making a string constant, and your technique only works on the first one. The first one makes an array of characters which you can get the size of at compile time, and the other creates a pointer to an array of characters.
Trying to take the size of the second case the way you're doing it just gives you the size of a pointer. On a 32-bit build the size of a pointer is 4 characters, i.e. a pointer takes the same amount of memory as 4 characters.
The following will always give the correct length for a properly null-terminated string, but it's slower.
4
isn't the size of the string, becausesamplestring
isn't a string. It's achar*
, whose size is (on your platform) 4, divided by 1 (size ofchar
) is, correctly, 4.In C++, you'd use
std::string
and thelength()
method.In C, you'd use
strlen
which takes as parameter a NULL-terminated char pointer.The sizeof of an element returns the size of the memory allocated for that object. In your example the string is probably declared somewhere as
In which case the size of the memory is 4. The method you probably want in your application is
Which returns the size of the null terminated string (without the termination)
you are asking the size of a pointer on a char. So I guess you're using a 32bit system.
If you're using C++, use std::string :
First of all,
sizeof(samplestring[0])
is the same assizeof(*samplestring)
, they're both returning the size of the first element of thesamplestring
array. And, assumingsamplestring
is an array of chars,sizeof(char)
is defined to be 1.You haven't shown how
samplestring
is declared. It could be one of the following:or
or
In the first 2 cases the type of
samplestring
ischar *
, sosizeof(samplestring)
returnssizeof(char *)
, which, on your platform is 4.In the third case, the type of
samplestring
ischar[10]
(array of 10 chars), but if you call a function that takes achar *
as its parameter, the char array will decay to a pointer pointing to the first element of the array. In this case, trying to printsizeof
within the function will still result in the size of a pointer being printed.If you want the size of the original array to be printed from within the function, then the function parameter needs to be a pointer to the original array type (and the type includes size of the original array).
Output:
This fixes the size of the array that can be passed to the function and is not desirable in a lot of cases. The only other solution is to keep track of the array yourself (or use
strlen
if you have a NULL terminated string).It looks as though you have a pointer, not an array. Arrays are converted to pointers when the program requires it, so you'd get:
although, since
sizeof (char) == 1
, the division is redundant here; if the pointer were to a larger type, then you'd get a differently unexpected result.In C++ (but obviously not C), you can deduce the size of an array as a template parameter:
or you can use classes such as
std::vector
andstd::string
to keep track of the size.In C, you can use
strlen
to find the length of a zero-terminated string, but in most cases you'll need to keep track of array sizes yourself.