First, the structure type declaration
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 声明结构类型
struct date
{
int month;
int day;
int year;
};
// 使用自定义的类型
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n", today.year , today.month, today.day);
return 0;
}
Second, in the function / outside?
- And local variables, can only be used inside a function in the structure type declared inside a function
- It is usually a function of the result type declared outside, so that a plurality of functions can be used
#include <stdio.h>
struct date
{
int month;
int day;
int year;
};
int main(int argc, char const *argv[])
{
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n", today.year , today.month, today.day);
return 0;
}
Third, the structure of the form of declaration
struct point{
int x;
int y;
}
struct point p1,p2;
// 其中p1和p2都是point
// 里面有x和y的值
There is another form:
struct{
int x;
int y
}p1 , p2 ;
// p1和p2都是一种无名结构,里面有x和y
Of course, there is a more common form
struct point {
int x;
int y;
}p1 , p2;
p1 and p2 are the point, which has values of x and y
Fourth, the initialization structure
struct date{
int month;
int day;
int year;
};
int main(int argc, char const *argv[])
{
struct date taday = {9 , 30 , 2019};
struct date thismonth = {.month=9 , .year=2019};
printf("Today is date is %i-%i-%i\n", taday.year,taday.month,taday.day );
printf("This month is %i-%i-%i\n", thismonth.year,thismonth.month,thismonth.day);
return 0;
}
// Today is date is 2019-9-30
// This month is 2019-9-0
Structural member
- A bit like structures and arrays
- Array access its members a [10] = 10 with [] operator and subscripts;
- Its members today.day access structure with "." Operator and name, student.firstName
Computing structure
- To access the entire structure, the structure of the direct use of variable name
- For the overall configuration, the assignment can be done, taking address, parameters may be passed to the function
- p1 = (struct point) {5,10}; equal p1.x = 5, p1.y = 10;
- p1 = p2; equal p1.x = p2.x; p1.y = p2.y;
Structure pointer
- And an array of different, the name is not the address of the structure variable structure variable, you must use the & operator
- struct date *pDate = &today;
V. Structure and Function
Structure as a function parameter
int numberOfDays(struct date d);
- The entire structure can be passed as a function of the value of the parameter
- Value at this time is to create a structure variable in the function and copy the caller's structure
- It returns a structure may be
- This is a completely different array
Input structure
- There is no direct way a structure can be a scanf
- If we are going to write a function that reads the results
#include <stdio.h>
struct point
{
int x;
int y;
};
void getStruct(struct point p);
void output(struct point p);
int main()
{
struct point y = {0,0};
getStruct(y);
output(y);
return 0;
}
void getStruct(struct point p){
scanf("%d" , &p.x);
scanf("%d" , &p.y);
printf("%d , %d\n", p.x , p.y);
}
void output(struct point p)
{
printf("%d , %d\n", p.x , p.y);
}
// 2
// 3
// 2 , 3
// 0 , 0
- But read the structure of how to send it back?
- Remember when the c language function calls are passed by value, so the main function of p and y are in different
Solution to the problem
- Previous embodiment, to a structure passed to the function and operation in a function, but does not return back
- The problem is that the function is passed clone outside of that structure, rather than a pointer
- Incoming structure and incoming arrays are different
- In this input function, can create a temporary structure variable, then this structure is returned to the caller, but this is not common, you can understand
#include <stdio.h>
struct point
{
int x;
int y;
};
struct point getStruct(void);
void output(struct point p);
int main()
{
struct point y = {0,0};
y = getStruct();
output(y);
return 0;
}
// 这里返回一个struct
struct point getStruct(void){
struct point p;
scanf("%d" , &p.x);
scanf("%d" , &p.y);
printf("%d , %d\n", p.x , p.y);
return p;
}
void output(struct point p)
{
printf("%d , %d\n", p.x , p.y);
}
// 5 6
// 5 , 6
// 5 , 6
- The most common is the structure pointer as a parameter
Sixth, the pointer to the structure
struct date
{
int month;
int day;
int year;
}myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
By -> structure of a member in the pointer variable
Structure pointer parameter
struct point
{
int x;
int y;
};
struct point* getStruct(struct point *p);
void output(struct point p);
void print(const struct point *p);
int main(int argc, char const *argv[])
{
struct point y = {0,0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
return 0;
}
struct point* getStruct(struct point *p)
{
scanf("%d" , &p->x);
scanf("%d" , &p->y);
printf("%d , %d\n", p->x , p->y);
return p;
}
void output(struct point p)
{
printf("%d , %d\n", p.x , p.y);
}
void print(const struct point *p)
{
printf("%d , %d\n", p->x , p->y);
}