0

I'm trying to input a 2D array into a function. I don't know the number of rows or columns to this array and it was loaded into c++ via CImg. This is what I have:

// Main function:
int main()
{
    int rows, columns;
    float summation;
    CImg<unsigned char> prettypicture("prettypicture.pgm");
    rows = prettypicture.height();
    columns = prettypicture.width();

    summation = SUM(prettypicture[][], rows, columns);
}

// Summation function:
float SUM(int **picture, int rows, int column)
{
... // there is some code here but I don think this is important.
}

I would like to pass the array into the summation function and I'm aware that I should be using pointers in some way, but I'm not sure how to do this. Any help would be much appreciated.

Thank you

(sorry for being a noob)

2 Answers 2

1

Try this:

summation = SUM(prettypicture.data(), rows, columns);

and make your SUM function look like this:

float SUM(char* picture, int rows, int column) ...

You need to pass in data (if you want a pointer to the data) because that's what CImg provides. It's a pointer to character because that's the kind of CImg you have; and it's a char*, not char**, because that's what data provides.

You didn't show us the inside of the SUM function, so I wonder if you might do well to pass in the CImg instead of just its data, and call the member function atXY that takes a position. Hard to say without seeing more.

For more information on data and other member functions of CImg, see http://cimg.eu/reference/structcimg__library_1_1CImg.html .

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, just what I needed :D
0

why not pass it as reference?

summation = SUM(prettypicture);

float SUM(const CImg<unsinged char>& picture) {
  // ...
}

Comments

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.