[英]Why is my output 5 and 0 instead of 6 and 5, please help me
why my code output is 5 and 0,not the 6 and 5. I think i should get the 6 and 5. I am a newer.Please help me.为什么我的代码 output 是 5 和 0,而不是 6 和 5。我想我应该得到 6 和 5。我是新人。请帮助我。
#include <stdio.h>
int swap(int a,int b);
int main()
{ int x =5;
int y =6;
printf("%d %d\n",x,y);
int number[2]={swap(x,y)};
x=number[0];
y=number[1];
printf("%d %d\n",x,y);
return 0;
}
int swap(int a,int b)
{
return b,a;
}
There are several things wrong here.这里有几件事是错误的。
First, you can't return multiple values in C, like you can in Python.首先,您不能在 C 中返回多个值,就像在 Python 中一样。
return b, a;
uses the comma operator , which evaluates both its operands and returns the second one.使用逗号运算符,它计算其两个操作数并返回第二个。 So this is equivalent to just
return a;
所以这相当于只
return a;
. .
Second, your array initializer is only initializing the first element of the array.其次,您的数组初始化程序仅初始化数组的第一个元素。 There's only one expression in the initialization braces, so that initializes
number[0]
.初始化大括号中只有一个表达式,因此初始化
number[0]
。 The remaining elements of the array are initialized by default to 0
.数组的其余元素默认初始化为
0
。
Combining both of these, it's equivalent to:结合这两者,它相当于:
int number[2] = {y, 0};
I can see you are new to C programming.我可以看到您是 C 编程的新手。 The problem is in your
swap()
function.问题出在您的
swap()
function 中。 You're using a language construct that does not exist in C, namely tuples.您正在使用 C 中不存在的语言结构,即元组。 Check out pointers for a proper way to return multiple values from a function.
查看指针,了解从 function 返回多个值的正确方法。
This function...此 function...
int swap(int a,int b)
... returns one int
, as its prototype says. ...返回一个
int
,正如它的原型所说。
This statement...这个说法...
return b,a;
... involves C's comma operator ,
, which evaluates its left-hand operand, discards the result, then evaluates to the value of its right-hand operand. ... 涉及 C 的逗号运算符
,
,它计算其左侧操作数,丢弃结果,然后计算其右侧操作数的值。 Since evaluating b
has no side effects in your case, that return
statement is equivalent to由于评估
b
在您的情况下没有副作用,因此该return
语句等效于
return a;
In C, it is valid to initialize an array with fewer explicit elements than the length of the array.在 C 中,使用少于数组长度的显式元素来初始化数组是有效的。 For an automatic (local, non-
static
) array such as yours, as long as at least one element is initializer, all elements not explicitly initialized are implicitly initialized (to 0 in the case of int
elements).对于像您这样的自动(本地,非
static
)数组,只要至少一个元素是初始化器,所有未显式初始化的元素都会被隐式初始化(在int
元素的情况下为 0)。 Thus, for your implementation of swap()
, this...因此,对于您的
swap()
实施,这...
int number[2]={swap(x,y)};
... is equivalent to ... 相当于
int number[2] = { x, 0 };
, which explains the output. ,其中解释了 output。
Here is a way to solve your problem.这是解决您的问题的方法。
#include <stdio.h>
void swap(int *a,int *b);
int main()
{
int x = 5;
int y = 6;
swap(&x, &y);
printf("post swap x = %d, y = %d\n", x, y);
return 0;
}
// No need to return anything, we change the x, y values using the pointer
// This is passing by reference. Instead of passing the value, we are
// passing the reference (i.e address of the variable). swap function can
// now directly access the values and change them
void swap(int *a, int *b)
{
int tmp;
printf("Swap got a = %d, b = %d\n", *a, *b); // Note: we access value of a pointer using * in front of the pointer varaible
tmp = *a;
*a = *b;
*b = tmp;
}
outputs:输出:
bhakta: /tmp$ cc x.c
bhakta: /tmp$ ./a.out
Swap got a = 5, b = 6
post swap x = 6, y = 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.