Difference Between char* s and char s[] and char s[n]
This page answers questions like these:
- What’s the difference between char* s and char s[] in C/C++?
- What’s the difference between char s[] and char s[n] in C/C++?
- What’s the difference between char s[n] and char* s in C/C++?
Related Links:
Difference Between char* s = "string"; and char s[] = "string";
warning/error: invalid conversion from 'char**' to 'const char**'
Difference Between char* s and char s[] and char s[n]
char* s;
- Create a character pointer s (which is uninitialized).
- You can change the value of s. i.e. You can make it point to something else.
- sizeof(s) == sizeof(char*).
char s[];
- Invalid as a stand-alone variable! This is not valid C++ or C. (See below for valid usage inside a struct or function.)
char s[5];
- Create an array s of 5 characters. The array elements are uninitialized.
- You can’t change the value of s. i.e. You can’t make it point to something else.
- sizeof(s) == 5 because there are 5 characters in the array.
struct MyStruct
{
char* s;
};
- Declare a struct type containing a character pointer s.
- In any variable of this type, the element s is uninitialized.
- You can change the value of s. i.e. You can make it point to something else.
- sizeof(MyStruct) == sizeof(char*) because s is a variable of type char*.
struct MyStruct
{
char s[];
};
- Declare a struct containing an array s of characters of unknown length. i.e. The variable s is merely another name for the first memory location in the struct.
- You can’t change the value of s. i.e. You can’t make it point to something else.
- sizeof(MyStruct) == 0 because s is merely a name for a memory location, not a variable.
int func(char* s) { ... };
int func(char s[]) { ... };
- Declare a function taking a char* and returning an int.
- There is no difference between these two function declarations.
Related Links:
Difference Between char* s = "string"; and char s[] = "string";
warning/error: invalid conversion from 'char**' to 'const char**'
Home > C++ / C > Difference Between char* s and char s[] and char s[n]
Tags: strings in C, C string, C array length, C char array, char*, char[], bounded array, unbounded array, C++, C
Copyright © HelpDoco.com
difference-between-char-star-and-char-array.txt
C++-C/difference-between-char-pointer-and-char-array.htm
1