The path of IOS development--c language construction type _ios

Source: Internet
Author: User
Tags arrays constant reserved

Overview

In the first section we refer to the construction type of C, which is divided into: arrays, structs, enumerations, and common bodies, of course, the contents of the previous array have been said a lot, this section will focus on the other three types.

Structure body Enumeration Common body

Structural body

Arrays are stored in a series of identical data types, if you want a variable to store different data types, you use a struct, which is similar to the definition of classes in C + +, C #, Java, and other high-level languages, but in fact they have a big difference. A struct is a type, not a variable, but this type can be composed of other basic types of C language.


//MAIN.C
//Constructedtype////
Created by Kenshin Cui on 14-7-18.
Copyright (c) 2014 Kenshin Cui. All rights reserved.

#include <stdio.h>

//struct type Date
struct date{
 int year;
 int month;
 int day;

struct person{
 char *name;
 int age;
 struct Date birthday;//Another struct type is used in one structure body, and the struct type variable declaration must precede the struct keyword
 float height;

int main (int argc, const char * argv[]) {
 struct person p={"Kenshin", 28,{1986,8,8},1.72};
 Defines and initializes a struct variable, not allowing initialization to be defined first, for example: struct person p;p={"Kenshin", 28,{1986,8,8},1.72}; is the wrong
 
 printf ("name=%s,age=%d, Birthday=%d-%d-%d,height=%.2f\n ", p.name,p.age,p.birthday.year,p.birthday.month,p.birthday.day,p.height); 
 Results: name=kenshin,age=28,birthday=1986-8-8,height=1.72, the structure is referenced through "struct variables. Member name"
 
 printf ("Len (Date) =%lu,len" ( person) =%lu\n ", sizeof (struct Date), sizeof (struct person)); 
 Result: Len (Date) =12,len (person) =32 return
 
 0;
}

For the above example you need to make the following note:

struct variables can be declared at the same time as the structure type is defined;
If the structure body type is defined at the same time, the structure body name can be omitted;
Defining a struct type does not allocate memory, and allocates memory when defining a struct variable (similar to the base type);
The amount of memory occupied by a struct type is equal to the sum of the memory size of all members (if the memory alignment is not considered);

A description of the 4th need, for example, the above code is the result of running under a 64-bit compiler (int length 4,char length 1,float type 4), date=4+4+4=12. But for a person it's not that simple, because person=8+4+12+4=28 is calculated in the normal way, but the result from the above code is 32, why? Here we have to introduce a concept of "memory alignment", the concept of memory alignment is not detailed here, and what you need to know is that the alignment parameter in Mac OS x defaults to 8 (you can change the alignment parameter by adding a #pragma pack (8) in your code), if the type in the struct is not greater than 8, The length of the structure is the sum of its member types, but if the length of the member variable is greater than the alignment parameter, the result is not necessarily the sum of the member variables. The length of the person type is 32, in fact, because the date type length 12 is offset 12 is not a multiple of 8 at the time of storage, considering the reason for the memory alignment to add 4 completion lengths, here is a table listing the specific reasons:

Table specific sources please watch the video below (note that the reason for recording the software is not clear for a few seconds but does not affect the analysis):

Next look at the structure array and the pointer to the structure:

MAIN.C//Constructedtype////Created by Kenshin Cui on 14-7-18. Copyright (c) 2014 Kenshin Cui.
All rights reserved.
 #include <stdio.h> struct date{int year;
 int month;
int day;

};
 struct person{char *name;
 int age;
 struct Date birthday;
float height;

};

void ChangeValue (struct person) {person.height=1.80;} int main (int argc, const char * argv[]) {struct person persons[]={{"Kenshin", 28,{1986,8,8},1.72}, {"Kaoru", 27,{1987
 , 8,8},1.60}, {"Rosa", 29,{1985,8,8},1.60}}; for (int i=0; i<3; ++i) {printf ("name=%s,age=%d,birthday=%d-%d-%d,height=%.2f\n", Persons[i].name, Persons[i
 ].age, Persons[i].birthday.year, Persons[i].birthday.month, Persons[i].birthday.day, persons[i].height); }/* Output result: name=kenshin,age=28,birthday=1986-8-8,height=1.72 name=kaoru,age=27,birthday=1987-8-8,height=1.60 Name=R
 osa,age=29,birthday=1985-8-8,height=1.60 * * struct person person=persons[0];
 ChangeValue (person); Printf ("name=%s,age=%d,birthday=%d-%d-%d,height=%.2f\n", Persons[0].name, Persons[0].age, persons[0].birthday.year
 , Persons[0].birthday.month, Persons[0].birthday.day, persons[0].height);
 /* Output Results: name=kenshin,age=28,birthday=1986-8-8,height=1.72 * * struct person *p=&person; printf ("name=%s,age=%d,birthday=%d-%d-%d,height=%.2f\n", (*p). Name, (*p). Age, (*p). Birthday.year, (*p). Birthda
 Y.month, (*p). Birthday.day, (*p). height); /* Output Result: name=kenshin,age=28,birthday=1986-8-8,height=1.72/printf ("name=%s,age=%d,birthday=%d-%d-%d,height=%.2f \ n ", P->name, P->age, P->birthday.year, P->birthday.month, P->birthday.day, P->height)
 ;
/* Output Results: name=kenshin,age=28,birthday=1986-8-8,height=1.72 */return 0; }

The struct body passes as a function parameter the value of the member (value pass instead of reference pass), which can be accessed through the "->" operator for the struct pointer.

Enumeration

An enumeration type is a relatively simple data type, in fact an enumeration type is handled as an integer constant in the C language, often referred to as an "enumeration constant."


//MAIN.C
//Constructedtype////
Created by Kenshin Cui on 14-7-18.
Copyright (c) 2014 Kenshin Cui. All rights reserved.

#include <stdio.h>

enum season{//spring=0,summer=1,autumn=2,winter=3
 spring
 by default Summer,
 autumn,
 winter
};

int main (int argc, const char * argv[]) {
 enum Season Season=summer;//enumeration assignment, equivalent to season=1
 printf ("summer=%d\n", Season); Result: Summer=1
 
 for (Season=spring;season<=winter;++season) {
  printf ("Element value=%d\n", season);
 }
 /* Result:
  element value=0
  element value=1 element
  value=2 element value=3/return
 0;
}

Note that the enumeration member defaults start at 0, and if you assign a value to one of the members, the rest of the members are assigned a value, such as the above if summer is manually specified as 8, autumn=9,winter=10, and Sprint is 0.

Common body

The common body is also called the Union, because its key word is union (seemingly database operations often use this keyword), its use is not like the enumeration and the structure is so frequent, but as a data type in C language We also need to understand its usage. From the previous analysis we know that the total length of the structure is equal to the sum of all the members (and, of course, the alignment problem may be encountered at this time), but unlike structs, all members of the shared body share a single piece of memory, which is stored sequentially from a low address, only one member at a time, and the union final size is determined by the largest member of the shared body. , assigning a value to a member may overwrite another member.


//MAIN.C
//Constructedtype////
Created by Kenshin Cui on 14-7-20.
Copyright (c) 2014 Kenshin Cui. All rights reserved.

#include <stdio.h>

Union type{
 char A;
 short int b;
 int c;
};

int main (int argc, const char * argv[]) {
 Union Type t;
 T.a= ' a ';
 t.b=10;
 t.c=65796;
 
 printf ("Address (Type) =%x,address (T.A) =%x,address (t.b) =%x,address (t.c) =%x\n", &t,&t.a,&t.b,&t.c) ;
 Result: Address (Type) =5fbff7b8,address (T.A) =5fbff7b8,address (t.b) =5fbff7b8,address (t.c) =5fbff7b8
 
 printf ("Len (type) =%d\n ", sizeof (Union Type));
 Result: Len (Type) =4
 
 printf ("t.a=%d,t.b=%d,t.c=%d\n", t.a,t.b,t.c);
 Result: t.a=4,t.b=260,t.c=65796 return
 
 0;
}

Here needs to focus on explaining a problem: Why T.a, t.b, t.c output results are 4, 260, 65796, of course, t.c equals 65796 is not surprising, but t.a before the assignment is ' a ' should not be 97, and t.b should not be 10? In fact, if the concept of the common body of the problem is basically clear.

According to the aforementioned, the common body can only use one of the members at a time, for the above code after three times assigned value is actually t.c, and through the above output we do see C is effective. One feature of a common body is that its members are stored in the same area of memory, which depends on the length of the member's maximum length. Because the above code is compiled under the 64-bit compiler, the specific length: Char=1,short int=2,int=4, so concluded that the type length of 4, and according to the output of the above address, you can get the following storage information (note how the data storage: High address storage, Low address storage status):

When reading C, its binary is "00000000 00000001 00000001 00000100", converted to decimal is 65796, and after three assignment, at this time B's storage has been the C member of the low data coverage, B length is two, So the binary data obtained from the starting address at this time is "00000001 00000100" (b the original data is already covered by C-Low 2-bit data, in fact, at this time is the low 2-bit C data), converted to decimal is 260; A similar data at this point is the lower data of C. 00000100 ", conversion into decimal is 4.

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: 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.