Home > Enterprise >  How to return values over multiple functions, or declare a function implicit?
How to return values over multiple functions, or declare a function implicit?

Time:12-15

Im currently making a TicTacToe and Im passing 2 values through several functions. After the functions, I need the value to be returned to the main() function, or call the first function (here it is named 'eingabe'. Sadly, I dont know, how to pass 2 values over 4 functions or how to call a "yet undeclared" function.

Here you can see my Code:

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

char spielfeld(char symbol[3][3], int counter){ 
    printf("\n");
    for(int i=0; i<4;i  ){
        for(int j =0; j<3;j  ){
            printf(" ---");
        }
        printf(" \n");
        if(i<3){
            for(int k=0; k<3;k  ){
                if(symbol[i][k]== 0){
                    symbol[i][k]=" ";
                }
                printf("| %c ", symbol[i][k]);
            }
            printf("|  ");
        printf("%d\n",i);
        }
    }
    printf("  A   B   C\n");
    counter = counter  1;
    eingabe(counter, symbol);
    return(symbol, counter);
}

char symbolset(int auswahl[2], char* spieler, char symbol[3][3], int counter){
    for(int i=0; i<3;i  ){
        for(int j=0; j<3;j  ){
            if(auswahl[0]==i && auswahl[1]==j){
                symbol[i][j]= *spieler;
            }
            else{
                symbol[i][j]=0;
            }
        }
    }
    spielfeld(symbol, counter);
    return(symbol, counter);

}

char umwandlung(char* eingabe, char* spieler, char symbol[3][3], int counter){
    int auswahl[2];
    char* chara="aa";
    char* charb="bb";
    if(eingabe[1]==48){
        auswahl[0]=0;
    }
    else if (eingabe[1]==49)
    {
        auswahl[0]=1;
    }
    else{
        auswahl[0]=2;
    }
    if(eingabe[0]==chara[0]){
        auswahl[1]=0;
    }
    else if (eingabe[0]==charb[0])
    {
        auswahl[1]=1;
    }
    else{
        auswahl[1]=2;
    }
    symbolset(auswahl, spieler, symbol, counter);
    return(symbol, counter);
}

char eingabe(int counter, char symbol[3][3]){
    char* spieler = "X";
    if(counter%2==0){
        spieler = "X";
    }
    else{
        spieler = "O";
    }
    int auswahl[2];
    char* eingabe= calloc(2, sizeof(char));
    scanf("%s", eingabe);
    printf("%d", eingabe[1]);
    umwandlung(eingabe, spieler, symbol, counter);
    return(symbol, counter);
}

int main(){
    int counter = 0;
    char symbol[3][3];
    printf("%d,%c",eingabe(counter, symbol));
    eingabe(counter, symbol);
    printf("%d,%c",eingabe(counter, symbol));
    
}

I tried already using a struct, but I still would need to pass it through all the functions, right? Any advices are welcome :) Thanks already

CodePudding user response:

  1. Return a struct

Close to OP's request.

typedef struct {
  char symbol[3][3];
  int counter;
} ttt;


ttt spielfeld(char symbol[3][3], int counter) { 
  ttt t;
  ...
  return t;
}
// or 
ttt spielfeld(ttt state) { 
// or 
ttt spielfeld(ttt *state) {

  1. Pass in the location to save the new state as a struct *.

This is more C idiomatic.

// Perhaps return an error flag should something go wrong
bool spielfeld(ttt *new_state, char symbol[3][3], int counter) { 
// or 
bool spielfeld(ttt *new_state, ttt *old_state) { 
  • Related