
I know a lot of people had answered your question, but here are some points: But since the 'std::string' class only holds the pointer to this potentially infite length buffer, the sizeof(std::string) always remains the same as sizeof pointer on the given architecture which on your system is 4. This can have as many elements as the system allows (C++ places no restriction on this length). Inside the 'std::string' class, there is a non static member pointer which is allocated dynamically to hold the input buffer. It does not mean that the actual buffer of characters (which is called string in common parlance) is only 4 bytes. Note that signed T, unsigned T always have the same size. It occupies 4 times the number of bytes that a 'char' on that machine takes. Having said, that assuming that 'string' is 'std::namespace', sizeof(string) = 4 means that the sizeof the 'std::string' class is 4 bytes. This is true only when a char is 8-bits, and plain char is unsigned. So it is wrong to assume that char can have only values from 0 to 255. A plain char is either signed or unsigned. The next important thing to note is that C++ has three character types: plain char, signed char and unsigned char. The sizeof applied to any other type is in multiples of sizeof(char) which is by definition 1. There are architectures where a BYTE is 32-bits, 24-bits and so on. A byte which we think is 8-bits need not be the case. New_length = target_size - 1 //That is all that will fit.Sizeof(char) is always 1 byte.

to as much of the value of source as will fit is stored in target.Ĭhar short_string //Can hold strings of up to 10 characters. If target is not large enough to hold the entire string, a string equal source, provided the declared size of target is large enough. Postcondition: The value of target has been set to the string value in The array source contains a string value terminated with '\0'. Precondition: target_size is the declared size of the string variable Void string_copy( char target, const char source, int target_size) and the usage of cstring parameters in a function. Example to demonstrate some of the cstring functions The null character can be used to detect the end of the cstring value.

If a function only uses the value in a cstring argument, but does not change that value, then there is no need to keep track of its declared or actual size. A cstring variable is an array, so a cstring parameter to a function is simply an array parameter.Īs with any array parameter, whenever a function changes the value of a cstring parameter, it is safest to include an additional integer parameter giving the declared size of the cstring variable.Cstring as Function Arguments Cstring as Function Arguments
