简体   繁体   English

为什么我的 output 是 5 和 0 而不是 6 和 5,请帮帮我

[英]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.

相关问题 我无法在另一个文本文件中打印 output 请帮帮我 - i cant print output in another text file please help me 请帮助我在我的代码中查找错误(细分错误) - please help me to find Bug in my Code (segmentation fault) 请帮助我调试我的插入排序程序 - Please help me debug my Insertion Sort Program 请帮我正确的我的代码循环(C语言) - Please help me for the correct My Code looping (C Language) 我的程序代码 c 中的问题请帮助我解决问题 - Problem in my program code of c please help me in resolving the problem 在下面的程序中,我期望无限循环作为输出。 但是输出是0,请帮我解释一下它背后的概念 - In the below program I was expected Infinite loop as a output. But output is 0, please help me with explain the concept behind it 我无法弄清楚下面的代码是如何产生ffffffaa的输出的,请帮助我理解吗? - i am unable to figure out how the below code produce output as ffffffaa please help me understand? 有人可以帮我找出为什么没有输出吗? - Can someone help me to find why there's no output? 线性搜索代码表明我的商品不存在。请帮助我进行更正 - Linear Search Code Shows that my item is not present.Please help me make corrections 你能帮我找出我的词法分析器错误的原因:无效符号吗? - Would you please help me find the cause for my Lexical Analyzer Error: Invalid Symbol?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM