简体   繁体   中英

Storing Address of a character to an Integer Pointer

As we know that the task of a pointer (In C++) is to store an address. The size of address depends on the computer architecture . For this example let us assume that we have a 32-bit machine and the size of address is 4-Byte . Now if we create an Integer pointer

int x=20;
int * ptr;
ptr = &x;    //  This is fine the address of x is of 4-Bytes

Similarly if we have a character c

char c='a';
ptr = &c;      // This puts an error although address size of &c is still 4-Byte.

The task of the pointer is to store an address then why ptr do not store the address of a character. Although the address for both the integer and a character is of 4-Bytes.There is no datatype of address then should it not save the address in ptr (pointer) ?

The issue is not that the pointer variable is the wrong size. The issue is one of type safety. You are declaring that the pointer variable contains the address of an int . Which means that when you try to pass the address of a char , the compiler complains. It says, "you promised to give me the address of an int, but that is the address of a char".

The compiler is not trying to protect you against problems with storing the address. As you note, there are none. The problem that the compiler is protecting you from is that which arises when you de-reference the address.

Just think what would happen when you try to dereference the pointer. If

int * ptr;
char c='a';
ptr = &c;

worked, what would

int d = *ptr;

put in d ? In fact, since an int is 4 bytes but a char is only 1, reading from d would produce garbage and writing to it would contaminate the subsequent 3 bytes.

If you define ptr in your second case as

char *ptr;

then that will compile. The pointer is still, as you said, 4 bytes on a 32-bit architecture. The reason for writing char * is just to clarify that your pointer is pointing to a char type. This makes your code less buggy, it makes it possible to easily point to structure/class members etc.

In theory you could of course cast your char-pointer to an int-pointer, they're both pointers. But this would not be correct as the data you're pointing to is not an integer.

Basically to ensure some kind of type safety , even if void* always ruins the party. As you can see in this little example, a pointer to void does what you are looking for:

int main() {
    int x = 0;
    char y = 'a';
    void* ptr = &x;
    ptr = &y;
}

But it's a C-ism, and you should probably use either boost::any or boost::variant to express a variable that can be of different types.

如果您真的想这样做,请尝试强制类型转换...但是我敢打赌,您会遇到很多麻烦...

ptr = (int *)&c;

Because types exist.

If this were allowed:

char x   = '!';
int* ptr = &x;

then the type of the expression *ptr would be int , which is wrong because the pointer is pointing to a char , not an int .

char c='a';
int * ptr;
ptr = &c;
printf("%d",*ptr);

The above gives you garbage value for *ptr in the output of print because we are declaring that the pointer variable contains the address of an int. Which means that when you try to pass the address of a char then the compiler says that the pointer is integer type so we have to pass integer address only but we are passing the address of a char which is 1 byte but integer pointer points to 4 bytes address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM