Cprogramming 简明教程

Structures and Functions in C

在 C 编程中, struct 是一种派生数据类型。正如我们可以传递基本数据类型的参数一样,也可以将 struct 数据类型的变量传递给函数。您还可以使用 call by valuecall by reference 方法传递 structures 。C 中的函数还可以返回 struct 数据类型。

In C programming, struct is a derived data type. Just as we can pass arguments of primary data types, a variable of struct data type can also be passed to a function. You can also pass structures using call by value and call by reference methods. A function in C may also return a struct data type.

请阅读本章以了解以下概念−

Read this chapter to understand the following concepts −

  1. How to pass elements of struct type

  2. How to pass a struct variable

  3. How to return struct from a function

  4. How to return a struct pointer

我们从第一个开始,了解如何传递 struct 类型元素。

Let’s start with the first one and learn how to pass elements of struct type.

How to Pass Struct Elements

派生类型是任何基本类型的一个或多个元素以及另一个派生类型的组合。可以按值或引用将元素传递给 function

A derived type is a combination of one or more elements of any of the primary types as well as another derived type. It is possible to pass elements to a function, either by value or by reference.

Example

在下面的示例中,我们有一个名为“rectangle”的派生类型,其中包含两个元素。我们有一个 struct 变量“r”,其中包含元素“r.len”和“l.brd”,并且它们被传递给函数。然后,area() 函数计算矩形的面积。

In the following example, we have a derived type called "rectangle" with two elements. We have a struct variable "r" with the elements "r.len" and "l.brd" and they are passed to a function. The area() function then computes the area of the rectangle.

#include <stdio.h>

struct rectangle{
   float len, brd;
};

int area(float, float);

int main(){

   struct rectangle r;
   r.len = 10.50; r.brd = 20.5;
   area(r.len, r.brd);

   return 0;
}

int area(float a, float b){

   double area = (double)(a*b);
   printf("Length: %f \nBreadth: %f \nArea: %lf\n", a, b, area);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Length: 10.500000
Breadth: 20.500000
Area: 215.250000

How to Pass a Struct Variable

让我们修改上述示例以将 struct 变量本身(而不是其元素)传递给 area() 函数。rectangle struct 类型还具有一个称为“area”的其他元素。

Let us modify the above example to pass the struct variable itself (instead of its elements) to the area() function. The rectangle struct type also has an additional element called "area".

Example

在函数内部,可以通过点运算符 (.) 访问 struct 变量的元素,并计算面积。

Inside the function, the elements of the struct variable are accessed though the dot operator (.) and the area is calculated.

#include <stdio.h>

struct rectangle{
   float len, brd;
   double area;
};

int area(struct rectangle);

int main(){

   struct rectangle r;
   r.len = 10.50; r.brd = 20.5;
   area(r);

   return 0;
}

int area(struct rectangle r){

   r.area = (double)(r.len*r.brd);
   printf("Length: %f \nBreadth: %f \nArea: %lf\n", r.len, r.brd, r.area);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Length: 10.500000
Breadth: 20.500000
Area: 215.250000

How to Return Struct from a Function

我们知道 C 中的函数可以返回任何类型的值。在此示例中,area() 函数被定义为返回一个 struct 变量。

We know that a function in C can return a value of any type. In this example, the area() function is defined to return a struct variable.

Example

在 main() fuction 内部,长度和宽度的输入被传递给 area() 函数。在 area() 函数内部,计算面积并填充一个 struct 变量并将其返回给 main() 函数,并在其中显示其元素。

Inside the main() fuction, the inputs for the length and the breadth are passed to the area() function. Inside the area() function, the area is computed and a struct variable is populated and returned to the main() function, where its elements are displayed.

#include <stdio.h>

struct rectangle {
   float len, brd;
   double area;
};

struct rectangle area(float x, float y);

int main(){

   struct rectangle r;
   float x, y;

   x = 10.5; y = 20.5;
   r = area(x, y);

   printf("Length: %f \n Breadth: %f \n Area: %lf\n", r.len, r.brd, r.area);

   return 0;
}

struct rectangle area(float x, float y){

   double area = (double)(x*y);
   struct rectangle r = {x, y, area};

   return r;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Length: 10.500000
Breadth: 20.500000
Area: 215.250000

How to Pass a Struct by Reference

在 C 语言中,可以将函数定义为通过值或引用传递其参数。引用是对现有变量的 pointer

In C language, a function may be defined to have its arguments passed by value or reference. A reference is the pointer to an existing variable.

Example

在这个示例中,一个 "rectangle" 类型的结构变量在 main() 中声明,其地址传递到一个名为 area() 的用户自定义函数。

In this example, a struct variable of "rectangle" type is declared in main() and its address is passed to a user-defined function called area().

当 area() 函数被调用时,它可以使用变量的元素与间接运算符 (→)。它计算结果并将其赋值给面积元素 "r → area"。

When the area() function is called, it can use the elements of the variable with the indirection operator (→). It computes the result and assigns it to the area element "r → area".

#include <stdio.h>

struct rectangle{
   float len, brd;
   double area;
};

int area(struct rectangle *);

int main(){

   struct rectangle r;
   r.len = 10.50; r.brd = 20.5;
   area(&r);

   return 0;
}

int area(struct rectangle *r){

   r -> area = (double)(r -> len * r -> brd);
   printf("Length: %f \nBreadth: %f \nArea: %lf\n", r -> len, r -> brd, r -> area);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Length: 10.500000
Breadth: 20.500000
Area: 215.250000

How to Return a Struct Pointer

让我们重写上面的代码来定义 area() 函数并返回指向矩形数据类型的结构的指针。

Let us rewrite the above code to define the area() function and return a pointer to a struct of rectangle data type.

Example

area() 函数有两个按值传递的参数。main() 函数从用户读取长度和宽度,并将其传递给 area() 函数,填充一个结构变量并将其引用返回给 main() 函数。

The area() function has two call-by-value arguments. The main() function reads the length and breadth from the user and passes them to the area() function, which populates a struct variable and passes its reference back to the main() function.

#include <stdio.h>

struct rectangle {
   float len, brd;
   double area;
};

struct rectangle * area(float x, float y);

int main (){

   struct rectangle *r;
   float x, y;

   x = 10.5; y = 20.5;
   r = area(x, y);
   printf("Length: %f \n Breadth: %f \n Area: %lf\n", r->len, r->brd, r->area);

   return 0;
}

struct rectangle * area(float x, float y){

   double area = (double)(x*y);
   static struct rectangle r;
   r.len = x; r.brd = y; r.area = area;

   return &r;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Length: 10.500000
Breadth: 20.500000
Area: 215.250000