简体   繁体   English

在C中返回递归所需的硬编码数组是否正确?

[英]Is it correct to return a hard-coded array needed for recursion in C?

I realized that I need my C function to return 2 values and not just one, so is it correct to return a hard-coded array in this way?我意识到我需要我的 C function 返回 2 个值而不仅仅是一个值,所以以这种方式返回硬编码数组是否正确?

int * funct(){
    if(condition1){
       return{1,1}
    }else{
       return{1,-1}
    }
}    

I need this return array structure for implementing my minimax algorithm.我需要这个返回数组结构来实现我的 minimax 算法。 Here's some code for context, but it's not necessary (the example above should be enough to deliver the idea).这是上下文的一些代码,但这不是必需的(上面的示例应该足以传达这个想法)。

//Requires: board, depth & true if we're playing max player/ false if min player 
//Effects: returns best score & col
int * minimax(int ** board, int depth, bool maxPlayer){
  int newscore;
  int * validLocations= getValidLocations(board);
  bool gameOver= gameOver(board);
  if (depth==0 || gameOver){
    if (gameOver){
      if(isWin(board, COMPUTER)){
      return {-1, +10000};
      }
    else if(isWin(board,PLAYER)){
      return {-1, -10000};
      }
    else{
      return {-1, 0};; //tie
      }
    }
    else{ //depth is 0
      return {-1, boardScore(AI)};
    }
  }
  if(maxPlayer){
    int val= INT_MIN;
    int bestCol= validLocations[0];
    int validLocationsIndex=0;
    int col= validLocations[0];
    while (col!=-1  && validLocationsIndex<7){
      int ** possibleBoard= copyBoard(board);
      insert(possibleBoard, col, COMPUTER);
      newscore= minimax(possibleBoard, depth-1, false)[1];
      freeBoard(possibleBoard);
      if (newscore>val){
        val= newscore;
        bestCol= col;
      }
      validLocationsIndex++;
      col= validLocations[validLocationsIndex];
      return {bestCol, newscore};
      
    }
  }
  else{
    int val= INT_MAX;
    int validLocationsIndex=0;
    int col= validLocations[0];
    while (col!=-1  && validLocationsIndex<7){
      int ** possibleBoard= copyBoard(board);
      insert(possibleBoard, col, PLAYER);
      newscore= minimax(possibleBoard, depth-1, true)[1];
      freeBoard(possibleBoard);
      if (newscore<val){
        val= newscore;
        bestCol= col;
      }
      validLocationsIndex++;
      col= validLocations[validLocationsIndex];
      return {bestCol, newscore};
    }
  }
}
  1. automatic storage duration arrays cannot be returned in C language.自动存储时长arrays 不能用C语言返回。
  2. return {-1, boardScore(AI)}; this syntax is wrong这个语法是错误的
  3. You can use struct s as structs are passed bt value您可以使用struct s 作为结构传递 bt 值
  4. You can use compound literals您可以使用复合文字
typedef struct
{
    int data[2];
}mystruct;

mystruct funct(int condition)
{
    if(condition)
    {
       return (mystruct){{1,-1}};
    }else
    {
       (mystruct){{1, -1}};
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有返回硬编码字符串文字的函数的字符串存储在哪里? - Where is the string stored with functions that return a hard-coded string literal? c - 定义的值比硬编码的数字慢 - c - Are defined values slower than hard-coded numbers scanf格式的硬编码字符串 - Hard-coded string in the format of scanf 在C中使用硬编码值创建字符串数组 - Creating Array of Strings with hard coded value in C C堆分配硬编码的结构数组 - C Heap Allocating a Hard Coded Struct Array 将(uint32_t)与硬编码值进行比较是否安全? - Is it safe to compare an (uint32_t) with an hard-coded value? 为什么使用fgets()读取的命令不能与execlp()一起使用,但硬编码的相同命令是否正常工作? - Why does my command read with fgets() not work with execlp(), but the same command hard-coded works correctly? 如何使用python变量而不是硬编码值创建ctypes变量? - How to creating ctypes variables using python variables instead of hard-coded values? 尝试递归匹配2个硬编码数组中的数字,不能超过1个成功周期 - Trying to Recursively match numbers in 2 hard-coded arrays, unable to go further than 1 successful cycle 如果编译器绝对路径硬编码在 makefile 中,我应该如何使用 ccache? - How should I use ccache if the compiler absolute path is hard-coded in the makefile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM