Created
September 2, 2015 20:50
-
-
Save hholst80/c1daffad38fd19f06071 to your computer and use it in GitHub Desktop.
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
/* strtab.c */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
char *base = ""; | |
#define STRING_ADDRESS(id) (base+id) | |
#define STRING_ID(address) (signed short)((address)-base) | |
signed short foo(void) | |
{ | |
char *c = "test"; | |
return STRING_ID(c); | |
} | |
signed short bar(void); | |
int main(void) | |
{ | |
char *a = "hello"; | |
char *b = "world"; | |
int id_a = STRING_ID(a); | |
int id_b = STRING_ID(b); | |
int id_c = foo(); | |
int id_d = bar(); | |
printf("string(%d)=%p (%s)\n", id_a, STRING_ADDRESS(id_a), STRING_ADDRESS(id_a)); | |
printf("string(%d)=%p (%s)\n", id_b, STRING_ADDRESS(id_b), STRING_ADDRESS(id_b)); | |
printf("string(%d)=%p (%s)\n", id_c, STRING_ADDRESS(id_c), STRING_ADDRESS(id_c)); | |
printf("string(%d)=%p (%s)\n", id_d, STRING_ADDRESS(id_d), STRING_ADDRESS(id_d)); | |
return 0; | |
} | |
/* bar.c */ | |
extern char *base; | |
#define STRING_ADDRESS(id) (base+id) | |
#define STRING_ID(address) (signed short)((address)-base) | |
signed short bar(void) | |
{ | |
char *d = "barstring"; | |
return STRING_ID(d); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you explain what you would use this for?