[Xcode C-5] structure, global local variables and enumeration variable knowledge

Source: Internet
Author: User

I. global variables and local variables


(1) global variables are defined outside the function, and local variables are in the function body. They have different scopes and lifecycles.


2. struct


(1) It defines that the struct will not allocate memory. Only when the struct defines a variable will it allocate memory for this variable. The address of the first member is the address of the struct variable.

Int main (INT argc, const char * argv []) {// structure format struct person {int age; char * Name ;}; // defines the variable struct person P1; // assign values. There are multiple assign values, such as struct person P2 = {10, "Tom"}; p1.age = 28; p1.name = "Andy "; // access printf ("% s \ n", p1.name); Return 0 ;}

(2) The allocated space is a multiple of the largest byte member variables. For example, in the above struct, Int Is 4 bytes, char * is 8 bytes, total is 12 bytes, but to be an integer multiple of the maximum byte member, each struct variable is 16 bytes in total.


(3) There are many other ways to define struct variables. In addition to the above method, you can add a P before the semicolon to define a variable while defining the struct. Another method is the anonymous struct, which can only define variables at one time, because it cannot be reused in the future.


(4) struct array. It is defined and initialized in batches when struct variables are defined. For example, struct person P [3] = {{},{},{}};. But it is rarely used. Struct Arrays can only be initialized when they are defined.


(5) The struct is the same as the array. Variables cannot be defined first and then assigned values.


(6) pointer to the struct. Because it is directed to the struct, the definition method is struct person * P. P = & P1; point this pointer to P1. The key is how to set the value. (* P). Age can be used, but p-> age is more common.


(7) struct, of course, can be nested.


(8) The largest feature of struct compared to arrays is that it can store different types of values, while Arrays can only be the same type of values.


Iii. Enumeration Variables

Int main (INT argc, const char * argv []) {// do not use a value. All values are devil values. // int gender = 0; // define an enumeration // we can give man = 100, then the following value is incremented at a time, that is, woman is 101 Enum gender {Man, woman }; // use and define an enumerated variable gender1 Enum gender gender1 = man; return 0 ;}


[Xcode C-5] structure, global local variables and enumeration variable knowledge

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: info-contact@alibabacloud.com 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.