gpt4 book ai didi

c - 将二维数组 char 传递给函数

转载 作者:太空狗 更新时间:2023-10-29 17:02:36 24 4
gpt4 key购买 nike

我正在尝试将其中包含字符串的二维数组传递给函数。我不断得到] 标记前的预期表达式。

这段代码的重点是阅读一个单词搜索谜题,然后在该谜题中找到单词。我将编写一个用于向前搜索、反向搜索以及向上和向下搜索的函数。

如何摆脱这个错误?错误出现在我调用 forward 函数的最底部。

    /*Andrea Hatfield CPE 101 October 31st, 2012*/
#include <stdio.h>
#include <string.h>

int forward(char words[][8], char puzzle[][11]);


int main()
{
char puzzle[11][11];
char words[8][8];
FILE *fin, *fwords;
int i = 0;
int j= 0;

fin = fopen("puzzle.in", "r");
fwords = fopen("words.in", "r");


if(fin == NULL) /*Reads in the puzzle file*/
printf("File does not exist");
else
{
while(fscanf(fin,"%s", puzzle[i])!=EOF)
{
printf("%s\n", puzzle[i]);
i++;
}


}

if(fwords == NULL) /*Reads in the words the puzzle will search for */
printf("File does not exist");
else
{
while(fscanf(fwords, "%s", words[j])!=EOF)
{
printf("%s\n", words[j]);
}
}


forward(&words[][8], &puzzle[][11]); /*Error at this point*/
return(0);
}

最佳答案

要得到你想做的事情有点困难,但这里有一个问题:

forward(&words[][8], &puzzle[][11]);

试试这个:

forward(words, puzzle);

这里还有两个应该从 here 中提及的选项:

#define ROWS 4
#define COLS 5

void func(int array[ROWS][COLS]) {
int i, j;

for (i=0; i<ROWS; i++) {
for (j=0; j<COLS; j++) {
array[i][j] = i*j;
}
}
}

void func_vla(int rows, int cols, int array[rows][cols]) {
int i, j;

for (i=0; i<rows; i++) {
for (j=0; j<cols; j++) {
array[i][j] = i*j;
}
}
}

int main() {
int x[ROWS][COLS];

func(x);
func_vla(ROWS, COLS, x);
}

关于c - 将二维数组 char 传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com