Created
September 5, 2013 05:39
-
-
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
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
#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