[英]How to copy char array to a double pointer in C
Im writing a fairly simple program to read a file line by line and store it into an array of lines, my program compiles fine but it crashes everytime I run it. 我编写了一个相当简单的程序来逐行读取文件并将其存储到行数组中,我的程序可以正常编译,但是每次运行它都会崩溃。 This is my code:
这是我的代码:
#include <stdio.h>
#include <string.h>
#define LINESIZE 512
typedef struct {
char **data;
size_t nused;
} lines_t;
lines_t readlines(FILE *fp);
int main(int argc,char* argv[]) {
FILE *fp;
(void)argc;
if((fp = fopen(argv[1],"r+")) == 0) {
perror("fopen");
}
readlines(fp);
return 0;
}
lines_t readlines(FILE *fp) {
lines_t line_data;
char line[LINESIZE];
char temp[20];
int num_lines = 0;
(*line_data.data) = (char *)malloc(LINESIZE);
while(fgets(line,LINESIZE,fp)) {
sscanf(line,"%s\n",temp);
strcpy(line_data.data[num_lines], temp); /* Program crashes here */
num_lines++;
}
return line_data;
}
The line where I try to copy my array is giving me trouble, So my question is, How do I copy my character array temp
into the char **data
inside struct lines_t
if I am not doing it right? 我尝试复制数组的行给我带来麻烦,所以我的问题是,如果我做的不正确,如何将我的字符数组
temp
复制到struct lines_t
内部的char **data
?
You are dereferencing an invalid pointer here: 您在这里取消引用无效的指针:
(*line_data.data) = (char *)malloc(15);
line_data.data
is a char **
. line_data.data
是char **
。 You are trying to deference it but it is not yet set to any meaningful value. 您正在尝试尊重它,但尚未将其设置为任何有意义的值。 You need to allocate memory for
line_data.data
before you allocate memory for *line_data.data
. 在为
*line_data.data
分配内存之前,您需要为line_data.data
分配内存。
(char *)malloc(15)
is particularly suspicious also. (char *)malloc(15)
也特别可疑。 Where does 15
come from and what are you actually allocating memory for? 15
是从哪里来的?您实际上是在分配什么? Casting the result of malloc
is generally considered bad practice, and in your case, rightly so, because malloc
is declared in stdlib.h
and you aren't including that header. 强制转换
malloc
的结果通常被认为是不好的做法,在您的情况下,正确的做法是,因为malloc
是在stdlib.h
声明的,并且您不包括该标头。 If you want to allocate enough space to hold 15 char *
, then use malloc(15 * sizeof(char *))
or alternatively, malloc(15 * sizeof(*line_data.data))
(here it is safe to use *line_data.data
even if it doesn't point to anything, because sizeof
does not evaluate its operand). 如果要分配足够的空间来容纳15个
char *
,请使用malloc(15 * sizeof(char *))
或使用malloc(15 * sizeof(*line_data.data))
(在这里可以安全地使用*line_data.data
即使它没有指向任何内容,因为sizeof
不会评估其操作数)。
You can try malloc line_data.data
before strcpy
. 您可以在
strcpy
之前尝试使用malloc line_data.data
。
lines_t readlines(FILE *fp) {
lines_t line_data;
line_data.data = malloc(LINESIZE);
*line_data.data = malloc(LINESIZE);
return line_data;
}
You first need to allocate memory. 您首先需要分配内存。
You need to allocate both for the array of strings and string array. 您需要同时分配字符串数组和字符串数组。 Here you need to have an upper limit while calling a malloc.
在这里,调用malloc时需要有一个上限。
line_data = malloc(10* sizeof(char*)); // for example here the upper limit is 10
while(fgets(line,LINESIZE,fp)) {
sscanf(line,"%s\n",temp);
line_data.data[num_lines] = malloc(sizeof(char) * (strlen(temp)+1));
strcpy(line_data.data[num_lines], temp); /* Program crashes here */
num_lines++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.