我正在try 编译一个新的c程序,但收到以下错误,不知道如何处理它们.

[{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 49,
    "startColumn": 26,
    "endLineNumber": 49,
    "endColumn": 26
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 52,
    "startColumn": 37,
    "endLineNumber": 52,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 53,
    "startColumn": 33,
    "endLineNumber": 53,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 56,
    "startColumn": 37,
    "endLineNumber": 56,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 57,
    "startColumn": 33,
    "endLineNumber": 57,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 60,
    "startColumn": 37,
    "endLineNumber": 60,
    "endColumn": 37
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "comparison with string literal results in unspecified behavior [-Waddress]",
    "source": "gcc",
    "startLineNumber": 61,
    "startColumn": 33,
    "endLineNumber": 61,
    "endColumn": 33
},{
    "resource": "/c:/CodeExample/TESTPROG1.C",
    "owner": "cpptools",
    "severity": 4,
    "message": "pointer of type 'void *' used in arithmetic [-Wpointer-arith]",
    "source": "gcc",
    "startLineNumber": 64,
    "startColumn": 37,
    "endLineNumber": 64,
    "endColumn": 37
}]

我不是一个C程序员,我可以调试现有的程序,但创建一个新程序是一种学习经历.

任何帮助都将是有益的,并非常感激.

我真的不知道我需要改变什么.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define the structure for table1
typedef struct {
    char customer_code[31];
    char firstname[101];
    char lastname[101];
} table1_t;

// Define the structure for table2
typedef struct {
    char customer_code[31];
    char invoice_code[31];
    float amount;
    char date[11]; // Assuming DATE is in 'YYYY-MM-DD' format
} table2_t;

// Define the structure for table3
typedef struct {
    char invoice_code[31];
    char item_code[31];
    float amount;
    int quantity;
} table3_t;

// Define the structure for table4
typedef struct {
    char customer_code[31];
} table4_t;

int rows;

// Function to read CSV file and populate the table name passed
int parse_csv(const char *filename, void *table, const char *structure) {
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        perror("Error opening file");
        return -1;
    }

    // Skip the first record
    char line[4096];
    fgets(line, sizeof(line), fp);

    int count = 0;
    while (fgets(line, sizeof(line), fp)) {
        if (structure == "table1_t") {
            table1_t *t1 = (table1_t *)table;
            sscanf(line, "%[^,],%[^,],%s", t1->customer_code, t1->firstname, t1->lastname);
            table += sizeof(table1_t);
        } else if (structure == "table2_t") {
            table2_t *t2 = (table2_t *)table;
            sscanf(line, "%[^,],%[^,],%f,%s", t2->customer_code, t2->invoice_code, &t2->amount, t2->date);
            table += sizeof(table2_t);
        } else if (structure == "table3_t") {
            table3_t *t3 = (table3_t *)table;
            sscanf(line, "%[^,],%[^,],%f,%d", t3->invoice_code, t3->item_code, &t3->amount, &t3->quantity);
            table += sizeof(table3_t);
        } else if (structure == "table4_t") {
            table4_t *t4 = (table4_t *)table;
            sscanf(line, "%[^,]", t4->customer_code);
            table += sizeof(table4_t);
        } else {
            fprintf(stderr, "Invalid table structure\n");
            fclose(fp);
            return -1;
        }
        count++;
    }

    fclose(fp);
    return count;
}

int main() {
    // Process table1
    table1_t table1[500000];
    rows = parse_csv("table1.csv", table1, "table1_t");
    printf("Number of rows in table1: %d\n", rows);

    //Process table2
    table2_t table2[1000000];
    rows = parse_csv("table2.csv", table2, "table2_t");
    printf("Number of rows in table2: %d\n", rows);

    //Process table3
    table3_t table3[5000000];
    rows = parse_csv("table3.csv", table3, "table3_t");
    printf("Number of rows in table3: %d\n", rows);   

    //Process table4
    table4_t table4[1000];
    rows = parse_csv("table4.csv", table4, "table4_t");
    printf("Number of rows in table4: %d\n", rows);   

    return 0;
}

推荐答案

您有两个问题:

首先,您通过structure == "table1_t"(以及其他)与字符串进行比较.用strcmp(structure, "table1_t")代替.对于早先直接与字符串进行比较的其他实例,请执行此操作.

其次,你有你的void *table参数,当你做像这样的事情

table += sizeof(table1_t);

then you effectively say that you want to move the pointer of table further in memory by a magnitude you increment it with. See more about this here: warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]|

C++相关问答推荐

ISO_C_BINDING,从Fortran调用C

为什么已经设置的值在C中被重置为for循环条件中的新值?

如何将FileFilter添加到FileDialog GTK 4

返回一个包含数组的 struct

在C中使用动态内存分配找到最小的负数

如何调试LD_PRELOAD库中的构造函数?

仅在给定的大小和对齐方式下正确创建全局

二进制计算器与gmp

非正规化边缘毛刺

是否需要包括<;errno.h>;才能使用perror?

C语言中MPI发送接收字符串时出现的分段错误

C";中的ANN运行时判断失败#2-变量outputLayer;周围的堆栈已损坏.运行后出错

OSDev--双缓冲重启系统

隐藏测试用例无法在c程序中计算位数.

不兼容的整数到指针转换传递';char';到类型';常量字符*

在C中使用字符串时是否不需要内存分配?

Ubuntu编译:C中的文件格式无法识别错误

为什么 int32_t 和 int16_t 在 printf 输出中具有相同的位数?

clion.我无法理解 Clion 中发生的 scanf 错误

与 C 相比,C++ 中无副作用的无限循环的好处是 UB?