C language structure study notes induction

structure

1. Function: Use a whole to express multiple data gathered together (the data are not necessarily of the same type).
2. Statement of structure:

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

  • Note :
  1. A structure is a composite data type, and there can be different types of variables in it, and a variable is used as a whole to express all the variables in it.
  2. Struct is a keyword, followed by a C language statement, so the last ";" cannot be omitted.
  3. After the struct declaration, there will be three variables "day", "month", and "year" in every subsequent date.
  4. Like local variables, if the structure type is declared inside the function, it can only be used inside the function. If you want to be used by multiple functions, it is usually declared outside the function.
3. The form of the declaration structure

struct point{ int x; int y; }; // declare structure



struct point p1, p2; // define variables

  • Note: p1 and p2 have x and y values ​​respectively

struct point{
int x;
int y;
}p1,p2;

  • Declare and define variables at the same time
  1. (This practice is not common)

struct{
int x;
int y;
}p1,p2;

  • p1, p2 are unnamed structures, generally used for short-term use
4. Structural variables
  1. Look at the following example, once today is defined, today's memory is composed of three parts: month, day, and year.

struct date today;
today.month=11;
today.day=23;
today.year=2007;

month 11
day 23
year 2007
  1. Initialization of structure variables (similar to arrays)
  • The first

struct date today={07,31,2014};
// The value of the braces will be assigned to the variables defined in the declaration in turn .

  • The second

struct date thismonth={.month=7};
// Like arrays, when the number of initializations is less than the number of variables, other variables are automatically assigned zero values; the difference is that you can specify which variable is assigned what value.

  1. Note that the structure type is different from the structure variable. The structure type describes the internal situation of this type of structure, and the structure variable represents the entire structure.
5. Structural members
  1. The structure is similar to the array. The array has many units and the structure has many members. The difference is that the members of the structure can be of different types, and the array must be the same.
  2. Arrays use "[ ]" operators and subscripts to access their members, and structures use " . " operators and names to access their members.

a[0]=10;


today.day;

6. Structural operations (different from arrays)
  1. To access the entire structure, use the name of the structure variable directly.
  2. For the entire structure, you can do assignments, take addresses, or pass to function parameters.
  • Such as assignment, the array cannot do these two operations:

p1= (struct point) {5,10};
// equivalent to p1.x=5, p1.y=10;


p1=p2;
// equivalent to p1.x=p2.x, p1.y=p2.y;

7. Structure pointer
  • Unlike arrays, the names of structure variables are not the addresses of structure variables. To get the address, you must use the "&" operator.

struct date *pDate=&today;

8. Structure as a function parameter

int numberOfDays (struct date d)

  • note
  1. The entire structure can be passed into the function as the value of the parameter.
  2. At this time, a new structure variable is created in the function , but the value of the caller's structure is copied.
  3. You can also return a structure.
  4. These properties are completely different from arrays. (The address is passed when the array is passed parameters, and what changes is itself)
9. Structure input
  1. There is no direct way to scanf the entire structure at once.
  2. Two methods of input structure:
1. Write an input function, create a temporary structure variable in it, and return this structure.

struct point inputPoint()
{
struct point temp;
scanf("%d",&temp.x);
scanf("%d",&temp.y);
return temp;
}


void main()
{
struct point y;
y = inputPoint();
Outout(y);
}

2. The structure pointer is used as a parameter, the method is better.
  • This involves accessing the members in the pointer, and a new symbol " -> " (arrow) is introduced to represent the members in the structure variable pointed to by the pointer.

struct date{
int month;
int day;
int year;
} myday;
struct date *p = &myday;


(*p).month = 12;
p->month = 12;
// The above two expressions have the same meaning, but the second one is more concise.

  • After using pointers, the code can be written as:

struct point* getStruct( struct point *p)
{ scanf("%d",&p->x); scanf("%d",&p->y); return p; } // pointer type return is used here The value returns p. As a trick, it is convenient for other functions to continue to use this pointer in future programs. Such as: int main() { output(*getStruct(&y)); print(getStruct(&y)); }










// Other usages include:
getStruct(&y)->x= 0;
*getStruct(&y) = ( struct point){1,2};

10. Structure array

struct date dates[100];
struct date dates[ ] = { {4,5,2005},{2,4,2005} };


// When referencing a member of a cell of the array
dates[1].day = 20;

11. Structure nesting

struct point{
int x;
int y;
};
struct rectangle{
struct point pt1;
struct point pt2;
};


// If you have a variable
struct rectangle r;
// you can have
r.pt1.x, r.pt1.y, r.pt2.x, r.pt2.y
// If you define a variable
struct rectangle r,*rp;
rp = &r;
// Then the following is equivalent to
r.pt1.x
rp->pr1.x
Note that there is no rp->pt1->x (because pt1 is not a pointer, -> can only be used for pointers)

  • Here, the combination of array and structure can be diversified and multi-layered. For example, the unit of the array can be a structure, and there are structures inside each structure.

Guess you like

Origin blog.csdn.net/weixin_45688536/article/details/104156859