1

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

 }
4
  • 1
    I get more errors than that when I compile it. Commented Nov 21, 2014 at 0:19
  • There are a lot more errors. I don't want people doing out my entire code for me just needed help fixing that error haha Commented Nov 21, 2014 at 0:20
  • 1
    You've defined add to take the whole array as its first argument, but then you call it passing it only one element (which is also outside the bounds of the array--another problem). Commented Nov 21, 2014 at 0:27
  • Well now that I am sending the whole array into the function, that error clears up but I am now getting this error: Homework3.c:41: error: expected ‘)’ before ‘[’ token Homework3.c:56: error: expected ‘)’ before ‘[’ token Commented Nov 21, 2014 at 0:34

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

How would the prototype definitions look like then?
Also from doing that I am now getting this error Homework3.c:41: error: expected ‘)’ before ‘[’ token Homework3.c:56: error: expected ‘)’ before ‘[’ token
@TheMadHouse Prototypes should always match the function definition. The only difference should be the semicolon at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.