Char Pointers

Hi all. This is probably an easy one for you guys. Why is it that when I declare a pointer variable that points to an 'int', the pointer variable stores the address of the 'int' that it points to. However when I declare a pointer that points to a 'char' variable, the pointer variable appears to store the value stored in the 'char' it points to...not its address. My code below should help explain what I mean.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;


int main(int nNumberofArgs, char* pszArgs[])
{

    int nNum = 5;
    int * pnNum = &nNum;

    char cLetter = 'E';
    char * pcLetter = &cLetter;

    cout << "nNum is: <" << nNum << ">" << endl;
    cout << "pnNum points to nNum and gives: <" << pnNum << ">" << endl;

    cout << "cLetter is: <" << cLetter << ">" << endl;
    cout << "pcLetter points to cLetter and gives: <" << pcLetter << ">" <<  endl;

    system("PAUSE");
    return 0;

}


When I run this, it displays the address of pcNum, but not of pcLetter. Help much appreciated!
The extraction operators have a special function for only char* that prints the data pointed to by the pointer until it reaches a null character ('\0'). This is for compatibility with C-style strings. Cast the char* to something else to see the actual address.
Like Zhuge said, cast it to an integer.
thanks a lot for the quick responses guys, much appreciated. I don't need to know for any real purpose, just more curiousity. However, I can't change the char* to an int* since it is pointing to a char not an int right?
Don't cast it to an int*, cast it to an int. As a pointer is always a number, casting it to another number type always works.
I wouldn't cast it to an int* or an int.

I would cast it to a void*

Topic archived. No new replies allowed.