简体   繁体   English

为什么我没有使用 c 中函数中的指针获得该程序所需的输出?

[英]Why am I not getting the desired output for this program using pointers in functions in c?

Header file : circlehead.h头文件:circlehead.h

#include <stdio.h>
void circle_Data(float *r);
#define PI 3.14f

C FILE1: circle.c C FILE1: circle.c

#include "circlehead.h"
void circle_Data(float *r)
{
    float ar=0,peri=0;
    ar= PI * (*r) * (*r);
    peri=2 * PI * (*r);
}

MAIN FUNCTION circle_main.c主要功能circle_main.c

#include<stdio.h>
#include "circlehead.h"
int main()
{
    float r=5.24;
float  ar, peri;
    
    circle_Data(&r);
    printf("Area is %f", ar);
    printf("Perimeter is %f", peri);
}


I have linked the files into a single executable:我已将文件链接到一个可执行文件中:

gcc -c circle.c
gcc -c circle_main.c
gcc -o x_exe circle.o circle_main.o 
./x_exe

But I am getting the output as area: 3.728 and perimeter: 0.000 The code was compiled successfully.但我得到的输出为 area: 3.728 和perimeter: 0.000 代码编译成功。 What am I doing wrong?我究竟做错了什么?

You never assign ar or peri any values in main , so those variables don't every get assigned any values.您永远不会在main arperi分配任何值,因此这些变量不会每个都被分配任何值。 That different variables with the same names get assigned elsewhere doesn't matter.在别处分配具有相同名称的不同变量并不重要。 (And the language would be pretty much unusable if it did.) (如果确实如此,该语言将几乎无法使用。)

You had the right idea (passing a pointer) but you used it in the wrong way.你有正确的想法(传递一个指针),但你以错误的方式使用它。

The problem was that you passed a pointer to the variable that wouldn't be changed, and didn't pass pointers to the variables that you did need to be changed, by circle_Data() .问题是您通过circle_Data()传递了一个指向不会更改的变量的指针,并且没有传递指向您确实需要更改的变量的指针。 This version ought to behave in the way you wanted.这个版本应该按照你想要的方式运行。 The values of ar and peri that are local to main() are modified by circle_Data() , and the correct values can then be printed. main()本地的arperi值由circle_Data()修改,然后可以打印正确的值。

Note that circle_Data() gets a copy of r that it can modify, but that won't change the value of r in main() .请注意, circle_Data()获得了一个它可以修改的r副本,但这不会改变main()r的值。 It's a totally separate variable, just as ar and peri were in your first version.它是一个完全独立的变量,就像你的第一个版本中的arperi一样。

#include "circlehead.h"
void circle_Data(float r, float* ar, float* peri )
{
    *ar= PI * r * r;
    *peri=2 * PI * r;
}


#include<stdio.h>
#include "circlehead.h"
int main()
{
    float r=5.24;
    float ar, peri;
    
    circle_Data(r, &ar, &peri);
    printf("Area is %f", ar);
    printf("Perimeter is %f", peri);
}

You could do something like the below.您可以执行以下操作。 The problem is caused by the fact that you never pass in ar or peri by reference.问题是由于您从未通过引用传入 ar 或 peri 的事实造成的。 So you code does not change them.所以你的代码不会改变它们。

Main主要的

#include<stdio.h>
#include "circlehead.h"


int main()
{
  float r = 5.24;
  float ar = 0;
  float peri = 0;
    
    circle_Data(r, ar, peri);
    printf("Area is %f", ar);
    printf("Perimeter is %f", peri);

    return 0;
}

Header标题

#include <stdio.h>
void circle_Data(float r, float &ar, float &peri);
#define PI 3.14f

Body身体

#include "circlehead.h"
void circle_Data(float r, float &ar, float &peri)
{
    ar = PI * (r) * (r);
    peri = 2 * PI * (r);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM