Printing address of a char with a pointer

Hey, why does this code return a set of really weird squigly characters as the address of a variable of type char?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <conio.h>

int main(){

	std::cout << "Pointers example:" << std::endl;
	_getch();
	std::cout << "Enter a character to store as a variable. " << std::endl;
	std::cout << "If a string is entered the first letter only will be stored: " << std::endl;
	char var;
	std::cin >> var;
	std::cout << "You entered '" << var << "'." << std::endl;
	char* varloc;
	varloc = &var;
	std::cout << "It is being stored at: " << varloc << "." << std::endl;
	std::cout << std::endl;
	std::cout << std::endl;
	
}
Last edited on
Line 16 you need a * before varloc to dereferance it such as *varloc
- No he doesn't. Doing so will display the value at the address of "varloc", which is var.

- What you have should work correctly AFAIK.
now it just prints the value of the variable?

I rewrote line 16 like this:

std::cout << "It is being stored at: " << *varloc << "." << std::endl;

I enter 'f' and it says:

'It is being stored at: f.'
http://imgur.com/ilSBHI6

Here's a picture
- That's because you had it correct the first time. That user that said to dereference the variable was incorrect.
- Why it's displaying special characters, i'm not sure. However, the method you are using is correct AFAIK.
ok, thanks, i've gone back to my original code.

i also just wrote
1
2
3
4

char letter = 's';
cout << &s << endl;


and got the same weird characters, perhaps it's something to do with visual studio?
- I'm not sure, as it just happened in codeblocks for me.

- I'll probably make a project and try it again.
No, cout is simply overloaded to presume char*'s are C-style strings and print the contents rather than the address. To get the address stored in the char*, cast it to another pointer type then print it out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
	std::cout << "Pointers example:" << std::endl;

	char var = 'A';
	char* varloc = &var;
	
	std::cout << "  (char *)  : " <<  (char *) varloc <<  std::endl;
	std::cout << "  (void *)  : " <<  (void *) varloc <<  std::endl;

    return 0;	
}

Output:
Pointers example:
  (char *)  : A; #
  (void *)  : 0x23ff3b
Chervil would you be able to explain that code to me?
cheers
It's just an example of what Zhuge explained above, "cast it to another pointer type then print it out."
i meant how you wrote
(char *) and it didn't work, but then you wrote (void *) and it did?
why is that?
The code i wrote was a bit odd. I put (char *) simply for comparison purposes. In fact if that part is omitted, the behaviour is exactly the same, since the variable was already a pointer to char.

It didn't work because, as Zhuge said, cout is overloaded to presume char*'s are C-style strings and print the contents rather than the address.

The word "overloaded" here is important. It refers to a function which has multiple versions with the same name but a different type of parameter.

For example, you could have two functions called add:
1
2
int add(int x, int y) { return x + y ; }
double add(double x, double y) { return x + y ; }

When you call the function, the compiler chooses which one to call, depending upon the type of parameter.

In the case of cout << if the parameter is a pointer to char, the compiler assumes this is a character string, and so tries to print out the string.

If the parameter is a pointer to void (that is, a general pointer to no specific type), the compiler chooses a different version, which prints the value of the pointer itself.
so it takes the address of the variable as a string, as oppose to just printing out the address?

thanks for the help :)
Last edited on
Topic archived. No new replies allowed.