功能:变量替代

时间:2013-05-02 04:42:16

标签: c function

我在使用下面的代码时遇到了一些问题;我知道这对于你更有经验的编码员来说是最基本的东西,但这是我第一次尝试用C编写代码,所以请耐心等待。

#include <stdio.h>

int main()
{
    printf("This program, or function, substitutes one given number for another given number, and then places the former with the latter number. ");

    float y1 = 10;  /* Assign value to variable 1*/
    float y2 = 20;  /* Assign value to variable 2*/
    float y3 = 30;  /* Assign value to variable 3*/
    float y4 = 40;  /* Assign value to variable 4*/

    void sub( float *, float *, float *, float * );                 /*declare function prototype*/

    printf("\nThe numbers' given values before the aforementioned function:  Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4 );
    sub( &y1, &y2, &y3, &y4 );   /*This is where the function, to move the numbers, is called */
    printf("\nThe numbers' given values after the aforementioned function:   Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4 );
}

void sub(float *z1, float *z2, float *n3, float *n4)
{
    int z;
    int n;
    z = *z1;
    *z1 = *z2;
    *z2 = z;
    n = *n3;
    *n3 = *n4;
    *n4 = n;
}

输出是:

This program, or function, substitutes one given number for another given number, and then places the former with the latter number. 

The numbers' given values before the aforementioned function:  Number 1 = 10.000000 , Number 2 = 20.000000  , Number 3 = &f , Number 4 =
30.000000

The numbers' given values after the aforementioned function:   Number 1 = 20.000000 , Number 2 = 10.000000  , Number 3 = &f , Number 4 =
40.000000

我的问题是:我如何更改代码,以便“数字3”替换自己就像其他变量一样。

2 个答案:

答案 0 :(得分:3)

你只是有一个错字:更改&amp; f到%f

3号=&amp; f

答案 1 :(得分:0)

麻烦在于你的printf。请在printf声明中将&f更改为%f,以获得数字3。