+ 3

What is the difference between char* s and char s[]?

In C++, we may use one of these three ways to define a string: 1) char* s = "Hello World!"; 2) char s[] = "Hello World!"; 3) string s = " Hello World!"; I do get the third one but what is the difference between the first one and the the second one? The first one defines a pointer, doesn't it? Then how can it be used as a string? Please, could anyone explain me in detail about the difference in structure, compile time, runtime, memory allocation etc.? It is causing me trouble. Thanks!

17th Mar 2018, 1:24 PM
Naveen Maurya
Naveen Maurya - avatar
6 Respostas
+ 2
That's a good question, and there is a difference between char * s and char s [] . However I am going to change names for my benefit , but every thing is the same. #1) char cname[]= "Hello World" ; #2) char *pName = "Hello World"; The first one is a character array , because of the empty brackets. Also the compiler counts the number of characters/letters plus the terminating null to mark the end of the string. The second is a character pointer that points to type char. pName is a pointer variable meaning it stores the address of the array it points to. But cname is a fixed pointer constant, that is its address can't be changed. It is not a pointer variable. But variable values can be changed any time you feel like it. Just as I can do this as long it is the same data type. For example float x = 2.4; float y = 3.14 ; then later in my code I can do the same with pointer variables. float x = 2.4 ; float y = 3.14 ; float *ptr = &x ; printf (" x is %f \n", *ptr); then do this ptr = &y; printf ("y is %f ", *ptr); I can do the same with character pointers. char * pName = "Rick"; then a few lines later do this pName ="Saturday"; C/C++ compiler is not really putting Saturday into pName, because pName can hold only addresses. But the compiler is putting the address of Saturday into the pointer variable pName. Now back to first one cname[] = "Hello World"; if I decide to change it later to cname ="error" ; in my code, the compiler will give a error , because once the program is compiled , the compiler never uses the word cname it just uses the starting address of the first member of the array and can never be changed. It is a fixed pointer constant. cname is an address only. That is I can't do this 2 = 10+ 2 ; because 2 can't be assigned to (10+2) , because it is also a constant and constants can't be changed they are not variables. Just like I can't do this #define PI 3.1416 then do this PI = 4, because constants are not variables. They are fixed .
17th Mar 2018, 10:46 PM
Rick Zalman
+ 5
There is no difference. "12345" takes 6 bytes no matter what you store it in. char s[] = {'1', '2', '3', '4', '5', '\0'}; All character strings include a null character to terminate the string. x = s[5]; generates code like; x = s + 5; in both cases the address of s is put in a register, 5 gets added to the register, and the register gets stored back in x.
17th Mar 2018, 2:38 PM
John Wells
John Wells - avatar
+ 3
There is no difference. C++ doesn't check types at that level so arrays and pointers are interchangeable so: char *s = "Hello World!"; cout << s[11]; cout << &s[6]; is legal and outputs '!World!'.
17th Mar 2018, 1:40 PM
John Wells
John Wells - avatar
+ 2
Thanks Naveen , I thought at the time I might of confused people reading my post, but remember this in my example cname [] is a collection of variables of the same type , and cname and is the starting address of the array and pName is a pointer variable and I am glad I helped you understand the difference between the two.
18th Mar 2018, 2:57 PM
Rick Zalman
0
@John Wells What about the differences in memory allocation, compile time etc.?
17th Mar 2018, 1:49 PM
Naveen Maurya
Naveen Maurya - avatar
0
@Rick Zalman Thanks a lot!
19th Mar 2018, 3:38 AM
Naveen Maurya
Naveen Maurya - avatar