我正在尝试从C中的文本文件读入。我尝试创建的方法将返回一个字符串数组,其中文本文件中的每一行都是数组的索引。我无法弄清楚我做错了什么,虽然我确信它很简单。
// returns an array of strings from a text file; lines is the number of lines
char *file_array(int lines)
{
char text[50][150],buffer[150];
int i=0;
FILE *file_in;
file_in=fopen("test.txt","r");
if (file_in == NULL) {
printf("Error opening file\n");
}
while (fgets(buffer,150,file_in)) {
strcpy(text[i],buffer);
i++;
}
fclose(file_in);
return text;
}
int main()
{
//totalLines is used to get the sizeof the array; this is the number of lines
//in the text file
int totalLines;
totalLines = lines();
char *strings_from_file[totalLines];
strings_from_file = file_array(totalLines);
printf("index 2 of text file: %s",strings_from_file[2]);
return 0;
}
编译时出现此错误
io.c:110:20: error: incompatible types when assigning to type ‘char *[(sizetype)(totalLines)]’ from type ‘char *’ strings_from_file = file_array(totalLines);
^
fabio93@fabio93:~/Documents/c_apps/io_test$ gcc -o str_arr_file io.c
io.c: In function ‘file_array’:
io.c:86:2: warning: return from incompatible pointer type [enabled by default]
return text;
^
io.c:86:2: warning: function returns address of local variable [-Wreturn-local-addr]
io.c: In function ‘main’:
io.c:110:20: error: incompatible types when assigning to type ‘char *[(sizetype)(totalLines)]’ from type ‘char *’ strings_from_file = file_array(totalLines);
我认为自己是C的初学者,所以请帮助我。谢谢。
答案 0 :(得分:3)
在你的函数中,你在堆栈上创建变量,当函数返回时会丢失。所以return text
是未定义的行为。
更好的方法是使用malloc
在堆上分配空间并返回。
char **file_array(int lines)
{
char **text;
text = malloc(sizeof(char*)*50); //no of strings
for(int i=0;i<50;i++)
text[i] = malloc(sizeof(char)*150); ..no of chars in each string
//do things with text..you can use in same way as your example
return text;
}
在主程序中,使用双指针
char ** strings_from_file;
strings_from_file = file_array(totalLines);
答案 1 :(得分:0)
如果你必须通过一个函数使用双指针(传递双指针作为参数又名三指针!),这里是我很久以前写的一些代码,专门解决这个问题
我希望对那些有双/三指针问题的人有用。请注意,我使用的是整数,但这个想法也适用于其他类型。
我认为需要注意的重要事项是以下语法
int ***coco_a
*coco_a = (int **) calloc(ROW, sizeof(int *));
(*coco_a)[i] = (int *) calloc(COL, sizeof(int));
scanf("%d", &(*coco_a)[i][j]);
我花了很长时间才意识到&(*coco_a)
有效且&*coco_a
(或coco_a
)不起作用!
这是测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 2
#define COL 2
void aloca(int ***coco_a)
{
*coco_a = (int **) calloc(ROW, sizeof(int *));
int i;
for(i=0; i<ROW; ++i) {
(*coco_a)[i] = (int *) calloc(COL, sizeof(int));
int j;
for(j=0; j<COL; ++j) {
scanf("%d", &(*coco_a)[i][j]);
}
}
}
int main(int argc, char *argv[]) {
int **coco_m;
aloca(&coco_m);
int i, j;
/*for(i=0; i<ROW; ++i)
for(j=0; j<COL; ++j)
scanf("%d", &coco_m[i][j]);*/
for(i=0; i<ROW; ++i)
for(j=0; j<COL; ++j)
printf("coco[%d][%d] = %d\n", i, j, coco_m[i][j]);
return 0;
}
以下说明了连续与非连续双指针的用法。
#include <stdlib.h>
#include <stdio.h>
#define ROW 2
#define COL 3
int main(void)
{
int i, j, val = 0;
/* Allocation and assignment */
int **doub_pointer = (int **) calloc(ROW, sizeof(int *));
printf("\ndoub_pointer = %d\n", doub_pointer);
for(i=0; i < ROW; ++i) {
doub_pointer[i] = (int *) calloc(COL, sizeof(int));
if(i == 0) printf("*doub_pointer = %d\n", *doub_pointer);
for(j=0; j < COL; ++j) {
doub_pointer[i][j] = val++;
printf("&doub_pointer[%d][%d] = %d\n", i, j, &doub_pointer[i][j]);
}
}
/* Printing */
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
printf("doub_pointer[%d][%d] = %d\n", i, j, doub_pointer[i][j]);
}
}
printf("sizeof(doub_pointer) = %d\n", sizeof(doub_pointer)/sizeof(int));
printf("sizeof(doub_pointer[0]) = %d\n", sizeof(doub_pointer[0])/sizeof(int));
/* Freeing */
for(i=0; i < ROW; ++i) free(doub_pointer[i]);
free(doub_pointer);
/*******************************************************************/
val = 0;
/* Allocation and assignment */
int (*pointer)[COL] = (int (*)[COL]) calloc(ROW, sizeof(int [COL]));
printf("\npointer = %d\n", pointer);
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
pointer[i][j] = val++;
printf("&pointer[%d][%d] = %d\n", i, j, &pointer[i][j]);
}
}
/* Printing */
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
printf("pointer[%d][%d] = %d\n", i, j, pointer[i][j]);
}
}
printf("sizeof(pointer) = %d\n", sizeof(pointer)/sizeof(int));
printf("sizeof(pointer[0]) = %d\n", sizeof(pointer[0])/sizeof(int));
/* Freeing */
free(pointer);
/*******************************************************************/
int true_2D[ROW][COL];
printf("\nsizeof(true_2D) = %d\n", sizeof(true_2D)/sizeof(int));
return 0;
}