Home > Back-end >  how to access the i element of the void* string?
how to access the i element of the void* string?

Time:03-09

i have

void *string; // it is array of characters different encodings

i read it with my func, after that i want to print it, all good, it prints right. and after that i want to print i character (first for example), how can i access him?

string=readline();
printf("%s\n", string);
printf("%c\n", ??? );

i tried this, but it doesn't work

string=readline();
printf("%s\n", string);
printf("%c\n", string   sizeof(void *);

CodePudding user response:

If the data is representable as char then simply cast and de-reference:

printf("%c\n", *(char*)string);

Or alternatively:

char* str = string;
...
printf("%c\n", str[i]);
  •  Tags:  
  • c
  • Related