Why do not work?, is the same.
Ingo Krabbe
ikrabbe.ask@web.de
Wed Sep 14 04:57:00 GMT 2005
Am Mittwoch, 14. September 2005 00:41 schrieb Fco. J:
> > 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.
There is a difference between char* and char [n] (array with n bytes), though
both display as a char*. If you have two variables
char* x; /* and*/
char y[10];
and you return from a function
struct f_ret { char s[10]; };
struct f_ret f() { struct f_ret r = { "0123456789" }; return (char*)r; }
then you will get two different results:
x = f().s; /* this is an error ! */
and
y= f().s;
both seem to be the same on the first view, but you will see that this even
gives a compiler error.
I have another example code below which says:
before x=(nil), y=0xbfff6d70
after x=0xbfff6d24, y=0xbfff6d70
Where x gets set to a pointer value of the returned array (as the argument of
printf would).
You see that the (char*) y doens't change its location but only the content
has been copied from f().y to y.
BYE INGO
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t.c
Type: text/x-csrc
Size: 369 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20050914/2457c70e/attachment-0001.bin>
More information about the Gcc-help
mailing list