Definition, initialization and memory allocation of structure in C language

Source: Internet
Author: User

#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));"Jack"; P1.age= -; printf (" =%s\n",; printf ("p1.age =%d\n", P1.age); structperson P2 = {"Rose", -}; printf (" =%s\n",; 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;}


Definition, initialization and memory allocation of structure in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.