Skip to content

Instantly share code, notes, and snippets.

@hholst80
Created September 2, 2015 20:50
Show Gist options
  • Save hholst80/c1daffad38fd19f06071 to your computer and use it in GitHub Desktop.
Save hholst80/c1daffad38fd19f06071 to your computer and use it in GitHub Desktop.
/* 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);
}
@hugows
Copy link

hugows commented Sep 3, 2015

Can you explain what you would use this for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment