I was learning about Structured Data and I have found some plenty of weird code. Here is the simplest code I have found, so everyone can understand the concepts.#include struct person { char *name; int age; }; int main() { struct person p; p.name = "John Smith"; p.age = 25; printf("%s",p.name); printf("%d",p.age); return 0; }Additionally, I have wrote this more elaborated code (based on the ebook Learning GNU C from Ciarán O'Riordan):#include struct Person { char *name; int age; int height_in_cm; }; int main() { struct Person hero = {"Robin Hood",20, 191 }; struct Person sidekick; sidekick.age = 31; sidekick.name = "John Little"; sidekick.height_in_cm = 237; printf("%s is %d years old and stands %dcm tall in his socks\n", sidekick.name, sidekick.age, sidekick.height_in_cm); printf("He is often seen with %s. \n", hero.name); return 0; }If you are on Linux, just do it to compile: #gcc -Wall -o code code.c
Advertisement