Character pointers and integer pointers (++)

前端 未结 10 1268
终归单人心
终归单人心 2020-11-29 11:50

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 byt         


        
相关标签:
10条回答
  • 2020-11-29 12:09

    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.

    0 讨论(0)
  • 2020-11-29 12:14

    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.

    0 讨论(0)
  • 2020-11-29 12:17

    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.

    0 讨论(0)
  • 2020-11-29 12:18

    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.

    0 讨论(0)
  • 2020-11-29 12:18

    Hint: p[i] is shorthand for *(p + i).

    0 讨论(0)
  • 2020-11-29 12:19

    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 :)

    0 讨论(0)
提交回复
热议问题