Cprogramming 简明教程
Return a Pointer from a Function in C
在 C programming 中,可以将函数定义为具有多个参数,但它只能将一个表达式返回给调用函数。
In C programming, a function can be defined to have more than one argument, but it can return only one expression to the calling function.
A function can return a single value that may be any type of variable, either of a primary type (such as int, float, char, etc.), a pointer to a variable of primary or user−defined type, or a pointer to any variables.
阅读本章以了解 C 程序中的函数如何通过不同方式返回指针。
Read this chapter to learn the different ways in which a function in a C program returns a pointer.
Return a Static Array from a Function in C
If a function has a local variable or a local array, then returning a pointer of the local variable is not acceptable because it points to a variable that no longer exists. Note that a local variable ceases to exist as soon as the scope of the function is over.
Example 1
以下示例显示了如何在所调函数 (arrfunction) 中使用 static array ,并将其指针返回给 main() function 。
The following example shows how you can use a static array inside the called function (arrfunction) and return its pointer back to the main() function.
#include <stdio.h>
#include <math.h>
float * arrfunction(int);
int main(){
int x = 100, i;
float *arr = arrfunction(x);
printf("Square of %d: %f\n", x, *arr);
printf("Cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
float *arrfunction(int x){
static float arr[3];
arr[0] = pow(x,2);
arr[1] = pow(x, 3);
arr[2] = pow(x, 0.5);
return arr;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Square of 100: 10000.000000
Cube of 100: 1000000.000000
Square root of 100: 10.000000
Example 2
现在考虑以下函数,它将生成 10 个随机数。它们存储在静态数组中,并将其指针返回到 main() 函数。然后在 main() 函数中遍历该数组,如下所示:
Now consider the following function which will generate 10 random numbers. They are stored in a static array and return their pointer to the main() function. The array is then traversed in the main() function as follows −
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* function to generate and return random numbers */
int *getRandom() {
static int r[10];
srand((unsigned)time(NULL)); /* set the seed */
for(int i = 0; i < 10; ++i){
r[i] = rand();
}
return r;
}
int main(){
int *p; /* a pointer to an int */
p = getRandom();
for(int i = 0; i < 10; i++) {
printf("*(p + %d): %d\n", i, *(p + i));
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
*(p + 0): 776161014
*(p + 1): 80783871
*(p + 2): 135562290
*(p + 3): 697080154
*(p + 4): 2064569097
*(p + 5): 1933176747
*(p + 6): 653917193
*(p + 7): 2142653666
*(p + 8): 1257588074
*(p + 9): 1257936184
Return a String from a Function in C
使用相同的方法,可以将 string 传递给函数并从函数返回。C 中的字符串是 char 类型的数组。在以下示例中,我们使用指针传递字符串,在函数内部操作它,并将其返回到 main() 函数。
Using the same approach, you can pass and return a string to a function. A string in C is an array of char type. In the following example, we pass a string with a pointer, manipulate it inside the function, and return it to the main() function.
Example
在被调用的函数的内部,我们使用 malloc() function 分配内存。通过的字符串在返回前会与本地字符串连接。
Inside the called function, we use the malloc() function to allocate the memory. The passed string is concatenated with the local string before returning.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *hellomsg(char *);
int main(){
char *name = "TutorialsPoint";
char *arr = hellomsg(name);
printf("%s\n", arr);
return 0;
}
char *hellomsg(char *x){
char *arr = (char *)malloc(50*sizeof(char));
strcpy(arr, "Hello ");
strcat(arr, x);
return arr;
}
运行代码并检查其输出:
Run the code and check its output −
Hello TutorialsPoint
Return a Struct Pointer from a Function in C
以下示例展示了如何返回指向 struct type 变量的指针。
The following example shows how you can return the pointer to a variable of struct type.
此处,area() 函数有 2 个按值传递的参数。main() 函数从用户读取长度和宽度并将其传递给 area() 函数,该函数会填充一个结构变量并将其引用(指针)返回给 main() 函数。
Here, 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 (pointer) back to the main() function.
Example
看一下这个程序 −
Take a look at the program −
#include <stdio.h>
#include <string.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 \nBreadth: %f \nArea: %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