https://www.sharetechnote.com/html/C_struct.html
结构体是一种特殊的数据类型,用于将多个相关数据(数据类型)打包到一个变量中。我认为它在C语言中最有用,但是当你刚开始学习编程时,可能不清楚为什么要使用这个数据类型以及何时/如何使用它。但是随着你花费更多的时间编写更多的程序,你会逐渐感受到它的用处以及为什么我需要这个数据类型。
定义和调用一个结构体
创建结构体数组
将结构体传递给函数
从函数返回结构体
示例01:
在这个例子中,我将两个变量x和y打包到一个名为Point的结构体中。你可以不使用结构体编写相同的程序。你可以定义两个单独的变量float PointX, float PointY。是否使用结构体还是独立的变量取决于你,但对我来说,将这些两个变量打包到一个包中更有意义,因为它们表示坐标系中的一个位置。
#include <stdio.h>
struct Point {
float x;
float y;
};
int main()
{
struct Point pt;
pt.x = 1.2;
pt.y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 02 >
这个例子并不是为了展示如何定义/使用结构体。这个例子和之前的例子的区别在于在使用内存空间之前对其进行保护。你并不总是需要这样做,但如果你要处理一个占用大量内存的结构体,那么这样做会是一个很好的习惯。
#include <stdio.h>
struct Point {
float x;
float y;
};
int main()
{
struct Point *pt;
pt = (struct Point*) malloc(sizeof(struct Point));
pt->x = 1.2;
pt->y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 03 >
This example is just to show you how to define and use a struct with typedef.
#include <stdio.h>
typedef struct POINT {
float x;
float y;
} Point;
int main()
{
Point pt;
pt.x = 1.2;
pt.y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 04 >
This is another example to show you how to define and use a struct with typedef with a little bit different way comparting to example 03.
#include <stdio.h>
struct POINT {
float x;
float y;
};
typedef struct POINT Point;
int main()
{
Point pt;
pt.x = 1.2;
pt.y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 05 >
This is another example to show you how to define and use a struct with typedef with a little bit different way comparting to example 03 and 04.
#include <stdio.h>
typedef struct{
float x;
float y;
} POINT;
typedef POINT Point;
int main()
{
Point pt;
pt.x = 1.2;
pt.y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 06 >
This is an example of using a struct as a pointer. The important thing to note is that I am using malloc() to secure a memory for the struct before I use (assign value to it).
#include <stdio.h>
#include <malloc.h>
typedef struct POINT {
float x;
float y;
} Point;
int main()
{
Point *pt;
pt = (Point*) malloc(sizeof(Point));
pt->x = 1.2;
pt->y = 2.1;
printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
creating struct array
Example 01 >
#include <stdio.h>
struct Point {
float x;
float y;
};
int main()
{
struct Point pt[5];
int i = 0;
for(i = 0; i < 5; i++){
pt[i].x = i;
pt[i].y = 10*i;
printf("The coordinate of pt[%d] is = (%f,%f)\n", i, pt[i].x, pt[i].y);
}
return 1;
}
Result :
The coordinate of pt[0] is = (0.000000,0.000000)
The coordinate of pt[1] is = (1.000000,10.000000)
The coordinate of pt[2] is = (2.000000,20.000000)
The coordinate of pt[3] is = (3.000000,30.000000)
The coordinate of pt[4] is = (4.000000,40.000000)
Example 02 >
#include <stdio.h>
struct Point {
float x;
float y;
};
int main()
{
struct Point *pt;
int i = 0;
pt = (struct Point*) malloc(5 * sizeof(struct Point));
for(i = 0; i < 5; i++) {
(pt+i)->x = i;
(pt+i)->y = 10*i;
printf("The coordinate of (pt+%d) is = (%f,%f)\n", i, (pt+i)->x, (pt+i)->y);
}
return 1;
}
Result :
The coordinate of (pt+0) is = (0.000000,0.000000)
The coordinate of (pt+1) is = (1.000000,10.000000)
The coordinate of (pt+2) is = (2.000000,20.000000)
The coordinate of (pt+3) is = (3.000000,30.000000)
The coordinate of (pt+4) is = (4.000000,40.000000)
Example 03 >
#include <stdio.h>
#include <malloc.h>
struct Point {
float x;
float y;
};
int main()
{
struct Point *pt;
int i = 0;
pt = (struct Point*) malloc(5 * sizeof(struct Point));
for(i = 0; i < 5; i++) {
pt[i].x = i;
pt[i].y = 10*i;
printf("The coordinate of (pt+%d) is = (%f,%f)\n", i, pt[i].x, pt[i].y);
}
return 1;
}
Result :
The coordinate of (pt+0) is = (0.000000,0.000000)
The coordinate of (pt+1) is = (1.000000,10.000000)
The coordinate of (pt+2) is = (2.000000,20.000000)
The coordinate of (pt+3) is = (3.000000,30.000000)
The coordinate of (pt+4) is = (4.000000,40.000000)
passing a struct into a function
在个人经验中,这是我最常使用结构体的情况/原因。当需要将多个变量传递给一个函数,且这些变量与某个特定目的/用例相关时,我倾向于将这些变量打包到结构体中,并将结构体传递给函数。通过这种方式,可以轻松定义/使用该函数。有时在定义函数时,我不知道要传递多少个单独的变量。如果我定义了一个结构体并将其传递给函数,即使变量的数量发生变化,我也不需要更改函数的原型。我只要根据需要添加或删除结构体的成员函数即可。
Example 01 >
#include <stdio.h>
struct POINT{
float x;
float y;
} ;
void printPoint(struct POINT pt)
{
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
}
int main()
{
struct POINT pt1;
pt1.x = 1.2;
pt1.y = 2.1;
printPoint(pt1);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 02 >
#include <stdio.h>
struct POINT{
float x;
float y;
} ;
void printPoint(struct POINT pt)
{
printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);
}
int main()
{
struct POINT pt1;
pt1.x = 1.2;
pt1.y = 2.1;
printPoint(pt1);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
Example 03 >
#include <stdio.h>
#include <malloc.h>
typedef struct POINT{
float x;
float y;
} Point ;
void printPoint(Point *pt)
{
printf("The coordinate of pt is = (%f,%f)\n", pt->x, pt->y);
}
int main()
{
Point pt1;
Point *pt2;
pt2 = (Point*) malloc(sizeof(Point));
pt1.x = 1.2;
pt1.y = 2.1;
pt2->x = 12;
pt2->y = 21;
printPoint(&pt1);
printPoint(pt2);
return 1;
}
Result :
The coordinate of pt is = (1.200000,2.100000)
The coordinate of pt is = (12.000000,21.000000)
returning a struct from a function
这是另一个常见的情况,我使用结构体。在C语言中,基本上一个函数只能返回一个值。当我想从函数返回多个值时,我会定义一个包含多个成员值的结构体,并从函数中返回该结构体。
Example 01 >
#include <stdio.h>
typedef struct POINT{
float x;
float y;
} Point ;
Point filloutPoint(float x, float y)
{
Point pt;
pt.x = x;
pt.y = y;
return pt;
}
int main()
{
Point rPt;
rPt = filloutPoint(1.2,2.4);
printf("The coordinate of rPt is = (%f,%f)", rPt.x, rPt.y);
return 1;
}
Result :
The coordinate of rPt is = (1.200000,2.400000)
Example 02 >
#include <stdio.h>
#include <malloc.h>
typedef struct POINT{
float x;
float y;
} Point ;
Point * filloutPoint(float x, float y)
{
Point *pt;
pt = (Point*) malloc(sizeof(Point));
pt->x = x;
pt->y = y;
return pt;
}
int main()
{
Point *rPt;
rPt = filloutPoint(1.2,2.4);
printf("The coordinate of rPt is = (%f,%f)", rPt->x, rPt->y);
return 1;
}
Result :
The coordinate of rPt is = (1.200000,2.400000)
Example 03 >
#include <stdio.h>
#include <malloc.h>
typedef struct POINT{
float x;
float y;
} Point ;
Point * filloutPoint(float *x, float *y,int n)
{
Point *pt;
pt = (Point*) malloc(n * sizeof(Point));
int i = 0;
for( i = 0; i < n; i++){
(pt+i)->x = x[i];
(pt+i)->y = y[i];
}
return pt;
}
int main()
{
Point *rPt;
float x[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
float y[5] = {10.0, 20.0, 30.0, 40.0, 50.0};
int n = 5;
int i = 0;
rPt = filloutPoint(x,y,n);
for( i = 0; i < n; i++){
printf("The coordinate of rPt[%d] is = (%f,%f)\n", i, (rPt+i)->x, (rPt+i)->y);
}
return 1;
}
Result :
The coordinate of rPt[0] is = (1.000000,10.000000)
The coordinate of rPt[1] is = (2.000000,20.000000)
The coordinate of rPt[2] is = (3.000000,30.000000)
The coordinate of rPt[3] is = (4.000000,40.000000)
The coordinate of rPt[4] is = (5.000000,50.000000)
本文详细介绍了C语言中结构体的定义、使用方法,包括创建结构体、数组、指针,以及在函数传递参数和返回值中的应用实例,帮助初学者理解其重要性和用途。

被折叠的 条评论
为什么被折叠?



