I know this is a very basic question. I am confused as to why and how are the following different.
char str[] = \"Test\";
char *str = \"Test\";
"Test"
is an array of five characters (4 letters, plus the null terminator.
char str1[] = "Test";
creates that array of 5 characters, and names it str1
. You can modify the contents of that array as much as you like, e.g. str1[0] = 'B';
char *str2 = "Test";
creates that array of 5 characters, doesn't name it, and also creates a pointer named str2
. It sets str2
to point at that array of 5 characters. You can follow the pointer to modify the array as much as you like, e.g. str2[0] = 'B';
or *str2 = 'B';
. You can even reassign that pointer to point someplace else, e.g. str2 = "other";
.
An array is the text in quotes. The pointer merely points at it. You can do a lot of similar things with each, but they are different:
char str_arr[] = "Test";
char *strp = "Test";
// modify
str_arr[0] = 'B'; // ok, str_arr is now "Best"
strp[0] = 'W'; // ok, strp now points at "West"
*strp = 'L'; // ok, strp now points at "Lest"
// point to another string
char another[] = "another string";
str_arr = another; // compilation error. you cannot reassign an array
strp = another; // ok, strp now points at "another string"
// size
std::cout << sizeof(str_arr) << '\n'; // prints 5, because str_arr is five bytes
std::cout << sizeof(strp) << '\n'; // prints 4, because strp is a pointer
for that last part, note that sizeof(strp) is going to vary based on architecture. On a 32-bit machine, it will be 4 bytes, on a 64-bit machine it will be 8 bytes.
The diference is the STACK memory used.
For example when programming for microcontrollers where very little memory for the stack is allocated, makes a big difference.
char a[] = "string"; // the compiler puts {'s','t','r','i','n','g', 0} onto STACK
char *a = "string"; // the compiler puts just the pointer onto STACK
// and {'s','t','r','i','n','g',0} in static memory area.
char str[] = "Test";
Is an array of chars
, initialized with the contents from "Test", while
char *str = "Test";
is a pointer to the literal (const) string "Test".
The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test"
, while the pointer simply refers to the contents of the string (which in this case is immutable).
A pointer can be re-pointed to something else:
char foo[] = "foo";
char bar[] = "bar";
char *str = foo; // str points to 'f'
str = bar; // Now str points to 'b'
++str; // Now str points to 'a'
The last example of incrementing the pointer shows that you can easily iterate over the contents of a string, one element at a time.
The first
char str[] = "Test";
is an array of five characters, initialized with the value "Test"
plus the null terminator '\0'
.
The second
char *str = "Test";
is a pointer to the memory location of the literal string "Test"
.
One is pointer and one is array. They are different type of data.
int main ()
{
char str1[] = "Test";
char *str2 = "Test";
cout << "sizeof array " << sizeof(str1) << endl;
cout << "sizeof pointer " << sizeof(str2) << endl;
}
output
sizeof array 5
sizeof pointer 4