Why do not work?, is the same.
Fco. J
rootag@terra.es
Tue Sep 13 22:43:00 GMT 2005
> The statement tmp.str = marca(...).str copies the CONTENTS of the string
> value, returned by the function which is ok, since when returning a variable
> of type struct Marca is shipped through the stack.
>
> The second form in block B this variable will also ships through the stack.
> This time not the CONTENTS of the string is copied but the pointer to str
> field of the variable that has been created on the stack frame is given as an
> argument to printf. The problem is, that while printf executes the stack
> frame contains the printf arguments. The stack frame of the previously
> executed marca function has been deleted at this time. But you still point
> to this stack frame by trading the str field pointer.
>
> You should be carefull exchanging such objects through the stack. It is much
> better to trade object references:
>
> int marca( struct Marca* x, const char* y, int v) -> operate on x->str
> and use the integer return value as an error indicator. You are on the save
> side.
>
> int main (...) {
> int ret=0;
> struct Marca marca;
>
> if ( 0 == (ret=marca(&marca,"bla.html",1)) ) printf( "OK=%s\n", marca.str);
> return ret;
> }
Thanks,
OK field pointers are missed by GCC while evaluate mother function.
But functions that return (char *) works shipping through the stack, like ctime,
printf("%s",ctime(...));
Is is incredible but I have a function that return (char *) and works in printf();
but fails in other stdio functions, in this case only the last function evaluated is returned to all arguments.
???printf("%s%s%s",a(1),a(2),a(3));
In this case printf() works but other functions print in %s only the value of a(1).
I need return char a[size] to ??printf functions, but strange (for me) GCC method said my that I have no way.
Thanks again.
More information about the Gcc-help
mailing list