如何从特定函数返回数组?

时间:2014-03-01 05:19:14

标签: c arrays

过去两天我一直在研究这个功能,我似乎无法弄明白。函数displayBoard()打开一个文本文件,在本例中为“board.txt”并将其放入一维数组中。代码由我的教授提供。我把它变成了一个函数并试图返回数组板[]所以我可以在main中操作它但我无法做到,我缺少什么?

void displayBoard ()
{
    FILE *pInputFile;        // file pointer
    char board[64];          // the 8x8 board
    int i=0;                 // loop counter
    char inputFileName[] = "board.txt";

    // Associate the actual file name with file pointer and try to open it
    pInputFile = fopen(inputFileName, "r");
    // Verify input file was found
    if (pInputFile == NULL) {
        printf("Attempt to open file %s failed.  Exiting ... \n", inputFileName);
        exit( -1);
    }

    // Read from the file into the board.  Space in front of %c is important!
    while (fscanf(pInputFile, " %c", &board[i]) != EOF) {
        i++;
    }

    fclose(pInputFile);   // close input file

    // show contents of board
    printf("Board is: ");
    for (i=0; i<64; i++) {
        if (i%8 == 0) {
            printf("\n");
        }
        printf("%c ", board[ i]);
    }

    printf("\n\n");

    return board;
}    

int main ()
{
    printf(" %s", board); // This is my printf statement to see if I am able to access 
                          // board[] from the function
}

5 个答案:

答案 0 :(得分:0)

您需要通过声明函数

使方法返回char *
char * displayBoard {
    ...
}

答案 1 :(得分:0)

你做不到。您必须使用指针传递要修改的数组,并在函数内修改它。

#include <stdio.h>


void function(int b[][10], int m, int n){
  int i = 0, j = 0; 
  for(i = 0; i < m; ++i){
    for(j = 0; j < n; ++j){
      b[i][j] = i + j;
    }
  }
}

int main(){
  int board[10][10] = {0};
  int i = 0, j = 0; 


  for(i = 0; i < 10; ++i){
    for(j = 0; j < 10; ++j){
      printf("%d ", board[i][j]);
    }
    printf("\n");
  }



  function(board, 10, 10);



  for(i = 0; i < 10; ++i){
    for(j = 0; j < 10; ++j){
      printf("%d ", board[i][j]);
    }
    printf("\n");
  }


  return 0;
}

答案 2 :(得分:0)

我的功能有更新,我是这个网站的第一次使用者,所以我不确定我是应该在这里发布还是在我的OP上发布。这就是我想出来的

char* displayBoard (char board[]){
    FILE *pInputFile;        // file pointer
    int i=0;                 // loop counter
    char inputFileName[] = "board.txt";
    // Associate the actual file name with file pointer and try to open it
    pInputFile = fopen(inputFileName, "r");
    // Verify input file was found
    if( pInputFile == NULL) {
        printf("Attempt to open file %s failed.  Exiting ... \n", inputFileName);
        exit( -1);
    }

    // Read from the file into the board.  Space in front of %c is important!
    while( fscanf(pInputFile, " %c", &board[ i]) != EOF)  {
        i++;
    }

    fclose( pInputFile);   // close input file

    // show contents of board
    printf("Board is: ");
    for( i=0; i<64; i++) {
        if( i%8 == 0) {
            printf("\n");
        }
        printf("%c ", board[ i]);
    }

    printf("\n\n");

    return board;
}

答案 3 :(得分:0)

好的我决定将我的功能分成2个,一个用于生成电路板,另一个用于显示电路板,所以每当我想在main中重新显示时,我就可以使用displayBoard()。

void generateBoard (char board[]){
    FILE *pInputFile;        // file pointer
    int i=0;                 // loop counter
    char inputFileName[] = "board.txt";
    // Associate the actual file name with file pointer and try to open it
    pInputFile = fopen(inputFileName, "r");
    // Verify input file was found
    if( pInputFile == NULL) {
        printf("Attempt to open file %s failed.  Exiting ... \n", inputFileName);
        exit( -1);
    }

    // Read from the file into the board.  Space in front of %c is important!
    while( fscanf(pInputFile, " %c", &board[ i]) != EOF)  {
        i++;
    }

    fclose( pInputFile);   // close input file
}


void displayBoard (char board[]){
    // show contents of board
    printf("Board is: ");

    int i;
    for( i=0; i<64; i++) {
        if( i%8 == 0) {
            printf("\n");
        }
        printf("%c ", board[ i]);
    }

    printf("\n\n");

}

答案 4 :(得分:0)

当你编写在数组中返回结果的代码时,最好让调用者提供代码应该返回其结果的数组,而不是让代码自己分配一个数组。

原因是指针所有权转移使得很难推断内存管理,因此最好将其数量保持在最低水平。

相关问题