Re: Rvalue of struct type
- From: "SRR" <SRRajesh1989@xxxxxxxxx>
- Date: 10 Mar 2007 03:38:33 -0800
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@xxxxxxxxx> wrote:
SRR wrote:Yes! You are right. But I did it inadvertantly while pasting the code.
Consider the following code:
#include <stdio.h>
#include <string.h>
struct test{
char a[100];
}
funTest( void );
While valid, this is really bad style. It looks like you forgot a
semicolon and expect funTest to return int, when you really do mean to
make funTest return struct test.
Actually I typed it as
struct test{
char a[100];
}funTest( void );
Thanks for your comment abouy it.
Thanks, I know the concept of Sequence Point. A function Call is a
int main( void )
{
printf("%s",funTest().a);
return 0;
}
struct test funTest()
{
struct test foo;
strcpy(foo.a,"Hello");
return foo;
}
In this I want to know how the following expression statement works:
printf("%s",funTest().a);
funTest() is evaluated to yield an rvalue of type, "struct test" and
the expression "funTest().a" yields an rvalue of type array of char,
which is converted to an rvalue equal to the pointer to the first
element of the char array and is passed to the printf() function.
Correct.
My doubt is, since "funTest().a" yields an rvalue of type, array of
char, how can it be converted to pointer type when we can have pointer
only for Lvalues and not for Rvalues.
The pointer points to a temporary, and accessing that temporary after
the next sequence point, or modifying it, results in undefined
behaviour. (If you don't know what a sequence point is, please ask.)
sequence point as all the arguments are evaluated including all side
effects before entering the function.
Therefore my code produces undefined behavior as the temporary storage
may no longer contain the required data!
Now I understood why I got run time error when I compiled and executed
the code using Dev-CPP for Windows OS, but it executed well with
TurboCPP for MS-Dos.
Thanks once again for helping me understand what is really happening.
Let us extend the topic. Is this the only case, when an rvalue of type
char c = funTest().a[0]; /* valid, initialises c with 'H' */
char *p = funTest().a; /* valid, initialised p with a pointer value */
c = *p; /* invalid, accessing the function call's result after the
next sequence point */
funTest().a[0] = 'H'; /* invalid, modifying function call's result */
char str[100];
strcpy(str, funTest().a); /* invalid, accessing the function call's
result after the next sequence point */- Hide quoted text -
array is converted to rvalue of type pointer to the first element of
the array using temporary storage?
Any other possibility?
Thanks in advance for the reply.
- Show quoted text -
.
- Follow-Ups:
- Re: Rvalue of struct type
- From: Harald van Dijk
- Re: Rvalue of struct type
- References:
- Rvalue of struct type
- From: SRR
- Re: Rvalue of struct type
- From: Harald van Dijk
- Rvalue of struct type
- Prev by Date: using strtok
- Next by Date: Re: longjmp, goto, and VLAs
- Previous by thread: Re: Rvalue of struct type
- Next by thread: Re: Rvalue of struct type
- Index(es):
Relevant Pages
|