Ok, char c
is the definition of a variable which contains a 8 bit number. The char* c
is a variable which contains the address of a variable which stores a 8 bit number. So the first thing you have to understand is the meaning of the *
and &
operators:
The &
operator means "get the address from". So for example if you have something like:
char c = 'a';
Then you have declared and defined a variable called "c". The content of this variable is the letter "a".
Now lets declare a pointer:
char* cPtr;
As you already know is "cPtr" a variable which contains the address of a char variable. So let's assign the address of our variable "c" to our pointer variable "cPtr":
cPtr = &c;
Now our pointer variable contains the address of the variable "c". What we can do now is to access the content of the variable "c" via our pointer "cPtr" by prepending our pointer variable with the *
operator. So the *
operator means something like "get the content of the variable to which this pointer is pointing to". When I write something like:
char b = *cPtr;
Then I create a new variable "b" and assign the value of the variable "c". It is equivalent to char b = c
because I have dereferenced the pointer variable cPtr, which means I used the pointer (address) stored in "cPtr" to access the content of c.
Now we create an array of char values:
char cArray[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
This array is a sequence of 6 char values which are placed in the same order in the memory. So in the first place in the memory you have the value 'H', the next place in the memory contains the value 'e' and so on. The difficult thing here is that you alwys need the index to access the single values. So if you write something like:
char a = cArray[1];
Then you copy the 'e' of the array to the variable "a".
Now the confusing thing is that an array can also be treaded like a pointer:
char* arrayPtr;
arrayPtr = cArray;
This means that you assigned the address of the first array element to the pointer variable arrayPtr. So you have to know if you use the array name without the index then you'll use the pointer to the first element of the array.
So if you create a string using
char* myString = "Hello";
then (under the hood) you create an array of char variables like shown with cArray.