In-Depth Discussion on Pointer Operations and Address Offsets in C

Question: When performing pointer + operations, how much does the address actually offset? This question is essentially a fundamental issue in C programming. However, I have stumbled on this as well, so I write this as a lesson and a reminder for everyone.
We know that the essence of a pointer is a memory address, and when a pointer performs a + operation, the address offset depends on the type of the pointer.
Here is an example:
#include <stdio.h>
#include <stdint.h>
uint8_t Arg_A[12] = {0x00};
uint16_t Arg_B[12] = {0x00};
uint32_t Arg_C[12] = {0x00};
int main(){
    printf("Arg_A Address = %p\n", Arg_A);
    printf("Arg_A Address add 2 = %p\n\n", (Arg_A+2));
    printf("Arg_B Address = %p\n", Arg_B);
    printf("Arg_B Address add 2 = %p\n\n", (Arg_B+2));
    printf("Arg_C Address = %p\n", Arg_C);
    printf("Arg_C Address add 2 = %p\n\n", (Arg_C+2));
    return 0;
}
The output is as follows:
Arg_A Address = 0x56aca32d9040
Arg_A Address add 2 = 0x56aca32d9042
Arg_B Address = 0x56aca32d9050
Arg_B Address add 2 = 0x56aca32d9054
Arg_C Address = 0x56aca32d9080
Arg_C Address add 2 = 0x56aca32d9088
The above example clearly shows that when a pointer performs a + operation, the amount of address offset is related to the initially defined pointer type, e.g., for uint8_t* p, p+2 offsets by two bytes, for uint16_t* p, p+2 offsets by four bytes.
Of course, in practical engineering, the use of pointers is far from this simple; when defining a data module or data object, multiple data types are generally used.
Let’s slightly modify the above program, as shown below:
#include <stdio.h>
#include <stdint.h>
typedef struct{
    uint8_t Arg_A;
    uint16_t Arg_B;
    uint32_t Arg_C;
    uint32_t* Arg_D;
}str_Type;
str_Type Arg_Test = {0x00};
int main(){
        printf("Arg_Test Address = %p\n", &Arg_Test);
    printf("Arg_Test Address add 2 = %p\n\n", (&Arg_Test+2));
    return 0;
}
The output is as follows:
Arg_Test Address = 0x5e9e5c0e1020
Arg_Test Address add 2 = 0x5e9e5c0e1040

&Arg_Test+2 why does it offset by 32 bytes? This is because my machine defaults to 4-byte alignment, meaning that each element in the str_Type structure occupies 4 bytes by default, so &Arg_Test increments by 16 bytes each time.

Even with such a basic concept, when writing code, one can occasionally make mistakes, overly assuming (believing that the address has produced a 2-byte offset), and I wish to encourage everyone together.

Previous Highlights

How to avoid cache causing data inconsistency between cores?
Information Security: Why use AES-GCM?
Debugging Techniques: How to convert JTAG to DAP?
How to diagnose ground short circuits, power short circuits, and open circuit faults?
Information Security: How to set passwords and unlock hardware debug ports?
An IPC communication solution for multi-core communication
Based on Davinci: HSM Development Practice (12): Engineering Development Practice of Security Logs (Event Log)

Click below to follow, let’s talk about Autosar/embedded systems. If needed, contact the author to join the group for more professional answers.

Leave a Comment