#include <stdio.h>structperson{Char*name; intAge ;};intMain () {//A struct can be defined within a function, or it can be defined outside a function//equivalent to global variables and local variables//struct person// {//Char *name;//int age;// }; structPerson p1; //A padded algorithm that allocates storage space as an integer multiple of the memory occupied by the largest member of memory in the structure//The most memory-occupying member in the person struct is a pointer, taking up 8 bytes,//another member int occupies 4 memory,//so the struct person defines a variable that consumes 16 bytes of memoryprintf"sizeof (p1) =%lu\n",sizeof(p1)); P1.name="Jack"; P1.age= -; printf ("p1.name =%s\n", P1.name); printf ("p1.age =%d\n", P1.age); structperson P2 = {"Rose", -}; printf ("p2.name =%s\n", P2.name); printf ("p2.age =%d\n", P2.age); /*An incorrect assignment struct can only be assigned with curly braces at initialization time, which is similar to an array where P3 has allocated memory addresses, P3 is an address constant, and is a struct person p that cannot be reassigned; p={"Name", 10}; */ return 0;}
Results
Definition, initialization and memory allocation of structure in C language