Skip to main content
added 224 characters in body
Source Link
Sourav Ghosh
  • 133.9k
  • 16
  • 187
  • 266

In your case

 char* s = "my String"

is legal and valid, because a representation like "my String" is called a string literal and the representation essentially gives the address of the first element in the string, which is again a pointer to a char. So, we can store that in another pointer to a char.

The usability, is mostly same with char s[10] = "my string";, except the fact that in former case, s pointing to a string literal, you cannot (even attempt to) modify it. Trying so will invoke undefined behavior.

In your case

 char* s = "my String"

is legal and valid, because a representation like "my String" is called a string literal and the representation essentially gives the address of the first element in the string, which is again a pointer to a char. So, we can store that in another pointer to a char.

In your case

 char* s = "my String"

is legal and valid, because a representation like "my String" is called a string literal and the representation essentially gives the address of the first element in the string, which is again a pointer to a char. So, we can store that in another pointer to a char.

The usability, is mostly same with char s[10] = "my string";, except the fact that in former case, s pointing to a string literal, you cannot (even attempt to) modify it. Trying so will invoke undefined behavior.

Source Link
Sourav Ghosh
  • 133.9k
  • 16
  • 187
  • 266

In your case

 char* s = "my String"

is legal and valid, because a representation like "my String" is called a string literal and the representation essentially gives the address of the first element in the string, which is again a pointer to a char. So, we can store that in another pointer to a char.