Hassaan Ahmed
Hassaan Ahmed

Reputation: 5

Error Passing Arrays as Function Arguments

I am getting error when I pass an array to a function

Array: int red_eachpix[MAX_PIX] = { 0 }

Function:

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++;
}

Error: error C2664: 'void sum_individualpix(int,int)' : cannot convert argument 1 from 'int [255]' to 'int'

The whole program is given below:

#define MAX_PIX 255
#define WIDTH 25
#define HEIGHT 25

void sum_individualpix(int , int);    // I think the error is here

int main()
{
    int X, Y, red, counter = 0;
    int red_eachpix[MAX_PIX] = { 0 }, Red[WIDTH][HEIGHT] = { 0 };
    long sum_red = 0;
    in.open("basicval.txt");
    if (in)
    {
        in >> X >> Y >> red;  //Data is in form of:   X  Y    B1(red pixel value)
        while (!in.eof())
            {
                counter++;
                sum_red += red;

                Red[X][Y] = red;

                sum_individualpix(red_eachpix, red);    //Getting Error here

                in >> X >> Y >> red;
             }

         double avg_red = (double)sum_red / counter;
         cout << "Average Value for Red = " << avg_red << endl;
    }
    in.close();
    getchar();
}

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++;
}

Just in case anyone wants to know the program reads the pixel values of an image, while the image itself has been converted into ASCII values by a program

Edited: Actually the Red[WIDTH][HEIGHT] & int X, Y, red, counter = 0; was not a mistake. I wrote it wrong accidentally.

Upvotes: 0

Views: 522

Answers (3)

Hassaan Ahmed
Hassaan Ahmed

Reputation: 5

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++; //Shows that first argument type is array
}

The error is with the function declaration void sum_individualpix(int , int) the first (argument) data type specifier is actually an array (int[]) not an integer(int) so the declaration should be changed to void sum_individualpix(int[], int)

Upvotes: 0

Moha the almighty camel
Moha the almighty camel

Reputation: 4483

you have int X, Y, red, counter = 0;, an int variable called red
also red[WIDTH][HEIGHT] = { 0 }; int array called red

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

You defined two times the same name 'red':

int X, Y, red, counter = 0;
int red_eachpix[MAX_PIX] = { 0 }, red[WIDTH][HEIGHT] = { 0 };

Upvotes: 1

Related Questions