c语言学习(4)
c语言从函数中返回多个值?
用指针参数来实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <stdio.h> return_more( int x, int y, int *sum, int *a, int *b); int *return_pointer( int *p, int n); int main() { int x, y; x = 131; y = 2; int *ptr; int arr[] = {1,2,3,4}; printf ( "Address of arr = %u\n" , arr); *arr = *arr + 15; ptr = return_pointer(arr, 22); printf ( "Address of arr = %u\n" , arr); printf ( "Address of ptr = %u\n" , ptr); for ( int i = 0; i<=4; i++) { printf ( "%d\n" , arr[i]); } //printf(arr); //int sum, diff, prod; //return_more(x, y, &sum, &diff, &prod); //printf("sum:%d\n", sum); //printf("diff:%d\n", diff); //printf("prod:%d\n", prod); return 0; } int *return_pointer( int *p, int n) { printf ( "Address of reference: = %u\n" , p); //p = p+n; printf ( "%d\n" , p); printf ( "%d\n" , p+1); printf ( "%d\n" , p+2); printf ( "%d\n" , p+3); printf ( "%d\n" , *p); printf ( "%d\n" , *(p+1)); printf ( "%d\n" , *(p+2)); printf ( "%d\n" , *(p+3)); return p; } return_more( int x, int y, int * sum, int * diff, int * prod) { *sum = x+y; *diff = x-y; *prod = x*y; } |
-------------------------------------------
原文: https://overiq.com/c-programming-101/returning-more-than-one-value-from-function-in-c/
Returning more than one value from function in C
Last updated on July 27, 2020
In the chapter Return statement in C, we have learned that the return statement is used to return a value from the function. But there is one limitation, a single return statement can only return one value from a function. In this chapter, we will see how to overcome this limitation by using call by reference.
Consider the following problem.
Q - Create a function to return sum, difference and product of two numbers passed to it.
Tell me how would you approach this problem?
One way to approach this problem is to create three functions for 3 operations and then use the return statement in each one of them to return sum, difference and product. By using call by reference we can solve this much easily.
The following program demonstrates how you can return more than one value from a function using call by reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<stdio.h>
void return_more_than_one(int a, int b, int *sum, int *diff, int *prod);
int main()
{
int x = 40, y = 10, sum, diff, prod;
return_more_than_one(x, y, &sum, &diff, &prod);
printf("%d + %d = %d\n",x, y, sum);
printf("%d - %d = %d\n",x, y, diff);
printf("%d * %d = %d\n",x, y, prod);
// signal to operating system program ran fine
return 0;
}
void return_more_than_one(int a, int b, int *sum, int *diff, int *prod)
{
*sum = a+b;
*diff = a-b;
*prod = a*b;
}
|
Expected Output:
1 2 3 |
40 + 10 = 50
40 - 10 = 30
40 * 10 = 400
|
How it works:
In return_more_than_one()
function a and b are passed using call by value, whereas sum
, diff
and prod
are passed using call by reference. As a result return_more_than_one()
function knows the address of sum
, diff
and prod
variables, so it access these variables indirectly using a pointer and changes their values.
--------------------------------------------
C Program to Access Array Elements Using Pointer
In this example, you will learn to access elements of an array using a pointer.
To understand this example, you should have the knowledge of the following C programming topics:
Access Array Elements Using Pointers
#include <stdio.h>
int main() {
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}
Output
Enter elements: 1 2 3 5 4 You entered: 1 2 3 5 4
In this program, the elements are stored in the integer array data[]
.
Then, the elements of the array are accessed using the pointer notation. By the way,
data[0]
is equivalent to*data
and&data[0]
is equivalent todata
data[1]
is equivalent to*(data + 1)
and&data[1]
is equivalent todata + 1
data[2]
is equivalent to*(data + 2)
and&data[2]
is equivalent todata + 1
...
data[i]
is equivalent to*(data + i)
and&data[i]
is equivalent todata + i
Visit this page to learn about the relationship between pointers and arrays.
-------------------------------------------------------------
Returning a Pointer from a Function in C
Last updated on July 27, 2020
We have already seen a function can return data of types int , float, char etc. Similarly, a function can return a pointer to data. The syntax of a function returning a pointer is as follows.
Syntax: type *function_name(type1, type2, ...);
Some examples:
1 2 3 |
int *func(int, int); // this function returns a pointer to int
double *func(int, int); // this function returns a pointer to double
|
The following program demonstrates how to return a pointer from a function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h>
int *return_pointer(int *, int); // this function returns a pointer of type int
int main()
{
int i, *ptr;
int arr[] = {11, 22, 33, 44, 55};
i = 4;
printf("Address of arr = %u\n", arr);
ptr = return_pointer(arr, i);
printf("\nAfter incrementing arr by 4 \n\n");
printf("Address of ptr = %u\n\n" , ptr);
printf("Value at %u is %d\n", ptr, *ptr);
// signal to operating system program ran fine
return 0;
}
int *return_pointer(int *p, int n)
{
p = p + n;
return p;
}
|
Expected Output:
1 2 3 4 5 6 7 |
Address of arr = 2686736
After incrementing arr by 4
Address of ptr = 2686752
Value at 2686752 is 55
|
How it works:
Since the name of an array is a pointer to the 0th element of the array. Here we are passing two arguments to the function return_pointer()
. The arr
is passed using call by reference (notice that name of the array is not preceded by &
operator because the name of the array is a constant pointer to the 0th element of the 1-D array) and i
is passed using call by value. Inside the function pointer p
is incremented by n
and reassigned to p
. Finally, the pointer p
is returned to the main()
function and reassigned to ptr
.
Never return a pointer to local variable from a function.
Consider the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h>
int *abc(); // this function returns a pointer of type int
int main()
{
int *ptr;
ptr = abc();
return 0;
}
int *abc()
{
int x = 100, *p;
p = &x;
return p;
}
|
Can you point out the problem with above code?
In the function abc()
we are returning a pointer to the local variable. Recall that a local variable exists only inside the function and as soon as function ends the variable x
cease to exists, so the pointer to it is only valid inside the function abc()
.
Even though the address returned by the abc()
is assigned to ptr
inside main()
, the variable to which ptr
points is no longer available. On dereference the ptr
you will get some garbage value.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-08-16 vim 多窗口,多tab编辑
2018-08-16 【转】php中的会话机制(2)
2017-08-16 【转】三年后再反思我的" Java Web项目管理得失谈"
2017-08-16 java构造函数重载this(true)
2017-08-16 为什么用clojure作为storm 的主要开发语言
2017-08-16 php模拟并发
2017-08-16 【转】storm 开发系列一 第一个程序