Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

c - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...