Created
April 26, 2021 15:08
-
-
Save rscarrera27/d897f433e60af874219e0356ecb8c549 to your computer and use it in GitHub Desktop.
Fat pointer of pascal string
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 <string.h> | |
#include <stdlib.h> | |
struct Pstr { | |
int size; | |
char* str; | |
}; | |
char** pstr_new() | |
{ | |
struct Pstr* p = malloc(sizeof(struct Pstr)); | |
p->size = 11; | |
p->str = "Hello world"; | |
return &(p->str); | |
} | |
int main() | |
{ | |
char** pstr = pstr_new(); | |
int size = *(int*)((unsigned long)pstr - 8); // only for LP64 arch | |
printf("pstr->str: %s - pstr->size: %d", *pstr, size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment