C++ How does this piece of code work(pointers and char arrays)?
Consider the following:
A pointer is a variable (usually one byte in size) that "points" to another place in memory. What the variable points to can be very large, very small, it doesn't matter. In C++, when you declare a "normal" variable, it makes space for the size of the object assigns it whatever value you give it:
int i = 15;
the variable i is of type "int", and is equal to a value of 15. Cool. Now, you declare a pointer variable:
int *myPtr;
The great confusion of this syntax is that the "*" operator can either be a multiplication operator, or a dereference operator (more on that in a minute). But in this case, it is neither. The above line of code declares a variable myPtr of type "pointer to an int", and it is not initialized (it is currently referred to as a null pointer). So, we want to do something useful with it. So, we can "point" the pointer variable to the first variable "i" like this:
myPtr = &i;
The "&"operator, when placed before a variable, means "take the address of this variable". Recall from before that the variable i has an address in memory, and a value associated with it. We have set myPtr to point to the address of i. This then allows us to use a dereference operation to get the variable:
*myPtr = 20; //i now equals 20, NOT 15
As mentioned before, "*" is a dereference operator, when appearing before a pointer variable. As such, these two lines are now equivalent:
i = 25;
*myInt = 25;
Both set the variable "i" to now equal 25! Further, you cannot directly assign to a pointer, but you can add or subtract from it (more on that later). So this is a syntax error:
myInt = 50; //ERROR! myInt is of type pointer, and cannot be assigned to a number
Okay, so that's all well and good, but how does that make "const char *" a string of characters? Well, it may be helpful to talk about arrays. Arrays in C++ are really just pointers in disguise. These two lines of code are the same:
const char *myChar = "Hello";
const char myChar[] = "Hello";
The easy question to answer is: where did const come from? The const keyword in C++ is absolutely wonderful. It allows variables to be declared that cannot be modified after they are assigned. As such, strings of characters are constant in memory. The compiler will store those strings somewhere in memory, and never change them. Don't worry about why for now, just accept that strings are not changeable as const char * variables. Single characters are in single quotes in C++, where strings are in double quotes. Also, the syntax myChar[0] would mean the first character in the myChar array. Okay, so now we look at the second example and exactly what the array contains:
myChar[0] = 'H'
myChar[1] = 'e'
myChar[2] = 'l'
myChar[3] = 'l'
myChar[4] = 'o'
myChar[5] = '\0'
Whoa, so what is that '\0' character at the end? Well, the forward slash is called an escape character, and it just means "treat whatever comes after this as something special". The '\0' character is especially special, as it is called the "null" character. It doesn't actually mean character code 0, it means "this is the end, period." So, when a computer reads through the character array of myChar, it gets to the null character and says "okay, this is the end of the string".
To bring it all together, recall that variable "myChar" is of type pointer to const char. The last trick up our sleeves is to know that you can add or subtract to pointers (as mentioned above). What does this do? Well, pretend myChar is pointing to an address of your house on your street. myChar+1 would then point to an address of your neighbor! Further, myChar-1 would point to an address of your other neighbor. And before we go all the way, note that this is a very easy way to crash programs: If you try to use the dereference operator on a pointer to a place in memory it does not own, the program will crash (called undefined behavior), and your operating system will tell you there was a "segmentation fault". This makes sense, as you don't want a program to be able to access any memory it darn well pleases, especially if that other memory has sensitive information in it. Okay, so now the above: the syntax [0], [1], ... is equivalent to this:
*(myChar+0) = 'H'
*(myChar+1) = 'e'
*(myChar+2) = 'l'
*(myChar+3) = 'l'
*(myChar+4) = 'o'
*(myChar+5) = '\0'
So each line adds to the myChar pointer (taking the address of a neighbor) before dereferencing it. So in memory, the computer has the string "Hello\0", and you are just taking the address of them one by one! Since the variable myChar was declared with an entire string constant (myChar = "Hello";), the operating system knows that your program owns all of that memory, as long as you don't try to actually use the 6th line (myChar + 5), because we now know that myChar+5 points to a null character.
Sorry if some thing doesn't make sense, but that's a (very) basic overview of pointers.
TLDR: The & symbol is an "address of" operator, and the * symbol is either a "I'm declaring this as a pointer" or a "dereference this pointer" operator.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now