warning: function returns address of local variable [-Wreturn-local-addr]

前端 未结 4 676
Happy的楠姐
Happy的楠姐 2020-12-22 10:58

I get this error when compiling and have checked out other questions here with no further progress:

funciones.c: In function ‘Lyapunov’: ../funciones.

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 11:41

    unsigned char rgb[3];
    

    is local to function and the scope of this array is within the function Lyapunov so once you exit the function this memory is no more valid. So the warning you are getting is the right one which says never return the local variables address which will lead to undefined behavior when used outside its scope.

    Have your memory allocated of heap and then return the pointer as shown below.

    unsigned char *rgb = malloc(3);
    

提交回复
热议问题