Characters. It Has A Starting Point and Ends With A String-Termination Character. While
Characters. It Has A Starting Point and Ends With A String-Termination Character. While
http://www.bogotobogo.com/cplusplus/string.php
1. "Hello" is a string literal, and we may want to use the following to assign it to a
pointer:
The following code is converting int to string, and it can be an example of returning a
pointer to character array from C++ string.
1. #include <sstream>
2. char *int2strB(int n, char *s)
3. {
4. stringstream ss;
5. ss << n;
6. string cpp_string = ss.str();
7. const char *pCstring = cpp_string.c_str();
8. s = const_cast<char *>(pCstring);
9. return s;
10. }
11.
In the code, we made a C++ string from stream (ss.str()), then converted it to C string
(const char *) using c_str(). Finally, we casted the constantness (const_cast<char *>)
to make it char * type.
12. An array name is a constant pointer to the first element of the array, and that's why
we can't even copy arrays using assignment.(arrayA = arrayB is not allowed)
C string
Though the Standard C library is included as a part of Standard C++, to use C library,
we need to include the C header file:
#include <cstring>
C-style strings have a special feature: The last character of every string is the null
character.
Both of these are arrays of char, but only the second is a string (an array of 9
characters). So, the 8-character string requires a 9-character array. This scheme makes
finding the length of the string an O(n) operation instead of O(1) operation.
So, the strlen() must scan through the string until it finds the end. For the same reason
that we can't assign one C array to another, we cannot copy C strings using '=' operator.
Instead, we generally use the strcpy() function. However, note that it has also long
been recommended to avoid standard library functions like strcpy() which are not
bounds checked, and can cause buffer overflow.
while(*str++){ }
The char* pointer str is dereferenced, and the character addressed is checked if it's true
or false (if it's null).
In both cases, each initialize a variable to the string "hello". The first declaration creates
a six-element array str containing the characters 'h', 'e', 'l', 'l', 'o' and '\0'. The second
declaration creates pointer variable pStr that points to the letter h in the string "hello",
which also ends in '\0', in memory.
Attempting to modify one of the elements str is permitted, but attempting to modify one
of the characters in pStr will generate a runtime error, causing the program to crash.
char str[50]="0123456789";
defines an array of characters with a size of 50 chars, but the C string, str, has a length
of only 10 characters. So, sizeof(str) = 50, but strlen(str) = 10.
String literals have static storage class, which means they exist for the duration of the
program. They may or may not be shared if the same string literal is referenced from
multiple locations in a program. According to the C++ standard, the effect of attempting
to modify a string literal is undefined. Therefore, we should always declare a pointer to a
string literal as const char *.
Since there are still many program situations which require understanding C-style string,
we need to be familiar with the C-style string.
Let's find out how much we know about C-string using the examples below. Can you
figure out what's wrong?
Question 1
Question 2
char *cstr2;
strcpy(cstr2, "hello");
*(cstr2)='t';
Question 3
char cstr3[100];
cstr3 = "hello";
*(cstr3)='t';
Question 4
When our program is compiled, the compiler forms the object code file, which contains
our machine code and a table of all the string constants declared in the program. In the
statement,
causes cstr1 to point to the address of the string hello in the string constant table.
Since this string is in the string constant table, and therefore technically a part of the
executable code, we cannot modify it. We can only point to it and use it in a read-only
manner.
The "hello" is a string literal (or string constant) because it is written as a value, not a
variable. Even though string literals don't have associated variables, they are treated
as const char*'s (arrays of constant characters). String literals can be assigned to
variables, but doing so can be risky. The actual memory associated with a string literal
is in a read-only part of memory, which is why it is an array of constant characters. This
allows the compiler to optimize memory usage by reusing references to equivalent
string literals (that is, even if your program uses the string literal "hello" 100 times, the
compiler can create just one instance of hello in memory). The compiler does not,
however, force our program to assign a string literal only to a variable of type const
char* or const char[]. We can assign a string to a char* without const, and the program
will work fine unless you attempt to change the string, which is what we're trying to do in
the last line of the Question #1.
Answer 2
This code is compiled successfully. But we need to allocate memory for the character
pointer.
The right code should look like this:
At the line:
cstr3 = "hello";
cstr3="hello";
int a[100];
int b[100];
...
a = b; //error
Answer 4
No problem at all.
Summary
In general, the Asnwers from 1 to 4 may vary depending on compiler. So, the best way
of avoiding unexpected run-time error, is to use a pointer to const characters when
referring to string literals:
const char* ptr = "hello"; // Assign the string literal to a variable.
C Library Utilities
Standard C library gives us a set of utility functions such as:
Some of the source code for the library utility functions can be found Small Programs.
Because we manipulate the C string using pointer which is low-level operation, it's error
prone. That's why we have C++ <string> class.
C++ string
Though there are lots of advantages of C++ string over C string, we won't talk about it at
this time. But because numerous codes still using C style string, we need to look at the
conversion between them. Here is an example with some error messages we'll get if we
run it as it is.
#include <string>
#include <cstring>
int main()
{
using namespace std;
string str1;
const char *pc = "I am just a character array";
return 0;
}
String Initialization
Here are the couple of ways to initialize a string:
The string class provides several constructors. If we define a string type without
explicitly initializing it, then default constructor is used.
Note that initialization and assignment are different operations in C++. When we do
initialization, a variable is created and given its initial value. However, when we do
assignment, an object's current value is obliterated and replaced with a new one. For a
built-in type variable like int, there is little difference between the direct and the copy
forms of initialization. However, when we deal with more complex types, the difference
becomes clear. The direct initialization tends to be more efficient.
tring Operations
1. s1=s2
2. s+=a
3. s[i]
subscripting
4. s1+s2
5. s1<s2
6. s1==s2
7. s.size()
8. s.length()
10. s.begin()
11. s.end()
12. s.insert(pos,a)
13. s.append(pos,a)
14. s.erase(pos)
15. pos=s.find(a)
17. getline(in,s)
Write from s to out.
The name of an array is the address of its first element. The name in the cout is the
address of the char element containing the character A. The cout object assumes that
the address of a char is the address of a string. So, it prints the character at that
address and then continues printing characters until it meets the null character, '\0'.
In other words, a C string is nothing more than a char array. Just as C doesn't track the
size of arrays, it doesn't track the size of strings. Instead, the end of the string is marked
with a null character, represented in the language as '\0'. So, if we give the cout the
address of a character, it prints everything from that character to the first null character
that follows it.
The key here is that name acts as the address of a char which implies that we can use
a pointer-to-char variable as an argument to cout.
1. strings in an array
2. quoted string constants
3. strings described by pointers
are all handled equivalently. Each of them is really passed along as an address.
The following example shows how we use different forms of strings. It uses two
functions from the string library, strlen() and strcpy(). Prototypes of the functions are
in cstring header file.
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
cout << nameArr << " and " << namePtrConstChar << endl;
cout << endl;
ptr = nameArr;
cout << "1: " << nameArr << " @ " << (int *)nameArr << endl;
cout << "1: " << ptr << " @ " << (int *) ptr << endl;
cout << endl;
"Edsger W. Dijkstra" actually represents the address of the string, so this assigns the
address of
The code illustrates that we can use the array name nameArr and the
pointer namePtrConstChar equivalently with cout. Both are the addresses of strings,
and coutdisplays the two strings stored at those addresses.
Let's look at the following code of the example:
cout << "1: " << nameArr << " @ " << (int *)nameArr << endl;
cout << "1: " << ptr << " @ " << (int *) ptr << endl;
To get a copy of a string, we need to allocate memory to hold the string. We can do this:
Then, we copy a string from the nameArr to the newly allocated space. It doesn't work if
we assign nameArr to ptr because it just changes the address stored in ptr and thus
loses the information of the address of memory we just allocated. Instead, we need to
use the strcpy():
strcpy(ptr, nameArr);
The strcpy() function takes two arguments. The first is the destination address, and the
second is the address of the string to be copied. Note that by using strcpy() and new,
we get two separate copies of "Alan Turing":