Ayushman
Ayushman

Reputation: 11

getting the warning : cast from pointer to integer of different size while cnverting string to integer array and printing unrelated numbers

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

typedef struct arrays {

    char air[28];
    char water[28];
    char land[28];
    char c9[28];
    char c11[28];
    char c13[28];
}arr;

int main()
{
    FILE* fp = fopen("ass6file.csv", "r");
    if (!fp)  {
        printf("Can not open file");
        return 0;
    }
    char buff[1024];
    int row = 0;
    int col = 0;
    int i = 0;
    arr value[999];

    while (fgets(buff, 1024, fp))  {
        col = 0;
        row++;
        if (row == 1)
            continue;

        char* column = strtok(buff, ",");
        while (column)  {
            if (col == 1)
                strcpy(value[i].air, column);
            if (col == 2)
                strcpy(value[i].water, column);
            if (col == 3)
                strcpy(value[i].land, column);
            if (col == 4)
                strcpy(value[i].c9, column);
            if (col == 5)
                strcpy(value[i].c11, column);
            if (col == 6)
                strcpy(value[i].c13, column);
            column = strtok(NULL, ",");
            col++;
        }
        i++;
    }
    fclose(fp);
    int sum1[28];
    /*char sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,k;
    int var1,var2,var3,var4,var5,var6;
    int m1,m2,m3,m4,m5,m6;
    int s1=0,s2=0,s3=0,s4=0,s5=0,s6=0;*/
    for (int k = 0; k < 28; k++)
        sum1[k] = (int)value[k].air;
    for (int k = 0; k < 28; k++)
        printf("%d\n", sum1[k]);

    return 0;
}

The warning I am getting is:

filh.c: In function ‘main’:

filh.c:62:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   62 |  sum1[k]= (int)value[k].air;
      |           

I am new to C. Can anyone please help its printing big numbers that are not in the file?

like--

1966677344

1966677512

1966677680

1966677848

1966678016

1966678184

1966678352

1966678520

1966678688

1966678856

1966679024

1966679192

1966679360

1966679528

1966679696

1966679864

1966680032

1966680200

1966680368

1966680536

1966680704

1966680872

1966681040

1966681208

1966681376

1966681544

1966681712

1966681880

Upvotes: 1

Views: 115

Answers (1)

Craig Estey
Craig Estey

Reputation: 33631

You are reading in columns from the .csv file as char arrays.

But, when doing your sum, you [probably] want to convert them to an int.

Just casting them to int (e.g. using (int)) won't work.

When you say: arr[i].air this is a pointer to the character data stored in the air field.

Serendipitously, the compiler flagged this because you're on a 64 bit machine and the size of an int variable is 32 bits but a pointer is 64 bits

To convert a string representation of a number to an actual number (e.g. we want an int value), we need to use the atoi [or strtol] function instead of casting.

Change:

sum1[k] = (int) value[k].air;

Into:

sum1[k] = atoi(value[k].air);

UPDATE:

i wanna perform arithmatic operations on the elements of array , find mean, variance etc of the respective columns, file is related to data science

Okay. So, I assume that all input data are numbers. We can refactor the struct to use integers. This means we can [using atoi et. al.] store the data as numbers once and then operate on it multiple times without having to reconvert it for each operation.

I assume that the input values are integers. If some/all of them can be fractional (e.g. 4.37), we can define INPUT_IS_FLOAT in the example code below.

your program works but its converting numbers with decimals to integers that will not give accurate results to me as i wanna calculate mean and other things and the data wont remain accurate

That is easily solved by using floating point variables for the sum, mean, variance, etc. This will work regardless if the input data is stored as an integer or floating point number

Here's some refactored code to compute the sum and mean of the air column. You can add similar code for the other fields/columns. Or, whatever values you need to compute:

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

#ifdef INPUT_IS_FLOAT
typedef double data_t;
#else
typedef long long data_t;
#endif

typedef struct arrays {
    data_t air;
    data_t water;
    data_t land;
    data_t c9;
    data_t c11;
    data_t c13;
} arr;

int
main()
{
    FILE *fp = fopen("ass6file.csv", "r");

    if (! fp) {
        printf("Can not open file");
        return 0;
    }

    char buff[1024];

    int count = 0;
    int col;
    arr value[999];

    arr *acur;

    // skip over column headers
    fgets(buff, sizeof(buff), fp);

    while (fgets(buff, sizeof(buff), fp)) {
        acur = &value[count++];

        col = 0;
        char *column = strtok(buff, ",");

        while (column) {
#ifdef INPUT_IS_FLOAT
            data_t curval = strtod(column,NULL,10);
#else
            data_t curval = strtoll(column,NULL,10);
#endif

            switch (col) {
            case 1:
                acur->air = curval;
                break;
            case 2:
                acur->water = curval;
                break;
            case 3:
                acur->land = curval;
                break;
            case 4:
                acur->c9 = curval;
                break;
            case 5:
                acur->c11 = curval;
                break;
            case 6:
                acur->c13 = curval;
                break;
            }

            column = strtok(NULL, ",");
            col++;
        }
    }
    fclose(fp);

    // sum all 'air' elements
    double sum = 0;
    for (acur = &value[0];  acur < &value[count];  ++acur)
        sum += acur->air;
    printf("sum of air: %g\n",sum);

    double mean = sum / count;
    printf("mean of air: %g\n",mean);

    return 0;
}

Upvotes: 2

Related Questions