I have a small school assignment. My output is not correct. Can you see if I’m doing anything wrong?
//1. Create a char pointer named cp
char *cp;
//2. Dynamically allocate a char and store its address in cp
char dynamicChar = 'A';
cp = &dynamicChar;
//3. Write the character 'x' into the char
*cp = 'x';
//4. Print out the value of the char using cout
cout << cp << endl;
The print out statement prints A@@ instead of just A. I’m not sure what I’m doing wrong.
Try
You want to print the character, not the pointer to it. When you hand it the pointer,
coutthinks you’re telling it to print out a character array starting there.