Help - Pointers

Hi, beginner over here.
I just started out with pointers and I have simple of understanding of how pointers work, they basically point to the address of an object. However what I could not understand is how the pointers work with 'char' data type.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

void main()
{
	char c;
	char *p;
	p = &c;
	cout << p;
	cin.get();
}


My Question - Why doesn't the program print the address of 'c'. To Display the address of 'c', I have to write cout << &p;
But this is not the case with int or float, writing cout << p; prints the address of the variable.

Please Help.
Firstly, main should return an int.

Secondly, what output are you getting?
As Far as I know it doesn't really matter if main returns a int.
as for for output im getting a mess of special characters starting with the NULL

but as u asked i changed main to return int, doesn't make a difference
Good question, I've never noticed this before. The answer can be found here:

http://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed

Ps. Writing cout << &p writes the address of the char* pointer to the console screen, not the address of the char c.
Last edited on
As Far as I know it doesn't really matter if main returns a int.


It is fine if you explicitly do not 'return' a value from main. However, the main function itself should be of type int main() so as to enable the compiler to return a default value.

Following link explains the answer to your question:
http://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed
@R10111001

Yeah I Realized That by the pointer, pointing to a pointer method.
the link does help but it creates more questions than answers, Eg: why does << operator treat char as string, what is static_ cast.
I guess its best to leave it until I come across it in Class

Thanks everybody for helping
why does << operator treat char as string


So that cout << "Some text\n"; works as expected. We don't want it's address in the output stream. We want the actual text.
Topic archived. No new replies allowed.