Re: Rvalue of struct type



On Mar 10, 2:51 pm, "Harald van Dijk" <true...@xxxxxxxxx> wrote:
SRR wrote:
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.
Yes! You are right. But I did it inadvertantly while pasting the code.
Actually I typed it as
struct test{
char a[100];
}funTest( void );
Thanks for your comment abouy it.






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.)
Thanks, I know the concept of Sequence Point. A function Call is a
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.


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 -

Let us extend the topic. Is this the only case, when an rvalue of type
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 -


.



Relevant Pages

  • Re: about string and character
    ... Looking forward for amusing explanations of how a sequence of one ... I don't take it because it converts a pointer to a char for some ... Do they represent a string or part of one; four individual char; 2 ... It has nothing to do with the original question though, ...
    (comp.lang.c)
  • Re: why is this assignment correct ?
    ... This object is actually an array of char initialized with a sequence of characters terminated by a 0. ... Like any array, its address is the same value and type that the address of the first element of this array, making the assignement to a pointer to char possible. ...
    (comp.lang.c)
  • read from console / command prompt help
    ... is it possible to read string(a sequence of char) which is input from ... console to a dynamic array(such pointer) unlimitly until press "enter" in ...
    (comp.lang.cpp)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)