Arrays, pointers and functions problem

Hi all, I'm struggling to get a function working, and hoping someone could help.

The idea is to have a function which I can call to load all my image files. Everything worked until I added surface pointers to the function, which I need because I intend to move this function to a difference source file, so can't act on the surfaces directly, so need to use pointers.

I have this declared globally:
1
2
3
const int numberOfImages = 2;
std::string imageFileName[numberOfImages] = {"strip.png","background.png"};
SDL_Surface *surface[numberOfImages];


This is my function:
1
2
3
4
5
6
7
8
9
10
bool load_images(int number, std::string fileName[], SDL_Surface *surfacePointer)
{
    for (int n=0; n<number; n++)
    {
        surfacePointer[n] = load_image( fileName[n].c_str(), R_INVIS, G_INVIS, B_INVIS );
        if surfacePointer[n] == NULL ){ return false; }
    }

    return true;
}


and this is my call to the function in main():
if( load_images(numberOfImages,imageFileName,surface) == false ){ return 1; }

So the problem is in assigning the surfaces in the function. I keep getting type mismatch issues and can't work out where I've gone wrong. The surfaces are defined as an array of pointers in my main source file. When the function is called it receives the pointer to the first element of the array. The function accepts type SDL_Surface pointer. Load_image returns an SDL_Surface pointer.
Originally I had the function set up like this, if this helps to clarify my objective:

1
2
3
4
5
6
7
8
9
10
bool load_images(int number, std::string fileName[])
{
    for (int n=0; n<number; n++)
    {
        surface[n] = load_image( fileName[n].c_str(), R_INVIS, G_INVIS, B_INVIS );
        if surface[n] == NULL ){ return false; }
    }

    return true;
}
Oh, I've got it working. Am I right in thinking that when a function receives an array, it actually just receives a pointer to that array? I was reluctant to write the function this way because I thought I'd be sending more data across than needed (i.e. the full array rather than just a single pointer to it)

Anyway, here's the working code:

1
2
3
4
5
6
7
8
9
10
11
bool load_images(int number, std::string fileName[], SDL_Surface* surfaces[])
{
    //Load the image
    for (int n=0; n<number; n++)
    {
        surfaces[n] = load_image( fileName[n].c_str(), R_INVIS, G_INVIS, B_INVIS );
        if ( surfaces[n] == NULL ){ return false; }
    }

    return true;
}

if( load_images(numberOfImages,imageFileName,surface) == false ){ return 1; }

If anyone has any comments I'd be glad to hear them.
Topic archived. No new replies allowed.