我有一个解析文本文件并将其存储在指针数组中的程序。我只有一个问题。我试图在一个char **
对象中存储一个字符串数组,但是每当我给char **
赋值时,就会发生seg故障。将值赋给char ** seg错误
#include "database.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char **get_values(int recipe_num, char *file) {
int placehold_num=recipe_num;
char *text=parse_recipes(file);
int num_recipes=count_recipes(file);
char **array_strings;
int index=-1;
for (int i=0;*(text+i)!='\0';i++) {
if (*(text+i)=='R' && *(text+i+1)=='e' && *(text+i+6)==':' && (text+i+7)==' ') {
i+=13;
index++;
for (int j=0;*(text+i+j-1)!='\n';j++) {
printf("%c",*(text+i+j));
*(*(array_strings+index)+j)=*(text+i+j);
}
}
}
}
这打印出下一行,我从*(text+i+j)
想要的字符,但赛格故障。我非常确定这不是一个被调用的函数的问题,我认为它必须是我提供的方式array_strings
。任何帮助是极大的赞赏。
请发表[mcve]。你的调试器告诉你什么? – melpomene
你从来没有为'array_strings'指定任何内存。 – Barmar
'text + i + j'不是访问二维数组元素的正确方法。它应该是'text +我* row_size + j' – Barmar