Short answer
Is char[] and char * the same in C? No. They are different types, but they often behave alike in code, which is why people mix them up. A char[] is an array: a block of char values stored together, with the storage belonging to that array. A char * is a pointer: a variable that holds an address, usually the address of the first character in a block.
How they differ in size
#include <stdio.h>
int main(void) {
char array_text[] = "hello";
char *pointer_text = "hello";
printf("sizeof array_text: %zu\n", sizeof array_text); /* 6 */
printf("sizeof pointer_text: %zu\n", sizeof pointer_text); /* often 8 on 64-bit systems */
return 0;
}
sizeof array_text is the size of the whole array: five letters plus the terminating null character, so 6 bytes. sizeof pointer_text is the size of the pointer itself. It has nothing to do with the length of the string, and its value depends on the system.
sizeof(char) is 1 by definition, so the size of a char array is simply its number of elements. Is char always 1 byte in C covers that rule.
Why they look alike
In most expressions, an array name turns into a pointer to its first element. This is called array-to-pointer decay. That is why array_text[1] and pointer_text[1] both read the second character, and why either one can be passed to a function that expects a char *.
Function parameters follow the same rule. void print_word(char word[]) and void print_word(char *word) declare the same parameter type, and the compiler treats both as char *. The question can I pass an array to a function in C covers the rest of that story.
Assignment and changes
The two differ when you try to change them.
- An array can’t be assigned as a whole.
array_text = "bye";doesn’t compile. To put new text into an array, copy it withstrcpyfrom<string.h>, and make sure the array is big enough for the text and its null character. - A pointer can be reassigned.
pointer_text = "bye";makes the pointer refer to a different string.
char buffer[10];
strcpy(buffer, "bye"); /* copies into the array's own storage */
char *pointer_text = "hello";
pointer_text = "bye"; /* the pointer now refers to another string */
Changing the characters of a string literal through a pointer, such as pointer_text[0] = 'H';, is undefined behavior in C. The array version, array_text[0] = 'H';, is allowed, because the array holds its own copy.
The C++ difference
In C++, string literals have type const char[]. Because of that, the correct form is const char *pointer_text = "hello";. The standard no longer allows the non-const form char *pointer_text = "hello";, and compilers usually warn about it. For what the star means in a declaration like this, see what does char* do in C++.
Quick summary
- A
char[]owns its own storage, andsizeofgives the total size of the array. - A
char *stores an address, andsizeofgives the size of the pointer. - Both can be indexed, and both can hold a C string that ends with a null character. What is a C string covers that.
- An array can’t be assigned as a whole, but a pointer can be reassigned.