c - 在 C 中使用指针传递矩阵

标签 c

编译时没有警告或错误,只是当我尝试读取或写入矩阵 constValues 时,以下代码崩溃。

它需要传递给另一个函数来读取;函数createOutputLine。

如何指向正确保存的数据以便对其进行修改和读取?谢谢。

#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];
}

最佳答案

在取消引用之前,指针应指向对象。句号。

char **constValues[10][10]; 只是声明一个指向字符指针的二维指针数组。由于它是一个自动数组(既不是静态分配也不是动态分配),所以它的指针只是未初始化。

当您稍后使用 **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 很难并寻求帮助。但是构建只包含您想要处理的内容的小代码。并且只有当这些小片段正常工作时,才尝试将它们组装成一个更大的程序。

关于c - 在 C 中使用指针传递矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54945533/

相关文章:

c - 使用 openmp 看不到任何加速

c++ - zip 文件可以是稀疏的/不连续的吗?

c - 在共享库中使用 __wrap_malloc

c - 哪个内核函数调用了用户空间中实现的 timer_settime() 和定时器处理程序?

c - 多线程编程使用全局变量时出现的奇怪问题

c - 过度使用 realloc

c - 链接到 libGL 添加了对 NVidia 库的引用

c - 工作队列启动、中断和从未完成导致 CPU 停顿

c - 比较 C 中链表中的值时出现问题

c - 使用 C,一段代码,多个 header if/else?