Skip to content

Instantly share code, notes, and snippets.

@arafatkamaal
Created September 5, 2013 05:39
Show Gist options
  • Save arafatkamaal/6446426 to your computer and use it in GitHub Desktop.
Save arafatkamaal/6446426 to your computer and use it in GitHub Desktop.
How to create a character array from a struct, and vice a versa
#include <stdio.h>
#include <string.h>
typedef struct teststructure {
int valid;
char key[10];
char value[10];
}test;
void print_struct_value(char* structarray);
int main(){
test ts;
ts.valid = 1;
strncpy(ts.key,"hello\0",5);
strncpy(ts.value,"bye\0",3);
char* struct_array_converted = (char*) &ts;
print_struct_value(struct_array_converted);
return 0;
}
void print_struct_value(char* structarray){
test *tsr;
tsr = (test*) structarray;
printf("The valid value is %d\n", tsr->valid);
printf("The key is %s\n", tsr->key);
printf("The value is %s\n", tsr->value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment