如何存储函数';s返回C中的一个变量

如何存储函数';s返回C中的一个变量,c,turbo-c,C,Turbo C,我编写了一个C程序,如下所示:- #include <stdio.h> #include <conio.h> #include <string.h> char getPositions(int randNo, int guessNo); void main() { char positions[6]; clrscr(); positions = getPositions(5242, 5243); printf(position

我编写了一个C程序,如下所示:-

#include <stdio.h>
#include <conio.h>
#include <string.h>
char getPositions(int randNo, int guessNo);

void main()
{
    char positions[6];
    clrscr();
    positions = getPositions(5242, 5243);
    printf(positions);
    getchar();

}
char getPositions(int randNo, int guessNo)
{
    char outPut[6];
    int randNoArr[4], guessNoArr[4];
    int c, w, p;

    for(int i=4; i>0; i--){
        randNoArr[i] = randNo%10;
        randNo /= 10;

        guessNoArr[i] = guessNo%10;
        guessNo /= 10;
        //If Number Possitioned Right Place Incress variable c
        if(randNoArr[i] == guessNoArr[i]){
            c++;
        }
    }
    for(int j=1; j<=4; j++){
        if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
            w++;
        }
    }
    if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
        p++;
    }
    if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
        p++;
    }
    sprintf(outPut, "%dC%dW%dP", c,w,p);
    return outPut;
}

首先,你犯了一个非常可怕的错误:

试图将函数中本地声明的数组返回给另一个函数。返回数组的正确方法是返回指向它的指针。并确保指针指向您拥有的内存。当函数返回时,局部变量将失去焦点,因此当函数返回时,您不拥有它们

快速解决方案:
将positions数组作为第三个参数传递,并使函数return void现在对您来说是最简单的事情。

问题是您试图将指针放入数组中。在C语言中,不能使用简单的等号复制指针内容

这里是解决方案

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void getPositions(char *outPut, int randNo, int guessNo);

    void main()
    {
        char positions[6];
        clrscr();
        getPositions(positions, 5242, 5243);
        printf(positions);
        getchar();

    }
    void getPositions(char *outPut, int randNo, int guessNo)
    {
        int randNoArr[4], guessNoArr[4];
        int c, w, p;

        for(int i=4; i>0; i--){
            randNoArr[i] = randNo%10;
            randNo /= 10;

            guessNoArr[i] = guessNo%10;
            guessNo /= 10;
            //If Number Possitioned Right Place Incress variable c
            if(randNoArr[i] == guessNoArr[i]){
                c++;
            }
        }
        for(int j=1; j<=4; j++){
            if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
                w++;
            }
        }
        if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
            p++;
        }
        sprintf(outPut, "%dC%dW%dP", c,w,p);
    }

可能重复的问题应该是
如何在字符数组中“存储”?
^我不知道。他的错误更多地与他的
getPositions
函数的返回类型有关。试着理解我想要一个返回Char数组的函数,我想把它存储在Char数组变量中。@rkaartikeyan很抱歉,但我对我的第一条评论是完全认真的。C不是PHP。它甚至都不接近。你没有那么多的抽象概念。您必须为C编程开发一种不同的思维方式,但我们无法在一个堆栈溢出答案中向您传授这一点。你肯定要花很多时间学习这门语言。不幸的是,这个问题不适用于堆栈溢出。我认为有必要指出,不允许返回声明为局部变量的数组,因为此变量范围将扩展到外部范围,但不应感谢您的友好回复兄弟。。。我将尝试并将通知您:)当我运行程序时,我发现错误,并且turboc被关闭,我希望我的条件语句和循环语句中可能有错误。将尝试修复它们并让您知道@Bingure此代码是否会超出边界?guessNoArr[4]在索引4处访问,该索引超出了范围。
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void getPositions(char *outPut, int randNo, int guessNo);

    void main()
    {
        char positions[6];
        clrscr();
        getPositions(positions, 5242, 5243);
        printf(positions);
        getchar();

    }
    void getPositions(char *outPut, int randNo, int guessNo)
    {
        int randNoArr[4], guessNoArr[4];
        int c, w, p;

        for(int i=4; i>0; i--){
            randNoArr[i] = randNo%10;
            randNo /= 10;

            guessNoArr[i] = guessNo%10;
            guessNo /= 10;
            //If Number Possitioned Right Place Incress variable c
            if(randNoArr[i] == guessNoArr[i]){
                c++;
            }
        }
        for(int j=1; j<=4; j++){
            if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
                w++;
            }
        }
        if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
            p++;
        }
        sprintf(outPut, "%dC%dW%dP", c,w,p);
    }
    #include <stdio.h>
    #include <string.h>

    void getPositions(char *outPut, int randNo, int guessNo);

    void main()
    {
        char positions[6];

        getPositions(positions, 5242, 5243);
        printf(positions);
        getchar();

    }
    void getPositions(char *outPut, int randNo, int guessNo)
    {
        int randNoArr[4], guessNoArr[4];
        int c, w, p, i, j;

        for(i=4; i>0; i--){
            randNoArr[i] = randNo%10;
            randNo /= 10;

            guessNoArr[i] = guessNo%10;
            guessNo /= 10;
            //If Number Possitioned Right Place Incress variable c
            if(randNoArr[i] == guessNoArr[i]){
                c++;
            }
        }
        for(j=1; j<=4; j++){
            if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
                w++;
            }
        }
        if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
            p++;
        }
        if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
            p++;
        }
        sprintf(outPut, "%dC%dW%dP", c,w,p);
    }
    T0109059@P90b11c603564 ~/tmp/test
    $ ./a.exe
    759583832C2272364W2282526P