Skip to content

Instantly share code, notes, and snippets.

@rscarrera27
Created April 26, 2021 15:08
Show Gist options
  • Save rscarrera27/d897f433e60af874219e0356ecb8c549 to your computer and use it in GitHub Desktop.
Save rscarrera27/d897f433e60af874219e0356ecb8c549 to your computer and use it in GitHub Desktop.
Fat pointer of pascal string
#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