Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在c函数中返回correct变量_C - Fatal编程技术网

无法在c函数中返回correct变量

无法在c函数中返回correct变量,c,C,我希望此程序跟踪用户输入值的周期数,程序运行良好,但函数cycler()未返回适当的值。如果有人能在这里帮助我,我将不胜感激 #include <stdio.h> #include <stdlib.h> int cycle=0; int main() { startProgram(0);//If status is 1, the program will exit return 0; } void startProgram(int status) {

我希望此程序跟踪用户输入值的周期数,程序运行良好,但函数
cycler()
未返回适当的值。如果有人能在这里帮助我,我将不胜感激

#include <stdio.h>
#include <stdlib.h>

int cycle=0;

int main()
{
    startProgram(0);//If status is 1, the program will exit
    return 0;
}

void startProgram(int status) {
    if (status == 0) {

        printf("Enter a value\n");
        int input;
        scanf("%d",&input);
        printf("input is: %d\n",input);

        /*
           Here is where i need help!!!!!!!!!!
           When the cycler() is called, i want it to pass in the value of current cycle,
           The value of cycle has first been initialized to 0  
        */
        int cycle = cycler(cycle); 

        printf("Cycle Number : %d\n",cycle);

        resetProgram(input);

    } else {
        printf("Exiting");
    }
}

int cycler(int x){
    int ret = x++;
    return ret;
}

void resetProgram(int status){
    if ((status > 0) && (status < 12)) {
        startProgram(0);
    } else {
        printf("\nExit\n");
    }
}
#包括
#包括
整数周期=0;
int main()
{
startProgram(0);//如果状态为1,程序将退出
返回0;
}
无效启动程序(int状态){
如果(状态==0){
printf(“输入值”);
int输入;
scanf(“%d”,输入(&I));
printf(“输入为:%d\n”,输入);
/*
这里是我需要帮助的地方!!!!!!!!!!
调用cycler()时,我希望它传入当前cycle的值,
cycle的值首先已初始化为0
*/
int循环=循环器(循环);
printf(“循环编号:%d\n”,循环);
重置程序(输入);
}否则{
printf(“退出”);
}
}
整数循环器(整数x){
int-ret=x++;
返回ret;
}
无效重置程序(int状态){
如果((状态>0)和&(状态<12)){
启动程序(0);
}否则{
printf(“\nExit\n”);
}
}

既然您已经将
循环
声明为全局变量,那么记录循环数的最佳方法是执行
循环+
而不是
cycler()
do
++x
中的
x++

int-ret=x++的问题是指它在递增x之前返回x的值。然后返回具有相同值(不递增)的
ret

假设
x
0
。0是初始
循环

在语句
int-ret=x++之后
x将增加到1,但
ret
将被分配0(仍然是旧值),并且该
0
将被返回

上述事件每次都会发生


还有一个问题:您已经定义了
cycle
2次:全局和局部。我认为您应该在
int cycle=cycler(cycle)之前删除
int

也许你指的是计数器而不是循环器。这是一种常见的数据结构,用于统计某些事件的发生次数。您可以只使用
int
变量,但我更愿意为它定义一个结构。这允许您拥有一个“黑盒”计数器,该计数器只有几个已定义的操作。在本例中,将使用“向上计数”和“获取”来查看计数器的当前值

这方面的代码是:

typedef struct {
    int value;
} Counter;

void counter_init(Counter *ctr) {
    ctr->value = 0;
}

void counter_up(Counter *ctr) {
    ctr->value++;
}

int counter_get(const Counter *ctr) {
    return ctr->value;
}
这看起来像是很多代码,但它是值得的。一件好事是,您不再声明
int
变量(基本上可以用于任何事情:计算、计数、ID、文件描述符),而是声明
计数器
变量,此定义已经说明了该变量的确切用途

Counter ctr;
counter_init(&ctr);

while (fgetc(stdin) != EOF) {
    counter_up(&ctr);
}

printf("You entered %d bytes.\n", counter_get(&ctr));
这个程序清楚地表达了它计算某物的意图,在本例中是计算输入的长度。另外,不可能意外地以非预期的方式修改计数器的值

代码的纯
int
版本为:

int ctr = 0;

while (fgetc(stdin) != EOF) {
    ctr++;
}

printf("You entered %d bytes.\n", ctr);
不同之处在于,如果您传递该计数器以便其他函数可以修改它,则该函数的参数为
int*ctr
,而类型
int*
的表达式不多。它可以是指向单个int的指针,也可以是指向int数组的指针。有一个
计数器*ctr
参数可以使意图非常清楚


这种情况是否值得定义自定义数据类型的开销是您必须做出的选择。分别针对每种情况。

startProgram()
中再次声明
int cycle
会导致问题

#include <stdio.h>
#include <stdlib.h>

int cycle=0;
void resetProgram(int status);
int cycler(int *x){
    return *x+1;
}

void startProgram(int status) {
    if (status == 0) {

        printf("Enter a value\n");
        int input;
        scanf("%d",&input);
        printf("input is: %d\n",input);

        /*
           Here is where i need help!!!!!!!!!!
           When the cycler() is called, i want it to pass in the value of current cycle,
           The value of cycle has first been initialized to 0  
        */
        //in your code you declared cycle again local variable was given priority
        cycle = cycler(&cycle); 

        printf("Cycle Number : %d\n",cycle);

        resetProgram(input);

    } else {
        printf("Exiting");
    }
}

void resetProgram(int status){
    if ((status > 0) && (status < 12)) {
        startProgram(0);
    } else {
        printf("\nExit\n");
    }
}




int main()
{
    startProgram(0);//If status is 1, the program will exit
    return 0;
}
#包括
#包括
整数周期=0;
无效重置程序(int状态);
整数循环器(整数*x){
返回*x+1;
}
无效启动程序(int状态){
如果(状态==0){
printf(“输入值”);
int输入;
scanf(“%d”,输入(&I));
printf(“输入为:%d\n”,输入);
/*
这里是我需要帮助的地方!!!!!!!!!!
调用cycler()时,我希望它传入当前cycle的值,
cycle的值首先已初始化为0
*/
//在代码中,您再次声明了cycle,局部变量被赋予了优先级
循环=循环器(&循环);
printf(“循环编号:%d\n”,循环);
重置程序(输入);
}否则{
printf(“退出”);
}
}
无效重置程序(int状态){
如果((状态>0)和&(状态<12)){
启动程序(0);
}否则{
printf(“\nExit\n”);
}
}
int main()
{
startProgram(0);//如果状态为1,程序将退出
返回0;
}

如果不确切知道您期望的输出是什么,很难回答您的问题,但我认为您希望“循环数”在每次调用时都增加。如果是这样,那么您需要某种方法来跟踪变量的先前值。有几种方法可以做到这一点,但是当代码本身的结构中存在一些明显的错误时,很难规定“正确”的方法

这里最大的错误是其中一条评论中提到的“相互递归”。假设您有两个函数,如下所示

void a()
{
    // do something
    b();
}

void b()
{
    // do something
    a();
}
您可以在这里看到
a
调用
b
,而
b
调用
a
。这个节目将永远重复。问题是,每次在C中调用函数时,都会在ram中分配一点内存,以跟踪所使用的函数。由于
a
b
都不允许结束,或者更正式地说,不允许“
return
”,因此该内存永远不会释放。该程序最终将耗尽内存并崩溃。(事实上,这可能不是因为所谓的“内联”,但这只是因为这是一个人为的例子。)

在C中执行此操作的更好方法是使用
whilevoid a()
{
    // do something
}

void b()
{
    // do something
}

int main()
{
    while(1)
    {
        a()
        b()
    }
}
void startProgram()
{
    int statusIsGood = 1;
    int cycle = 1;

    while(statusIsGood)
    {
        int input;

        scanf("%d", &input);

        statusIsGood = getStatus(input);

        printf("Cycle count: %d", cycle);

        // In practice, it makes more sense to simply do
        // cycle++; and get rid of `nextCycle()` all together
        cycle = nextCycle();
    }
}

int nextCycle(int cycle)
{
    return cycle + 1;
}

int getStatus(int input)
{
    if (input <= 0 || input >= 12)
        return 1;
    else
        return 0;
}