简体   繁体   中英

makes pointer to integer without cast

I'm having a bit of a problem with my code. The compiler is telling me that my code is making a pointer to an integer without a cast. Now I'm not familiar with what this means so maybe someone could point (pun very intended) me in the right direction. Also any general tips would be much appreciated!

The exact error is:

Homework3.c: In function ‘main’:
Homework3.c:32: warning: passing argument 1 of ‘add’ makes pointer from integer without a cast
Homework3.c:16: note: expected ‘int (*)[5]’ but argument is of type ‘int’

 #include <stdio.h>
 #include <stdlib.h>
 #define INPUT_FILE_NAME "amounts.txt"
 #define PRODUCTS 5
 #define SALESPERSON 4
 #define AMOUNT 1449
 #define TOTALS 20

 void printOut(int [SALESPERSON][PRODUCTS], float [TOTALS]);
 float add(int [SALESPERSON][PRODUCTS], float, float [TOTALS] );

 int main(void){

    FILE *inputFile;
    int sales[SALESPERSON][PRODUCTS];

    float data = 0.0;
    float totals[TOTALS];
    int count;

    inputFile = fopen(INPUT_FILE_NAME, "r");
    for ( count = 0; count < AMOUNT; count++){
        fscanf(inputFile, "%d", sales[SALESPERSON]);
        fscanf(inputFile, "%d", sales[SALESPERSON][PRODUCTS]);
        fscanf(inputFile, "%f", &data);
        add(sales[SALESPERSON][PRODUCTS], data, totals[TOTALS]);
    }
    fclose(inputFile);

    printOut(sales[SALESPERSON][PRODUCTS], totals[TOTALS]);

    return 0;
 }

 void printOut(sales[SALESPERSON][PRODUCTS], totals[TOTALS]){

    int count2 = 0;
    int count3 = 0;
    int count4 = 0;

    for(count2 = 0; count2 < SALESPERSON; count2++){    
        for(count3 = 0; count3 < PRODUCTS; count3++){
            printf("Sales Person %d \t Product%d \t Total: %.2f\n", count2, count3, totals[count4]);
            ++count4;
        }
    }
    return ;
 }

 void add(sales[SALESPERSON][PRODUCTS], data, totals[TOTALS]){


    if(sales[SALESPERSON] == 1){
        if(sales[SALESPERSON][PRODUCTS] == 1){
            totals[0] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 2){
            totals[1] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 3){
            totals[2] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 4){
            totals[3] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 5){
            totals[4] += data;
        }
    }
    else if(sales[SALESPERSON] == 2){
        if(sales[SALESPERSON][PRODUCTS] == 1){
            totals[5] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 2){
            totals[6] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 3){
            totals[7] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 4){
            totals[8] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 5){
            totals[9] += data;
        }
    }
    else if(sales[SALESPERSON] == 3){
        if(sales[SALESPERSON][PRODUCTS] == 1){
            totals[10] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 2){
            totals[11] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 3){
            totals[12] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 4){
            totals[13] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 5){
            totals[14] += data;
        }
    }
    else if(sales[SALESPERSON] == 4){
        if(sales[SALESPERSON][PRODUCTS] == 1){
            totals[15] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 2){
            totals[16] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 3){
            totals[17] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 4){
            totals[18] += data;
        }
        else if(sales[SALESPERSON][PRODUCTS] == 5){
            totals[19] += data;
        }
    }

 }

sales[SALESPERSON][PRODUCTS] returns the integer at position PRODUCTS of the array referenced at position SALESPERSON in sales . You just need to pass sales :

add(sales, data, totals); //same thing with totals

You also don't give the length of the array for the function, so your definition should just be:

void add(int[][] sales, float data, float[] totals)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM