c - C 中的函数参数在函数调用后未修改

标签 c

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

double gen(double dS) //Function for a random double variable
{
    double dx=0;
    dx=rand()%200+1;
    dS=dx/100;
    return(dS);
}

int main()
{
    double dZahl=0;
    double dTokens=1;
    srand((unsigned)time(NULL));
    double dSummand=0;
    int iGame=0;
    for(;;)
    {
        printf("Deine Tokens:%.2lf\n", dTokens);  //This doesn't really matter
        printf("Was m%cchtest du tun?\n[1]:Generiere Tokens\n[2]:Spiele ein Minigame\n", 148);
        fflush(stdout);
        scanf("%d", &iGame);
        fflush(stdin);
        switch (iGame)
        {
            case 1:
            {
                gen(dSummand);  //function in use
                dTokens=dTokens+dSummand;
                printf("\nDu hast %.2lf Tokens generiert!\n", dSummand);  //the output of the value
                fflush(stdout);
            }
            break;
        }
    }

    getch();
}

问题是 dSummand 没有从 dS 获取值。有谁知道问题是什么,因为我试图解决它,但我无法弄清楚。

我正在尝试为我的程序获取一个带有函数的随机 double 值。我以为它会像这样工作,但不幸的是它没有。

最佳答案

如果您希望 dS 的值从修改后的函数中出来,则必须传递一个指针。

double gen(double *dS) {

*dS=dx/100;

然后

gen(&dSummand);

关于c - C 中的函数参数在函数调用后未修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40536690/

相关文章:

php - 一起使用 c++ 和 php

c - c linux中的管道连接3个进程以执行命令

c - 在 C 中的 I/O 期间打印的交替行

c - 如何强制 C 程序打印意外结果?

c - 覆盆子 : how does the PWM via DMA work?

c++ - 使用指向字符的指针打印字符数组

c - 旅行商C程序错误

c - 解释以下输出

c - 如何清除保存内存位置起始位置的指针变量,该变量需要从起始位置清除 20 个字节的数据

C陷入无限循环