EN VI

I compiled a new c program and received the following errors?

2024-03-10 01:00:08
How to I compiled a new c program and received the following errors

I am attempting to compile a new c program and received the following error and don't know how to deal with them.

[{
    "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
}]

I am not a C programmer i can deal with debugging existing programs but creating a new program is a learning experience.

Any assistance would be helpful and much appreciated.

I don't really know what i need to change.

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

Solution:

You have two issues:

first, you compare to string literal, via structure == "table1_t" (among others). Use strcmp(structure, "table1_t") instead. Do this for other instances where you earlier compared with string literals directly.

second, you have your void *table parameter and when you do things like

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]|

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login