[En-Nut-Discussion] Static char array return problem

Thiago A. Corrêa thiago.correa at gmail.com
Wed Jul 12 21:15:42 CEST 2006


Hi,

the problem is that you are calling the function twice before it enters the
fprintf :)

the order is:
result1 = eRString()
result2 = eRString()
fprintf( stream, "string", result1, result2 );

fprintf sees the result1 and result2 as pointers, and since you return an
static address, they are the same, thus the second call overwrides the first
one. You could malloc a temp memory block and copy the result from the first
call then free it after the fprintf call.

ie:
char* temp = malloc( 30 );
strcpy( temp, eRString(...) );
fprintf( stream, "...", temp, eRString(...) );
free( temp );

Hope this helps :)

Regards,
   Thiago A. Corrêa


On 7/12/06, ivesworking <ivesworking at hotmail.com> wrote:
>
> to simplify eeprom calling, i use this function
>
> char *eRString(u_int add,u_int range){
>    int i;
>    static char buffer[33];
> for(i=0;i<range;i++)
>             buffer[i]=eeprom_read_byte((void *) (add+i));
> buffer[range]=0;
>   return buffer;
> }
>
> however when i start repeat calling this function, something when wrong
>
> when i call the with fprintf
>
> fprintf(stream,"1:%s,2:%s,eRString(erom11,16),eRString2(erom12,32));
>
> it seem the second string on printf  repeat  the result of
> eRString(erom11,16)
>
> any idea what is wrong on my program ? or gcc or some pointer problem ?
>
> wfloh
>
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
>



More information about the En-Nut-Discussion mailing list