Skip to content

Instantly share code, notes, and snippets.

@andreabadesso
Created April 7, 2014 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreabadesso/10021959 to your computer and use it in GitHub Desktop.
Save andreabadesso/10021959 to your computer and use it in GitHub Desktop.
Trying to access struct members knowing the data format
int codifica(void* valores, char *campos) {
char tipo;
int acc = 0;
int acc2 = 0;
int i;
for (i=0;i<=strlen(campos);i++) {
tipo = campos[i];
// p = *(int*)valores;
// v = (*((int*)valores+1));
// c = (*((char*)valores + sizeof(char*)));
//printf("%d, %d, %c\n", p,v,c);
switch(tipo) {
case 'i':
int i_valor;
i_valor = (*((int*)valores+acc));
printf("endereco: %p\n", ((((int*)valores+acc))));
printf("valor: %d\n", i_valor);
acc += 1;
acc2 += 4;
break;
case 's':
char* c_valor;
printf("endereco: %p\n", ((((char*)valores+acc))));
c_valor = &(*((char*)valores+acc2));
printf("valor: %s\n", c_valor);
acc += sizeof(c_valor);
// Fazer alguma coisa
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment