Last active
August 26, 2020 07:18
-
-
Save Mai-Lapyst/4001d416627d55cdbab313b0fa5d6214 to your computer and use it in GitHub Desktop.
Example what different types of pointer does with the underlaying addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
struct myStruct { | |
uint32_t a; | |
uint32_t b; | |
uint32_t c; | |
uint32_t d; | |
}; | |
int main(void) { | |
// this only gives us some data to play with, | |
// since we are in userspace we cannot simply use address how we like... | |
//void* data = malloc(1000); | |
uint8_t* ptr1 = (uint8_t*)0x100; | |
uint16_t* ptr2 = (uint16_t*)0x100; | |
uint32_t* ptr3 = (uint32_t*)0x100; | |
struct myStruct* ptr4 = (struct myStruct*)0x100; | |
void* ptr5 = (void*)0x100; | |
printf("start addresses:\n"); | |
printf("ptr1 = %p\n", ptr1); | |
printf("ptr2 = %p\n", ptr2); | |
printf("ptr3 = %p\n", ptr3); | |
printf("ptr4 = %p\n", ptr4); | |
printf("ptr5 = %p\n", ptr5); | |
ptr1 += 1; | |
ptr2 += 1; | |
ptr3 += 1; | |
ptr4 += 1; | |
ptr5 += 1; | |
printf("addresses after we add 1:\n"); | |
printf("ptr1 = %p\n", ptr1); | |
printf("ptr2 = %p\n", ptr2); | |
printf("ptr3 = %p\n", ptr3); | |
printf("ptr4 = %p\n", ptr4); | |
printf("ptr5 = %p\n", ptr5); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment