所以我有一个名为scanCode
的函数,它扫描文本文件中的单词并将其存储在2D数组中。然后我想把这个数组返回到main函数中的一个数组变量,这是我到目前为止的代码
#include <stdio.h>
char **scanCode()
{
FILE *in_file;
int i = 0;
static char scan[9054][6];
in_file = fopen("message.txt", "r");
while (!feof(in_file))
{
fscanf(in_file, "%s", scan[i]);
i++;
}
return scan;
}
int main(void)
{
int hi[9053];
FILE *in_file;
in_file = fopen("message.txt", "r");
char **array = scanCode();
printf("%c", array[0]);
printf("%c", array[1]);
printf("%c", array[2]);
printf("%c", array[3]);
}
所以基本上从scanCode
函数返回的数组我希望它存储在main函数的char数组中..在看了很多问题和答案之后,这就是我得到的但是指针等对我来说很难理解..有人能告诉我这里做错了吗?
答案 0 :(得分:4)
按以下方式更改功能的返回类型
#include <stdio.h>
char ( *scanCode() )[6]
{
FILE *in_file;
int i = 0;
static char scan[9054][6];
in_file = fopen("message.txt", "r");
while (!feof(in_file))
{
fscanf(in_file, "%s", scan[i]);
i++;
}
return scan;
}
int main(void)
{
int hi[9053];
FILE *in_file;
in_file = fopen("message.txt", "r");
char ( *array )[6] = scanCode();
printf("%s", array[0]);
printf("%s", array[1]);
printf("%s", array[2]);
printf("%s", array[3]);
}
同样在printf语句中使用格式说明符%s
并改变函数中的循环,如
while ( i < 9054 && fscanf(in_file, "%5s", scan[i]) == 1 ) ++i;
答案 1 :(得分:2)
我更喜欢以这种方式简化代码:
#include <stdio.h>
#define NumLines 9054
#define NumCols 6
void freeMem(char **ele) {
while (*ele != NULL) {
free(*ele);
ele++;
}
}
char **scanCode(char *fileName)
{
FILE *in_file;
char readingFormat[128];
int i = 0;
/*
* Instead to declare a static variable I prefer to allocate dynamically
* the bidimensional array.
* It is done in two steps:
* 1. allocate the memory for the first dimension
* 2. for each element in this dimension allocate the memory for each element in the second dimension
*
*/
char **scan = (char **)malloc((NumLines + 1) * sizeof(char *));
if (scan == NULL) {
return NULL;
}
for (int j = 0; j < NumLines; j++) {
scan[j] = (char *)malloc(NumCols + 1);
if (scan[j] == NULL) {
freeMem(scan);
return NULL;
}
scan[j][0] = NULL;
}
scan[NumLines] = NULL; // define the end of memory
in_file = fopen(fileName, "r");
if (fopen == NULL) {
freeMem(scan);
return NULL;
}
sprintf(readingFormat, "%%%ds", NumCols);
while (fscanf(in_file, readingFormat, scan[i]) == 1 && i < NumLines) {
i++;
}
return scan;
}
int main(void)
{
char **array = scanCode("message.txt");
if (array == NULL) {
printf("ERROR\n");
exit(0);
}
for (char **tp = array; **tp != NULL; tp++) {
printf("%s\n", *tp);
}
}
答案 2 :(得分:2)
数组不是指针(再次向我问好)。
此:
static char scan[9054][6];
有你想要的最明显的类型 - 'char [9054] [6]'而不是'char **'。它拼写为6个元素的数组,每个元素是9054个字符的另一个数组。另一方面,类型'char **'拼写为'指向char的指针',你现在可能看到它们完全不同。
您的代码应如下所示:
#include <stdio.h>
typedef char yourArrayType[9054][6];
typedef struct { yourArrayType return_value; } letsReturnArraysType;
letsReturnArraysType scanCode()
{
FILE *in_file;
int i = 0;
yourArrayType scan;
in_file = fopen("message.txt", "r");
while (!feof(in_file))
{
fscanf(in_file, "%s", scan[i]);
i++;
}
return *(letsReturnArraysType*)scan;
}
int main(void)
{
int hi[9053];
FILE *in_file;
in_file = fopen("message.txt", "r");
letsReturnArraysType arrayStruct = scanCode();
printf("%s", arrayStruct.return_value[0]);
printf("%s", arrayStruct.return_value[1]);
printf("%s", arrayStruct.return_value[2]);
printf("%s", arrayStruct.return_value[3]);
}