Character pointers are a little different than numeric pointers in C++. Most of the time the character pointer is used to point to a string literal—since strings are really a sequence of individual characters—so when you try to print the address using a character pointer, you will probably get some unprintable characters
- [Instructor] Character pointers are a little different than numeric pointers in C++. Most of the time the character pointer is used to point to a string literal, since strings are really a sequence of individual characters. So when you try to print the address using a character pointer, you'll probably get some unprintable characters. Let me show you what I mean. In this program here 0202, which is located in your exercise files folder, chapter 2, 0202, starting on line 11 in the main method, I declare a variable called initial.
It's of type character and it's set equal to the character P. Notice the single quotes around the P that indicates a character rather than a string. On line 12, I'm creating a character pointer called P initial, and I'm setting that equal to the address of the initial variable that is of type character. On line 13, I have a message that I'm printing out to the console as well as the P initial variable which is the pointer. Let's run the program. I'm going to use my control F5 to run, and you can see at the top memory address for initial P has some strange characters there.
That is because the compiler is expecting us to want to print out the actual initial as a string, because characters usually point to strings. So if we want to get the address of a single character, we actually have to use what's called casting. We have to cast the P initial pointer. That way we'll be able to see the correct address. So in front of P initial, I'm going to go ahead and say void asterik in parentheses and then P initial after that.
This time when I run the program, it should print out the address. Let's try again. And there's the hex address for the memory address of my initial P. 008FFE1F. There is two ways to do the casting here, let me show you. I'll take the comments out of the next line, line 14. So in order to cast my character pointer as an address, I have to use either the parentheses void asterik closed parentheses, or I can use static underscore cast, and in angle brackets, I can do void space asterik.
That tells the compiler that I want to print out the pointer value, and I'm not trying to print out a string located at that specific address. Let's run it again. As you can see, the address for both of these is the same because they're both pointing to the same character. When the character pointer is used to point to the start of a string literal, it assigns the starting address as the first character in that string literal which is an array. The string literal is stored automatically as a null terminated string literal, which means that a null character is added automatically to the character array as the last character.
Since strings are immutable, it is a good programming practice to create the pointer as a constant pointer using the key word C-O-N-S-T. Let me close my output window so we can see more of the program. This time I'm going to comment out the top part here for my character. And I'm going to uncomment the bottom portion of the program.
You can see here on line 16, I'm using the key word C-O-N-S-T to represent constant character pointer. It's called P answer one, or P a n s one, and it has a value of absolutely yes. By assigning the pointer as a constant, it syncs the constant string literal with the constant pointer. To output a string defined using a character pointer, simply use the pointer name. You don't need to use the de-reference symbol. This does not apply to numeric pointers though, they must be de-referenced or the address value will be printed.
Let's take a closer look at this program. First I'm going to go ahead and run it. Basically the program will ask the user to think of a yes/no question, they'll type in a number between one and eight, and then depending on what number they chose, it'll print out an answer. It's kind of like a magic eight ball if you've ever seen one of those. Okay, I'm going to run the program. And it says think of a yes/no question, then ask the oracle. So think of a question in your mind that's a yes/no question. I'm going to choose the number four. And when I do, it says the oracle says the outlook is good.
Okay, let's take a closer look at the program. So you can see here on line 16 through 24, I'm using constant character pointers to represent the eight different possible answers as well as a part of the answer that says the oracle says on line 24. Now notice, even though it says character pointer, inside the double quotes, I have a string literal. Remember, because the character pointer is pointing to the first character that it can be a string literal.
So for example, on line 16, the character pointer P A N S 1, is actually pointing to the A in the absolutely yes. Just to round out the program, let me scroll down a little bit more. On line 25 I declared integer called choice, on lines 27 and 28 I asked the user to think of a question, and then I asked them to enter a number between one and eight. On line 29 I used the C in to read their value. And then I have a switch statement. The switch statement allows me to determine what number the user entered.
It concatenates the P answer which is the part that says the oracle says. Along with whatever number they chose one through eight. And if they happen to type in something other than one through eight, it's going to give them a default message saying, sorry you didn't choose a number between one and eight. Notice in the C out statements though, for both P A N S and P A N S 1 2 3 4 et cetera, I did not use the de-reference symbol the asterisk, to be able to print out the message. All I have to do, is put the name of the variable.
It will not print the address, it'll actually print the string literal. So we can run it one more time. Let's say I put nine by accident, and I got a message saying it didn't choose a number between one and eight. And if I run it again, and this time I'll say one it says the oracle says absolutely yes. So you can see, let me scroll back up, that there is a difference between using the character pointer to point to an individual character, but you can also use the character pointer to point to a string literal.
It is important to use a constant key word for the character pointer, if you are using a string literal. Because strings are immutable, and that way the pointer is immutable as well.
- Addresses vs. pointers
- Understanding the relationship between pointers and arrays
- Dereferencing pointers
- Passing pointers as arguments
- Using new and delete operators