在C中使用指针传递矩阵
编译时没有警告或错误,只是在尝试读取或写入矩阵值时以下代码崩溃 它需要传递给另一个函数,以便从中读取;函数createOutputLine 我怎样才能指出正确保存的数据,以便对其进行修改和读取?谢谢在C中使用指针传递矩阵,c,C,编译时没有警告或错误,只是在尝试读取或写入矩阵值时以下代码崩溃 它需要传递给另一个函数,以便从中读取;函数createOutputLine 我怎样才能指出正确保存的数据,以便对其进行修改和读取?谢谢 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> void createOutputFile(FILE*, int, char**); ch
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void createOutputFile(FILE*, int, char**);
char createOutputLine(int, int, char*, char**);
int main(int argc, char **argv) {
int removeComments = 0;
FILE *file;
if (argc > 1 && strcmp(argv[1], "-i") == 0) {
if (argc > 2) {
if (!(file = fopen(argv[2], "r"))) {
printf("Error: file not found");
return -1;
}
}
else {
printf("Error: no file specified");
return -1;
}
}
else {
printf("Error: command requires -i");
return -2;
}
createOutputFile(file, argc, argv);
fclose(file);
}
void createOutputFile(FILE *file, int argc, char **argv) {
fseek(file, 0, SEEK_SET);
char *data = (char*)malloc(2000);
FILE *header;
char name[20];
char *token = strtok(argv[2], ".");
strcpy(name, strcat(token, ".o"));
FILE *output = fopen(name, "w");
char constNames[10][15];
char **constValues[10][10];
int constsStored = 0;
while (fgets(data, 2000, file) != NULL) {
for (int i = 0; i < strlen(data); i++) {
int c = i;
bool linePrinted = false;
if (data[i] == '#' && data[i + 1] == 'd') {
for (c = i; c <= i + 7; c++) {
data[c] = '\0';
} int ch = 0;
while (data[c] != ' ') {
constNames[constsStored][ch] = data[c];
data[c] = '\0';
ch++;
c++;
} ch = 0;
while (data[c] != '\n') {
**constValues[constsStored][ch] = data[c]; //this line crashes
data[c] = '\0';
ch++;
c++;
}
if (data[c] == '\n') data[c] = '\0';
constsStored++;
}
for (int ch = 0; ch <= constsStored; ch++) {
if (data[i] == constNames[ch][0]) {
int ch2 = i + 1;
int ch3 = 1;
bool isConst = false;
while (data[ch2] != ' ') {
if (data[ch2] == constNames[ch][ch3] && isConst == false) isConst = true;
ch2++;
ch3++;
}
if (isConst || data[i + 1] == ' ') {
char line[200];
line[200] = createOutputLine(i, ch, data, **constValues);
fprintf(output, "%c", line[200]);
linePrinted = true;
}
}
}
if (!linePrinted)
fprintf(output, "%c", data[i]);
}
}
fclose(output);
free(data);
}
char createOutputLine(int i, int constElem, char *data, char **constValues) {
int ch = i;
int ch2 = 0;
char temp[200];
while (data[ch] != '\n' && data[ch] != ' ' && data[ch] != ';') {
temp[ch2] = data[ch];
printf("%c", data[ch]);
ch++;
ch2++;
}
char line[200];
ch2 = 0;
for (ch = i; ch <= sizeof(data); ch++) {
line[ch2] = data[ch];
ch2++;
}
for (ch = 0; ch <= 10; ch++) {
line[ch2] = constValues[constElem][ch];
ch2++;
}
for (ch = 0; ch <= sizeof(temp); ch++) {
line[ch2] = temp[ch];
ch2++;
}
line[ch2 + 1] = '\n';
return line[200];
}
在解除防护之前,指针应指向对象。句号 字符**常量值[10][10];只声明一个指向字符指针的2D数组。由于它是一个自动数组,既不是静态分配的,也不是动态分配的,所以它的指针只是未初始化 当您延迟使用**constValues[constsStored][ch]=data[c];,您试图取消引用一个未初始化的指针,该指针是明确的未定义行为。你很幸运能立即坠机,因为UB的后果显然是不相关的问题 通常的方法是声明对象数组,并使用这些对象的地址作为指针 这还不是全部:C数组不是一等公民。您不能分配给数组,也不能从函数返回它。这显然是错误的:
char line[200];
line[200] = createOutputLine(i, ch, data, **constValues);
它只是将函数返回的唯一字符指定给数组末尾
这就是:
char line[200];
...
return line[200];
它不返回数组C不允许它,但恰好位于数组前面的字节的值
我很抱歉,但有太多的错误,我要修复他们是这么长的程序
你可能会发现C很难,并寻求帮助。但是构建只包含您想要处理的内容的小代码。只有当这些小部件正常工作时,才尝试将它们组装到一个更大的程序中。此外,您应该先使用调试器缩小范围。警告:如果没有收到任何警告,数组索引200将超过包含200个元素的数组的末尾,然后你需要学习如何正确使用编译器,或者你需要一个更好的编译器。不管怎样,这是**constValues[constsStored][ch]你想把这一行加粗,还是你的实际源代码中的内容?2D数组与'char**constValues[10][10]没有任何关系;,而这又与char**constValues`无关。参见。注意,对于ch=i;中国