1

为什么最后一行参数可能在函数之外没有影响:

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
}

谢谢

4

2 回答 2

1

在 C 中,参数本质上是局部变量,它们使用作为参数传入的值进行初始化。这意味着它们仅在函数正在执行时才存在。一旦函数存在并且您分配的值随之存在,您的saveframe变量将不复存在。

为了修改函数外部存在的值,您应该使用指针并修改该指针指向的

由于您正在使用的值已经是一个指针,因此您应该使用指向指针的指针:

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

于 2013-06-16T12:41:08.453 回答
0

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.

于 2013-08-24T06:34:39.813 回答