Dark Horse Programmer C language-enumeration, struct, pointer

Source: Internet
Author: User

First, enumerate

※ Definition of enumeration variable

Just before you define the enumeration type, you can then define the variable with the defined enumeration type.

As with structs, there are 3 ways to define enumeration variables

1. Define the enumeration type first, and then define the enumeration variables

Enum Season {Spring, summer, autumn, winter};

Enum Season s;

2. Defining enumeration variables while defining enumeration types

Enum Season {Spring, summer, autumn, winter} s;

3. Omit the enumeration name and define the enumeration variables directly

enum {spring, summer, autumn, winter} s;

All three of the above are defined as enumeration variables s

※ Notice of Use

1. The C language compiler handles enumeration elements (spring, summer, and so on) as integer constants, called enumeration constants.

2. The value of the enumeration element depends on the order in which the enumeration elements are arranged when defined. By default, the first enumeration element has a value of 0, the second is 1, and sequentially adds 1.

Enum Season {Spring, summer, autumn, winter};

That is, spring has a value of 0,summer value of 1,autumn and a value of 2,winter is 3

3. You can also change the value of an enumeration element when you define an enumeration type

Enum season {spring, summer=3, Autumn, winter};

An enumeration element with no value specified, with a value of 1 for the previous element. It is also said that the value of spring is 0,summer value of 3,autumn is 4,winter value is 5

second, the structural body

※ Definition of structural body

The elements within the structure, that is, the constituent components, are generally referred to as "members".

The general definition of a struct is:

struct struct body name {

Type name 1 member name 1;

Type Name 2 member name 2;

...

Type name n member name N;

};

A struct is a keyword, a symbol of a struct type.

※ Definition of structural variables

1. The previous definition of a struct type with the name student does not define a struct variable, just like an int, but a type.

Next, define a struct variable, in many ways.

struct Student {
Char *name;
int age;
};

struct Student stu;

2. Define variables while defining struct type

struct Student {

Char *name;

int age;

} Stu;

3. Directly define struct type variable, omit type name

struct {

Char *name;

int age;

} Stu;

※ The attention point of the structure body

1. Recursive definition of struct itself is not allowed

2. Structure body can contain other structures

struct Date {
int year;
int month;
int day;
};

struct Student {
Char *name;
struct Date birthday;
};

3. Define the struct type, just describe the composition of the type, and do not allocate storage space for it, just as the system does not allocate space for the int type itself. The system allocates storage to this variable only if it is defined as a variable of the struct type

struct Student {
Char *name;
int age;
};

struct Student stu;

※ The memory space occupied by a struct variable is the sum of the memory occupied by its members, and each member is arranged in memory in the order defined by

For example, the following student structure:

struct Student {

Char *name; //Name

int age; //Age

float height; //Height

};

※ Initialization of the structure body

struct Student {
Char *name;
int age;
};

struct Student stu = {"MJ", 27};

※ Use of structural body


1. Generally, the operation of the struct variable is in the member unit, the general form of the reference is: struct variable name. Member Name


struct Student {
Char *name;
int age;
};

struct Student stu;
Stu.age = 27;


2. If a member is also a struct variable, you can use the member operator continuously "." Access lowest level Member


struct Date {
int year;
int month;
int day;
};

struct Student {
Char *name;
struct Date birthday;
};
struct Student stu;
stu.birthday.year = 1986;
Stu.birthday.month = 9;
Stu.birthday.day = 10;

3. The same type of structure variables can be assigned to the overall value

struct Student {
Char *name;
int age;
};
struct Student stu1 = {"MJ", 27};
Assign the STU1 directly to STU2
struct Student stu2 = stu1;
printf ("Age is%d", stu2.age);


※ Structure Array

1. Definition
As with struct variables, there are 3 ways to define a struct array


struct Student {
Char *name;
int age;
};
struct Student stu[5]; Definition 1



struct Student {
Char *name;
int age;
} Stu[5]; Definition 2



struct {
Char *name;
int age;
} Stu[5]; Definition 3
The above 3 methods define a struct array with a variable named Stu, and the number of elements in the array is 5


2. Initialization
struct {
Char *name;
int age;
} Stu[2] = {{"MJ", +}, {"JJ", 30}};
You can also use an array subscript to access each structure element, which is the same as normal array usage.




※ Structure as function parameter


When a struct variable is passed as a function parameter, it actually passes the value of all members, that is, the value one by one of the member in the argument is assigned to the corresponding parameter member. Therefore, changes to the parameters do not affect the arguments.
#include <stdio.h>

Define a struct body
struct Student {
int age;
};

void Test (struct Student stu) {
printf ("Parameter before modification:%d \ n", stu.age);
Modify the age in an argument
Stu.age = 10;

printf ("Modified formal parameter:%d \ n", stu.age);
}

int main (int argc, const char * argv[]) {

struct Student stu = {30};
printf ("Pre-modification argument:%d \ n", stu.age);

Call the test function
Test (STU);


printf ("Modified argument:%d \ n", stu.age);
return 0;
}


* A struct variable stu is defined in line 18th and passed to the test function as an argument in the 22nd row


The output is: The parameter is changed, but the argument has not changed


※ Pointer to the structure body


* Each struct variable has its own storage space and address, so pointers can also point to struct variables


* Structure pointer variable definition form: struct struct body name * pointer variable name


* With pointers to structs, there are 3 ways to access struct members


struct variable name. Member Name
(* pointer variable name). Member Name
Pointer variable name, member name
#include <stdio.h>

int main (int argc, const char * argv[]) {
Define a struct type
struct Student {
Char *name;
int age;
};

To define a struct-body variable
struct Student stu = {"MJ", 27};

Defines a pointer variable that points to the struct body
struct Student *p;

Pointer to struct variable stu
p = &stu;

/*
Members of the struct can be accessed in 3 ways at this time
*/
Method 1: struct variable name. Member Name
printf ("name=%s, age =%d \ n", Stu.name, Stu.age);

Mode 2: (* pointer variable name). Member Name
printf ("name=%s, age =%d \ n", (*p). Name, (*p). age);

Mode 3: Pointer variable name, member name
printf ("name=%s, age =%d \ n", P->name, P->age);

return 0;
}

Dark Horse Programmer C language-enumeration, struct, pointer

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.