Difference Between "char a[] = "string";" and "char *p = "string";"
In programming, variables can be either pointers or arrays. When declaring variables, the syntax for pointers and arrays differs. Understanding the distinction between the two is crucial for correctly manipulating data.
char a[] = "string";
This line of code creates an array of characters large enough to store the string "string" along with its null-terminator. The array "a" is initialized with the elements of the string "string". An array can be modified later on, and its size is known at compile-time, allowing the use of the "sizeof" operator to determine its length.
char *p = "string";
This line of code creates a pointer named "p" that points to the address of the string "string". The pointer "p" cannot be modified, but the string it points to can, resulting in undefined behavior.
Comparison:
Note: The example in the question contains a typo, as "?" is not a valid operator in this context. It has been assumed that the intended question was about the difference between arrays and pointers.
The above is the detailed content of What's the Difference Between `char a[] = 'string';` and `char *p = 'string';` in C?. For more information, please follow other related articles on the PHP Chinese website!