Re: Why do not work?, is the same.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
#include <stdlib.h>
#include <stdio.h>
struct f_ret { char* x; char y[11]; };

struct f_ret f() {
	struct f_ret r = { NULL, "0231456789" };
	r.x = r.y;
	return r;
}

int main( int argc, char** argv )
{
	char* x = NULL;
	char y[11] = "9876543210";

	printf( "before x=%p, y=%p\n", x, y );

	x = f().x;
	y = f().y;

	printf ( "after x=%p, y=%p\n", x, y );
	
	return 0;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux