-3

I coded a C Program as below:-

#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;
}

I have Two Errors:

Error prog1.CPP 10: Lvalue required //positions = getPositions(5242, 5243);
Error prog1.cpp 50: Cannot convert 'char *' to 'char' //return outPut;
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
rkaartikeyan
  • 1,977
  • 9
  • 29
  • 57
  • possible duplicate of [Assigning strings to arrays of characters](http://stackoverflow.com/questions/579734/assigning-strings-to-arrays-of-characters) –  Aug 22 '13 at 13:58
  • Question should be `how to "store" in a char array ?` – 0decimal0 Aug 22 '13 at 13:59
  • ^ I don't know. His mistake is more to do with the return type of his `getPositions` function. – Ford Aug 22 '13 at 14:00
  • Friends My Problem is not Store a Char in array. Try to understood i want a function Return Char Array, and i want to store it in Char Array variable. – rkaartikeyan Aug 22 '13 at 14:01
  • @hkpeprah and with the UB (he's returning a pointer to a local automatic variable), and with the fact that arrays are not modifiable lvalues, and, and, ... –  Aug 22 '13 at 14:01
  • @H2CO3, yup, he's not using pointers (for whatever reason), and not using heap allocation (for whatever reason). – Ford Aug 22 '13 at 14:03
  • I didn't even look at the return type . – 0decimal0 Aug 22 '13 at 14:03
  • I am a website developer and having 4 year exp in PHP... but this is for my Education Project. unfortunately i am bit confused to do this in C :( – rkaartikeyan Aug 22 '13 at 14:04
  • 2
    @rkaartikeyan I am sorry, but I was entirely serious with my first comment. C is not PHP. It's not even close. You don't have as many abstractions. You **must** develop a different mindset for C programming, but we aren't going to be able to teach that to you in one Stack Overflow answer. You will definitely have to spend quite a bit of time learning this language. This question is not appropriate for Stack Overflow, unfortunately. –  Aug 22 '13 at 14:05

2 Answers2

2

First you are doing very horrible mistake which is:

trying to return an array declared locally in a function to another function. the right thing to return an array is to return a pointer to it. And make sure the pointer points to a memory you own. the local variable will lose focus when the function return so you do not own them when the function return.

Quick solution for you: Pass the positions array as a 3rd parameter and make the function return void is the easiest thing for you now.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
1

The problem was you try to put a pointer into an array. In C you cant copy pointers content by using a simple equal.

Here the solution.

    #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);
    }

That's was your code, but i'll compile and ran this following (i removed #include and clrscr();):

    #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);
    }

OUTPUT:

    T0109059@P90b11c603564 ~/tmp/test
    $ ./a.exe
    759583832C2272364W2282526P
Xavier S.
  • 1,147
  • 13
  • 33
  • I believe it is useful to point that returning array which is declared as a local variable is not allowed as this variable scope will span to outer scope while it should not – Mahmoud Fayez Aug 22 '13 at 14:09
  • Thanks for your Kind reply Bro... going to try and will inform you :) – rkaartikeyan Aug 22 '13 at 14:15
  • When i Run the Program i got Error and Turbo C is get closed, i hope there might be mistake in my Conditional and loop statements. Will try to fix them and let you know @bingure – rkaartikeyan Aug 22 '13 at 14:19
  • would this code run into out of boundary? guessNoArr[4] accessed at index 4 which is out of bounds. – Wang Shaowei Jul 04 '20 at 16:14