Character pointers and integer pointers (++)
I have two pointers,
char *str1;
int *str2;
If I look at the size of both the pointers let’s assume
str1=4 bytes
str2=4 bytes
str1++ will increment by 1 byte, but if str2++ it will increment 4 bytes.
开发者_JAVA技巧What is the concept behind this?
Simple, in the provided scenario:
- char is 1 byte long
- int (in your platform) is 4 bytes long
The ++ operator increments the pointer by the size of the pointed type.
When doing arithmetic on a pointer, it's always in terms of the objects pointed at, not in bytes.
So a pointer whose target object is e.g. four bytes, will increase it's actual numerical value by four when you add one.
This is much more usable, and makes far more sense than having all pointer arithmetic be in bytes.
A char
is 1 byte, an int
is (typically) 4 bytes. When you increment a pointer, you increment by the size of the data being pointed to. So, when you increment a char*
, you increment by 1 byte, but when you increment an int*
, you increment 4 bytes.
Pointer actualy holds the address of the memory location, which is 4bytes integer. str1 points to a location that holds 1byte, so if you increase the address of the str1 it jumps to next address of 1byte data. But in other case, str2 points to a 4byte data, so if you increase that address, it must jump over that data to get to the next 4byte data, so it incremets by 4.
This is how 1 byte sequence of data is stored in memmory:
ADDRESS: FF334400 FF334401 FF334402 FF334403
DATA (1BYTE): 1 2 3 4
So if str1 wants to point to the number 2, it must hold its address, which is FF334401. If you increase str1, it must jump over the 2s address and get to 3, and to do that it must be incremented by 1.
In other case:
ADDRESS: FF334400 FF334401 FF334402 FF334403 FF334404 ... FF334407
DATA (4BYTE): 0 0 0 1 0 2
Now if str2 points to the number 1 which is integer, and it actualy is 4byte data, it points to the begining of that data, which is address FF334400. When you increase it, it must jump over all 4 bytes of the 1s data to get to the 2s data, so it increases by 4 and its address becomes FF334404 which is the first byte of the 4byte data of the number 2.
Hint: p[i]
is shorthand for *(p + i)
.
Because this behaviour is more useful than the alternative, and allows you not to care how large a particular data type is.
Consider an array and an integer pointer to that array:
int p[10];
int *q = p;
Then *(q+1) is the same as p[1], i.e. the next int
in memory. It would be far less useful if it just pointed one byte ahead.
Pointer increment always increases the address it points to by the size of the type represented by it. So, for char pointer it increments by 1 and for integer by 4. But, pointer variable itself will require 4 bytes to hold the address.
You can inturn think of how array indexing works. Incase of an integer array a[0] will point to first element and a[1] will point to second. In this case for increment of 1 it should increment by 4 bytes to access the next integer. In case of characters it has to be 1. The same concept works for all pointer arithemtic.
A pointer is an abstraction that allows you to reference data in memory so that the raw memory is accessed as an atomic unit to ensure it interpreted appropriately for the type chosen.
A pointer itself is represented by the native machine word size. In your example, you have two pointers to different types, but they are still both pointers to addresses in memory and hence that is why they are the same size. As mentioned in other answers, to obtain the size of the data type referred to by the pointer, you have to dereference it in the sizeof operation e.g. sizeof(*p).
The ++ operator allows you to obtain a pointer that references the next address in memory for the appropriate type. If it simply incremented the address by one byte for all types you could end up with an address in memory that points into the middle of a data representation
e.g. for two unsigned 4-byte integers representing decimal values of 1 and 4,278,190,080 respectively, starting at address 0x00 in memory (note the address here are just for illustration and not representative of a real system since the OS would reserve them for it's own purposes)
address 0x00 0x01 0x02 0x03 | 0x04 0x05 0x06 0x07
data value (4 byte integer) 0x00 0x00 0x00 0x01 | 0xFF 0x00 0x00 0x00
If a pointer to integer has a reference to address 0x00 and operator ++ just incremented the pointer address by 1 byte, you would have a pointer to address 0x01 which if you then accessed that address (plus the subsequent 3 bytes) as an integer you would get an integer that is represent by the data bytes 0x00 0x00 0x01 plus the value of address 0x04 which in this case is the value 0xFF. This would lead to an integer with a decimal value of 511 which is does not represent either of the 2 integers stored in memory.
To correctly access the next integer in memory, operator ++ has to increment the byte address of the pointer by 4 bytes.
Simple. It depends upon the compiler.
If int has 4 bytes of size when you add 1 to its pointer, it will add its size to it, that is, for and if int is of 2 bytes then it will add 2 that is the size into a pointer. For example, in Turbo C++
int *str = NULL;
str + 1; //It will add 2 as Turbo C++ has int size 2 bytes
In Visual C++, str+1; //It will add 4 as Visual C++ has int size 4 bytes.
And the same is the case with char.
It's according to pointer arithmetic. Here it goes..
As you said, the memory allocated for all pointers is the same. But when you use the increment operator with a pointer variable, then it means to say the pointer should be made to point (increment) to the next location in memory.
So, if you are using a character pointer, then if you increment you wanted to point to the next character which is one byte wide. Similarly, if you want to increment a integer pointer, then it is like asking it to point to next integer which is four bytes wide.
I think this suffices to clarify your question :)
精彩评论