user935420
user935420

Reputation: 113

Assignment of function parameter has no effect outside the function

Why last line parameter maybe has no effect outside the function:

void save_last_frame( uint8_t *saveframe, uint8_t *curframe,
                             int width, int height, int savestride, int curstride )
{
    height /= 2;
    height--;
    while( height-- ) {
        blit_packed422_scanline( saveframe, curframe, width );
        saveframe += savestride;
        interpolate_packed422_scanline( saveframe, curframe, curframe + (curstride*2), width );
        saveframe += savestride;
        curframe += (curstride*2);
    }
    blit_packed422_scanline( saveframe, curframe, width );
    saveframe += savestride;
    blit_packed422_scanline( saveframe, curframe, width );
    saveframe += savestride;   // <-- Assignment of function parameter has no effect outside the function
}

Thanks

Upvotes: 2

Views: 4910

Answers (2)

Hgaur
Hgaur

Reputation: 109

You have passed the variable saveframe as a pointer; to change the value outside the function, do this:

*saveframe += savestride; 

instead. This way, your value will now be retained even after function exits.

Upvotes: 0

Adam Zalcman
Adam Zalcman

Reputation: 27233

In C parameters are essentially local variables which are initialized with values passed in as arguments. This means they exist only for as long as the function is being executed. Your saveframe variable ceases to exist once the function exists and with it the value you assigned.

In order to modify values existing outside the function you should use a pointer and modify the value pointed to by that pointer.

Since the value you're working with is a pointer already, you should use a pointer to pointer:

void save_last_frame( uint8_t **saveframe, uint8_t **curframe,
                             int width, int height, int savestride, int curstride )

You should then modify the code accordingly, replacing saveframe with *saveframe. Similarly for curframe if you also wish for it to be updated by the function.

An example of such "output pointer" argument is endptr used to record the end of parsed numeric string in strtol().

Upvotes: 1

Related Questions