I know that integer pointer will dereference 4 bytes, then what signed and unsigned do with pointers ? what is the meaning of "unsigned int *ptr" and "signed int *ptr"
Type of pointer suggests what kind of variables address it can keep. So, unsigned int *ptr
should keep address of unsigned int
and signed int *ptr
should keep address of signed int
Please look at following piece of code,
int main()
{
unsigned int * ptr1;
signed int * ptr2;
unsigned int i;
signed int s;
ptr1 = &s;
ptr2 = &i;
}
It gives me following errors in Visual Studio,
Error 1 error C2440: '=' : cannot convert from 'int *' to 'unsigned int *' [...]
Error 2 error C2440: '=' : cannot convert from 'unsigned int *' to 'int *' [...]
When you have
unsigned int* ptr;
the value of ptr
is assumed to hold an unsigned int
. Similarly, when you have
signed int* ptr;
the value of ptr
is assumed to hold an signed int
.
Here's a simple experiment:
#include <stdio.h>
int main()
{
signed int i = -10;
signed int* p1 = &i;
unsigned int* p2 = (unsigned int*)p1;
printf("Value of p1: 0x%p\n", p1);
printf("Value of p2: 0x%p\n", p2);
printf("Value of *p1: %d\n", *p1);
printf("Value of *p2: %u\n", *p2);
}
A sample output of that program:
Value of p1: 0x0x7fff1b52bd6c Value of p2: 0x0x7fff1b52bd6c Value of *p1: -10 Value of *p2: 4294967286
Even though the numeric value of the address held by p1
and p2
are same, when dereferenced, they evaluate to vastly different values.
A pointer, in C/C++, is nothing more than a memory location; their sizes are the same for a given architecture. The type of pointer such as void *, int *, unsigned int *, etc... tells the compiler how to handle pointer math.
Visualizing it this way always helps me...
char *ptrMyString = "test";
int *ptrMyINTS[] = {1,2,3,4}
The memory for each of the arrays above looks like this:
Offsets 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ptrMyString = [t] [e] [s] [t] [0]
ptrMyINTS = [0] [0] [0] [1] [0] [0] [0] [2] [0] [0] [0] [3] [0] [0] [0] [4]
sizeof(ptrMyString) should be equal to sizeof(ptrMyINTS) since they are just memory pointers. However, ptrMyString++ will increment by 1 where ptrMYINTS will increment by 4.
Now I'm over simplifying a lot of things and ignoring stuff like virtual memory but the basic idea is, for example, if both pointers start at 0x00000000 (32bit address) then the pointer's value for each offset would be:
ptrMyString+0 = 0x00000000 = t
ptrMyString+1 = 0x00000001 = e
ptrMyString+2 = 0x00000002 = s
ptrMyString+3 = 0x00000003 = t
ptrMyINTS+0 = 0x00000000 = 1
ptrMyINTS+1 = 0x00000004 = 2
ptrMyINTS+2 = 0x00000008 = 3
ptrMyINTS+3 = 0x0000000C = 4
Notice the size is the same for all locations but the amount we increment by, 1 and 4, comes from the type of pointers, char * and int * respectively. I made the assumption that an int is 4 bytes here because it is on 32 bit X86 and ARM architectures.
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.