473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Rvalue of struct type

SRR
Consider the following code:
#include <stdio.h>
#include <string.h>
struct test{
char a[100];
}

funTest( void );
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.

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.

Hope my question is clear.

Thanks in advance for the reply.

Mar 10 '07 #1
25 3453
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.
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.)

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 */

Mar 10 '07 #2
SRR
On Mar 10, 2:51Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
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 -

Mar 10 '07 #3
SRR
On Mar 10, 2:51Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
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.


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.)

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 */
Forgot to ask you one more question.
The left operand of assignment operator should be an Lvalue, but in
this case it is an rvalue, but the compiler doesn't complain that
"Lvalue required". So can I conclude that a structure containing an
array type returned by a function is converted from Rvalue to Lvalue!
(Really funny!!)
Or is it Compiler dependent?
Does the standard say anything about the value returned by, functions
returning structure containing array type?
I hope my question is clear.

Thanks in advance for the reply.
>
char str[100];
strcpy(str, funTest().a); /* invalid, accessing the function call's
result after the next sequence point */- Hide quoted text -

- Show quoted text -

Mar 10 '07 #4
On Mar 10, 11:38 am, "SRR" <SRRajesh1...@gmail.comwrote:
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@gmail.comwrote:SRRwrote:
Consider the following code:
#include <stdio.h>
#include <string.h>
struct test{
char a[100];
}
funTest( void );
int main( void )
{
printf("%s",funTest().a);
return 0;
}
struct test funTest()
{
struct test foo;
strcpy(foo.a,"Hello");
return foo;
}
[...]
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 */

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?
A structure that is not a function call's result must be an lvalue, as
far as I know. So yes, I believe this is the only special case.

Mar 10 '07 #5
On Mar 10, 12:03 pm, "SRR" <SRRajesh1...@gmail.comwrote:
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@gmail.comwrote:
funTest().a[0] = 'H'; /* invalid, modifying function call's result */

Forgot to ask you one more question.
The left operand of assignment operator should be an Lvalue, but in
this case it is an rvalue, but the compiler doesn't complain that
"Lvalue required". So can I conclude that a structure containing an
array type returned by a function is converted from Rvalue to Lvalue!
funTest().a[0] is short for *(funTest().a + 0).

funTest().a is an rvalue array, funTest().a + 0 is an rvalue pointer,
and *(funTest().a + 0) is a dereferenced pointer. Any dereferenced
pointer is an lvalue. There is no rvalue-to-lvalue conversion that
takes place.

The difference can matter:

struct S {
char m[1];
};
struct S s;
struct S f(void) {
return s;
}
int main(void) {
char *p;
p = s.m; /* valid */
p = (char *) &s.m; /* valid */
p = f().m; /* valid (but useless) */
p = (char *) &f().m; /* invalid */
}

The last statement is invalid because f().m is not an lvalue, so you
cannot apply to & operator to it. (The compiler I'm trying right now
disagrees with me; I might be wrong on this.)

Mar 10 '07 #6
[regarding a function, in this case named "funTest", that returns
a structure "struct test", containing an array, in this case "a"]
>SRR wrote:
>>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.
In article <11*********************@8g2000cwh.googlegroups.co m>
Harald van Dijk <tr*****@gmail.comwrote:
>Correct.
[...]
>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.)

char c = funTest().a[0]; /* valid, initialises c with 'H' */
It is worth adding that this is new in C99. C89 never really
quite said enough about these things: we can make guesses, but
there is no wording in that standard to pin down the behavior.
In C89, even something like:

char c = funTest().a[0];

might not be valid. (One might hope that it *is* valid, with the
semantics required by C99, but I think it is dangerous to assume
so.) Note that:

struct test val;

val = funTest();

is valid in both C89 and C99, and is "safe way" to handle
structure-valued functions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 10 '07 #7
On Mar 10, 10:02Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
On Mar 10, 12:03 pm, "SRR" <SRRajesh1...@gmail.comwrote:
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@gmail.comwrote:
funTest().a[0] = 'H'; /* invalid, modifying function call's result */
Forgot to ask you one more question.
The left operand of assignment operator should be an Lvalue, but in
this case it is an rvalue, but the compiler doesn't complain that
"Lvalue required". So can I conclude that a structure containing an
array type returned by a function is converted from Rvalue to Lvalue!

funTest().a[0] is short for *(funTest().a + 0).

funTest().a is an rvalue array, funTest().a + 0 is an rvalue pointer,
and *(funTest().a + 0) is a dereferenced pointer. Any dereferenced
pointer is an lvalue. There is no rvalue-to-lvalue conversion that
takes place.

The difference can matter:

struct S {
Â* Â* char m[1];};

struct S s;
struct S f(void) {
Â* Â* return s;}

int main(void) {
Â* Â* char *p;
Â* Â* p = s.m; /* valid */
Â* Â* p = (char *) &s.m; /* valid */
Â* Â* p = f().m; /* valid (but useless) */
Â* Â* p = (char *) &f().m; /* invalid */

}

The last statement is invalid because f().m is not an lvalue, so you
cannot apply to & operator to it. (The compiler I'm trying right now
disagrees with me; I might be wrong on this.)
Thanks, You are right.
My compiler Dev-CPP, gives Lvalue required error for the statement:
p = (char *) &f().m; /* invalid */

And I think it is even more "intelligent" enough to point out that the
statement:
p = f().m; /* valid (but useless) */
is useless and generates an error "invalid use of non-lvalue array"!

Now I modified the Code u gave:
#include <stdio.h>
struct S {
char m[1];
};
struct S s;
struct S f(void) {

return s;

}
int main(void) {
char *p;
printf("%s",(char*)f().m);/*Note Explicit casting to (char*) */

}

I know that I'm invoking UB by the following statement, if executed,:

printf("%s",(char*)f().m);/* Note Explicit casting to (char*) */

But the above statement is not compiling and gives an error:

"cannot convert to a pointer type"

But C standard guarantees that any pointer can be converted to (char*)
and back without causing any error or UB.
So I guess the assertion, "expression f().m is evaluated to yield an
rvalue pointer" is wrong and it remains as an rvalue array type only,
which is further clarified by the following code:
struct S {
char m[1];
};
struct S s;
struct S f(void) {

return s;

}
int main(void) {
char *p;
*(f().m+0) = 'a';/*Compiler complains*/

}

Compiler gives an error corresponding to the line:
*(f().m+0) = 'a';/*Compiler complains*/
The error is
"invalid operands to binary + "
which is possible only when f().m is an array type and not a pointer
type as we might think. So there is no conversion from array type to
pointer type here, *though C standard requires it*!

But, how the expression statement:
f().m[0] = 'a';
gets executed when f().m is not converted to pointer type?

Note that when we give statement like the following:

printf("%s",f().m);/* I am invoking a UB, but leave it! */

The compiler compiles without any error because printf() is a variable
argument function and there is no way to check the type of each
argument, though there is no possibility of passing an array as such
in C!(except in this case, I guess!)

Do reply about my comments and correct me if I'm wrong.

Mar 11 '07 #8
On Mar 10, 10:02Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
On Mar 10, 12:03 pm, "SRR" <SRRajesh1...@gmail.comwrote:
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@gmail.comwrote:
funTest().a[0] = 'H'; /* invalid, modifying function call's result */
Forgot to ask you one more question.
The left operand of assignment operator should be an Lvalue, but in
this case it is an rvalue, but the compiler doesn't complain that
"Lvalue required". So can I conclude that a structure containing an
array type returned by a function is converted from Rvalue to Lvalue!

funTest().a[0] is short for *(funTest().a + 0).

funTest().a is an rvalue array, funTest().a + 0 is an rvalue pointer,
and *(funTest().a + 0) is a dereferenced pointer. Any dereferenced
pointer is an lvalue. There is no rvalue-to-lvalue conversion that
takes place.

The difference can matter:

struct S {
Â* Â* char m[1];};

struct S s;
struct S f(void) {
Â* Â* return s;}

int main(void) {
Â* Â* char *p;
Â* Â* p = s.m; /* valid */
Â* Â* p = (char *) &s.m; /* valid */
Â* Â* p = f().m; /* valid (but useless) */
Â* Â* p = (char *) &f().m; /* invalid */

}

The last statement is invalid because f().m is not an lvalue, so you
cannot apply to & operator to it. (The compiler I'm trying right now
disagrees with me; I might be wrong on this.)

Sorry, I forgot to mention an important thing.
I recently subscribed to this group and I gave my full name
Rajesh S R
for subscription, which is abbreviated as SRR in the above posts!
So dont get confused by the new name in the recent post!!

I know that, what I said just now might be irrelevant, but I dont want
to confuse people unnecessarily!!

Mar 11 '07 #9
Rajesh S R wrote:
On Mar 10, 10:02Â*pm, "Harald van Dijk" <true...@gmail.comwrote:
On Mar 10, 12:03 pm, "SRR" <SRRajesh1...@gmail.comwrote:
On Mar 10, 2:51 pm, "Harald van Dijk" <true...@gmail.comwrote:
funTest().a[0] = 'H'; /* invalid, modifying function call's result */
Forgot to ask you one more question.
The left operand of assignment operator should be an Lvalue, but in
this case it is an rvalue, but the compiler doesn't complain that
"Lvalue required". So can I conclude that a structure containing an
array type returned by a function is converted from Rvalue to Lvalue!
funTest().a[0] is short for *(funTest().a + 0).

funTest().a is an rvalue array, funTest().a + 0 is an rvalue pointer,
and *(funTest().a + 0) is a dereferenced pointer. Any dereferenced
pointer is an lvalue. There is no rvalue-to-lvalue conversion that
takes place.

The difference can matter:

struct S {
Â* Â* char m[1];};

struct S s;
struct S f(void) {
Â* Â* return s;}

int main(void) {
Â* Â* char *p;
Â* Â* p = s.m; /* valid */
Â* Â* p = (char *) &s.m; /* valid */
Â* Â* p = f().m; /* valid (but useless) */
Â* Â* p = (char *) &f().m; /* invalid */

}

The last statement is invalid because f().m is not an lvalue, so you
cannot apply to & operator to it. (The compiler I'm trying right now
disagrees with me; I might be wrong on this.)

Thanks, You are right.
My compiler Dev-CPP, gives Lvalue required error for the statement:
p = (char *) &f().m; /* invalid */

And I think it is even more "intelligent" enough to point out that the
statement:
p = f().m; /* valid (but useless) */
is useless and generates an error "invalid use of non-lvalue array"!
Yes, I now see that too that I'm using GCC. It converts non-lvalue
arrays to pointers only in C99 mode (using the -std=c99 command-line
option). I missed that it's new in C99, but Chris Torek explained it
quite well. This also applies to the rest of your post.

Mar 11 '07 #10
Rajesh S R wrote:
>
.... snip ...
>
Sorry, I forgot to mention an important thing. I recently
subscribed to this group and I gave my full name Rajesh S R
for subscription, which is abbreviated as SRR in the above posts!
So dont get confused by the new name in the recent post!!
There is no such thing as subscribing to this newsgroup. You may
have subscribed to some sort of Google service, which provides a
poor interface to the real Usenet system, which in turn existed
before Google was a gleam in its grandaddies eye.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 11 '07 #11
CBFalconer wrote:
Rajesh S R wrote:
... snip ...
>Sorry, I forgot to mention an important thing. I recently
subscribed to this group and I gave my full name Rajesh S R
for subscription, which is abbreviated as SRR in the above posts!
So dont get confused by the new name in the recent post!!

There is no such thing as subscribing to this newsgroup.
Yes there is. I do it all the time in mozilla ;)
You may
have subscribed to some sort of Google service, which provides a
poor interface to the real Usenet system, which in turn existed
before Google was a gleam in its grandaddies eye.
Why do people use 'google' as 'microsoft', even if google isn't
mentioned? Was it that bad?

Yevgen
Mar 11 '07 #12
On Mar 12, 1:24 am, CBFalconer <cbfalco...@yahoo.comwrote:
>
There is no such thing as subscribing to this newsgroup.
Can you clarify that? Every newsreader I've ever looked at
has had the option to subscribe to newsgroups.

Mar 11 '07 #13
On Mar 10, 11:00 pm, Chris Torek <nos...@torek.netwrote:
[regarding a function, in this case named "funTest", that returns
a structure "struct test", containing an array, in this case "a"]
SRR wrote:
>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.

In article <1173520263.073652.20...@8g2000cwh.googlegroups.co m>
Harald van Dijk <true...@gmail.comwrote:
Correct.
[...]
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.)
char c = funTest().a[0]; /* valid, initialises c with 'H' */

It is worth adding that this is new in C99. C89 never really
quite said enough about these things: we can make guesses, but
there is no wording in that standard to pin down the behavior.
In C89, even something like:

char c = funTest().a[0];

might not be valid. (One might hope that it *is* valid, with the
semantics required by C99, but I think it is dangerous to assume
so.)
*This is what I follow from yor reply:*

So do you mean to say that result of trying to access an rvalue array,
is implementation dependent as the standard does not say anything
about Rvalue array, but describes about array type which implicitly
means an Lvalue array type, I guess.
So the implementation dependency of Rvalue array access, cleary points
the implementation dependency of the following code:

struct S {
char m[1];

};
struct S s;
struct S f(void) {

return s;

}
int main(void) {
char a;
a = *(f().m+0);/*Compiler complains*/
}
I guess any other compiler may or may not complain about it.
But if I modify the main() function to the following:
int main(void) {
char a;
a = f().m[0];/*Does'nt complain*/

}
There's no error generated by my compiler, even when I compile it
using -ansi and -std=C99.
But some other compiler might complain about it.Am I right?

Did I interpret what you said correctly?
Please comment about my statements and clarify my doubts.
Thanks in advance for the reply.
Note that:
struct test val;

val = funTest();

is valid in both C89 and C99, and is "safe way" to handle
structure-valued functions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Mar 12 '07 #14
[regarding array rvalues, which arise only from functions returning
structures that have as one of their members an array]
>On Mar 10, 11:00 pm, Chris Torek <nos...@torek.netwrote:
>>It is worth adding that [well-defined behavior for array rvalues,
provided the usage is well-enough contained,]
>>is new in C99. ...
In article <11**********************@q40g2000cwq.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>*This is what I follow from yor reply:*

So do you mean to say that result of trying to access an rvalue array,
is implementation dependent as the standard does not say anything
about Rvalue array, but describes about array type which implicitly
means an Lvalue array type, I guess.
For C89, pretty much, yes.
>But if I modify the main() function to the following:
int main(void) {
char a;
a = f().m[0];/*Does'nt complain*/

}
There's no error generated by my compiler, even when I compile it
using -ansi and -std=C99.
I assume you mean you tried once with "-ansi", and one other time
with "-std=c99" (since -ansi, in newer versions of gcc, means
"-std=c89"). (You should also add "-pedantic", in both cases, to
get gcc to print diagnostics for GNU-specific extensions.)
>But some other compiler might complain about it.Am I right?
It might or might not complain; the biggest danger is that it might
generate code that fails in "interesting" ways. For instance, the
code might work when tested (i.e., when run with test data), but
fail -- at worst, quietly produce wrong answers -- when put into
actual use on valuable input data.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 13 '07 #15
On Mar 13, 2:23 pm, Chris Torek <nos...@torek.netwrote:
[regarding array rvalues, which arise only from functions returning
structures that have as one of their members an array]
On Mar 10, 11:00 pm, Chris Torek <nos...@torek.netwrote:
>It is worth adding that [well-defined behavior for array rvalues,

provided the usage is well-enough contained,]
>is new in C99. ...

In article <1173699251.621983.133...@q40g2000cwq.googlegroups .com>,
Rajesh S R <SRRajesh1...@gmail.comwrote:
*This is what I follow from yor reply:*
So do you mean to say that result of trying to access an rvalue array,
is implementation dependent as the standard does not say anything
about Rvalue array, but describes about array type which implicitly
means an Lvalue array type, I guess.

For C89, pretty much, yes.
But if I modify the main() function to the following:
int main(void) {
char a;
a = f().m[0];/*Does'nt complain*/
}
There's no error generated by my compiler, even when I compile it
using -ansi and -std=C99.

I assume you mean you tried once with "-ansi", and one other time
with "-std=c99" (since -ansi, in newer versions of gcc, means
"-std=c89"). (You should also add "-pedantic", in both cases, to
get gcc to print diagnostics for GNU-specific extensions.)
But some other compiler might complain about it.Am I right?

It might or might not complain; the biggest danger is that it might
generate code that fails in "interesting" ways. For instance, the
code might work when tested (i.e., when run with test data), but
fail -- at worst, quietly produce wrong answers -- when put into
actual use on valuable input data.
Thanks for your reply.

So do you mean to state that since Ansi standard says nothing abour
rvalue type array, am I invoking undefined behavior, and *not*
implementation-defined behavior?

And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.

Thanks in advance for the reply.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Mar 13 '07 #16
Rajesh S R wrote, On 13/03/07 13:09:

<snip>
And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.
The C standards explicitly state that if the behaviour is not defined by
the standard then it is undefined behaviour exactly the same as if it
explicitly said it was undefined behaviour.

ISO is the international standard body, ANSI is mainly relevant to the
USA and the original publication of the standard in 1989 before ISO
adopted it.
Thanks in advance for the reply.
>--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Please don't quote peoples signatures, i.e. the bit I left in above that
you quoted, unless you are actually commenting on them.
--
Flash Gordon
Mar 13 '07 #17
Rajesh S R wrote:
On Mar 13, 2:23 pm, Chris Torek <nos...@torek.netwrote:
[regarding array rvalues, which arise only from functions returning
structures that have as one of their members an array]
>On Mar 10, 11:00 pm, Chris Torek <nos...@torek.netwrote:
>>It is worth adding that [well-defined behavior for array rvalues,
provided the usage is well-enough contained,]
>>is new in C99. ...
In article <1173699251.621983.133...@q40g2000cwq.googlegroups .com>,
Rajesh S R <SRRajesh1...@gmail.comwrote:
>*This is what I follow from yor reply:*
>So do you mean to say that result of trying to access an rvalue array,
>is implementation dependent as the standard does not say anything
>about Rvalue array, but describes about array type which implicitly
>means an Lvalue array type, I guess.
For C89, pretty much, yes.
>But if I modify the main() function to the following:
>int main(void) {
char a;
a = f().m[0];/*Does'nt complain*/
>}
>There's no error generated by my compiler, even when I compile it
>using -ansi and -std=C99.
I assume you mean you tried once with "-ansi", and one other time
with "-std=c99" (since -ansi, in newer versions of gcc, means
"-std=c89"). (You should also add "-pedantic", in both cases, to
get gcc to print diagnostics for GNU-specific extensions.)
>But some other compiler might complain about it.Am I right?
It might or might not complain; the biggest danger is that it might
generate code that fails in "interesting" ways. For instance, the
code might work when tested (i.e., when run with test data), but
fail -- at worst, quietly produce wrong answers -- when put into
actual use on valuable input data.

Thanks for your reply.

So do you mean to state that since Ansi standard says nothing abour
rvalue type array, am I invoking undefined behavior, and *not*
implementation-defined behavior?
I think it's behaviour that's unspecified by omission. A conforming
implementation *must* document it's choice for behaviour defined in
the Standard as implementation dependant. However that's not required
for undefined and unspecified behaviour.
And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.
If it doesn't say anything about something then that's unspecified by
omission. Otherwise it's either explicitly mentioned as unspecified,
undefined, implementation defined or defined.
Thanks in advance for the reply.
Note also that a lot of Defect Reports and their responses have been
published by the Standards committee. Not all of them are incorporated
in the latest Standard or draft document. They're however available at
WG14's website. Some of these Defect Reports may define or better
elucidate material left unspecified by omission in the actual Standard.

Mar 13 '07 #18
santosh wrote, On 13/03/07 15:07:
Rajesh S R wrote:
<snip>
>And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.

If it doesn't say anything about something then that's unspecified by
omission. Otherwise it's either explicitly mentioned as unspecified,
undefined, implementation defined or defined.
No, anything undefined by omission is UNDEFINED. I.e. it can make
daemons fly out of your nose. Read the definition of Undefined Behaviour
in n1124.pdf, you can find a pointer to it on http://clc-wiki.net/
>Thanks in advance for the reply.

Note also that a lot of Defect Reports and their responses have been
published by the Standards committee. Not all of them are incorporated
in the latest Standard or draft document. They're however available at
WG14's website. Some of these Defect Reports may define or better
elucidate material left unspecified by omission in the actual Standard.
Links to this and other useful stuff are also on http://clc-wiki.net/
under the standardisation section.
--
Flash Gordon
Mar 13 '07 #19
Flash Gordon wrote:
santosh wrote, On 13/03/07 15:07:
Rajesh S R wrote:

<snip>
And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.
If it doesn't say anything about something then that's unspecified by
omission. Otherwise it's either explicitly mentioned as unspecified,
undefined, implementation defined or defined.

No, anything undefined by omission is UNDEFINED. I.e. it can make
daemons fly out of your nose. Read the definition of Undefined Behaviour
in n1124.pdf, you can find a pointer to it on http://clc-wiki.net/
I see your point. Anything unspecified by omission, though not
explicitly mentioned as undefined behaviour, must be taken as such,
since the Standard, by failing to elaborate, imposes no requirement on
the implementation, one way or the other.

Mar 13 '07 #20
santosh wrote:
Flash Gordon wrote:
santosh wrote, On 13/03/07 15:07:
Rajesh S R wrote:
<snip>
>And a generic question will be:-
>If Ansi standard does'nt say anything about something, is the behavior
>implementation defined or undefined or any other behavior?
>If any other behavior, please specify what.
>
If it doesn't say anything about something then that's unspecified by
omission. Otherwise it's either explicitly mentioned as unspecified,
undefined, implementation defined or defined.
No, anything undefined by omission is UNDEFINED. I.e. it can make
daemons fly out of your nose. Read the definition of Undefined Behaviour
in n1124.pdf, you can find a pointer to it on http://clc-wiki.net/

I see your point. Anything unspecified by omission, though not
explicitly mentioned as undefined behaviour, must be taken as such,
since the Standard, by failing to elaborate, imposes no requirement on
the implementation, one way or the other.
Anything not explicitly defined must be taken as undefined for the
simple reason that the standard states so.

4p2:
If a "shall" or "shall not" requirement that appears outside of a
constraint is violated, the behavior is undefined. Undefined behavior
is otherwise indicated in this International Standard by the words
"undefined behavior" or by the omission of any explicit definition of
behavior. There is no difference in emphasis among these three; they
all describe "behavior that is undefined".

Mar 13 '07 #21
Harald van Dijk wrote:
santosh wrote:
Flash Gordon wrote:
santosh wrote, On 13/03/07 15:07:
Rajesh S R wrote:
>
<snip>
>
And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.

If it doesn't say anything about something then that's unspecified by
omission. Otherwise it's either explicitly mentioned as unspecified,
undefined, implementation defined or defined.
>
No, anything undefined by omission is UNDEFINED. I.e. it can make
daemons fly out of your nose. Read the definition of Undefined Behaviour
in n1124.pdf, you can find a pointer to it on http://clc-wiki.net/
I see your point. Anything unspecified by omission, though not
explicitly mentioned as undefined behaviour, must be taken as such,
since the Standard, by failing to elaborate, imposes no requirement on
the implementation, one way or the other.

Anything not explicitly defined must be taken as undefined for the
simple reason that the standard states so.

4p2:
If a "shall" or "shall not" requirement that appears outside of a
constraint is violated, the behavior is undefined. Undefined behavior
is otherwise indicated in this International Standard by the words
"undefined behavior" or by the omission of any explicit definition of
behavior. There is no difference in emphasis among these three; they
all describe "behavior that is undefined".
Thanks for the conformation.

Mar 13 '07 #22
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>I.e. it can make daemons fly out of your nose.
Now, that's just plain unsafe.

-Beej

Mar 14 '07 #23
"Old Wolf" <ol*****@inspire.net.nzwrites:
On Mar 12, 1:24 am, CBFalconer <cbfalco...@yahoo.comwrote:
>>
There is no such thing as subscribing to this newsgroup.

Can you clarify that? Every newsreader I've ever looked at
has had the option to subscribe to newsgroups.
"Subscribing" to a newsgroup is not any kind of interaction between
you and the newsgroup, or even your local server. It merely records
the name of the group somehow in your local client. (If you're using
Google Groups, it's likely to be a bit different, but in that case you
can think of the Google Groups server as analogous to a newsreading
client, sort of.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '07 #24
Keith Thompson wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
>On Mar 12, 1:24 am, CBFalconer <cbfalco...@yahoo.comwrote:
>>There is no such thing as subscribing to this newsgroup.
Can you clarify that? Every newsreader I've ever looked at
has had the option to subscribe to newsgroups.

"Subscribing" to a newsgroup is not any kind of interaction between
you and the newsgroup, or even your local server. It merely records
the name of the group somehow in your local client. (If you're using
Google Groups, it's likely to be a bit different, but in that case you
can think of the Google Groups server as analogous to a newsreading
client, sort of.)
The newsgroup doesn't know about subscription, of course, but
the OP said he "subscribed and gave his full name for subscription...
So dont get confused by the new name in the recent post!!".
OP just wanted to say "SRR" and "Rajesh S R" is the same person,
and got "there is no such thing as ...". What the heck, of course
there is. Who said it should/does affect the newsgroup in any way?

Yevgen
Mar 14 '07 #25
On Mar 13, 7:17 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Rajesh S R wrote, On 13/03/07 13:09:

<snip>
And a generic question will be:-
If Ansi standard does'nt say anything about something, is the behavior
implementation defined or undefined or any other behavior?
If any other behavior, please specify what.

The C standards explicitly state that if the behaviour is not defined by
the standard then it is undefined behaviour exactly the same as if it
explicitly said it was undefined behaviour.

ISO is the international standard body, ANSI is mainly relevant to the
USA and the original publication of the standard in 1989 before ISO
adopted it.
Thanks for the reply for my question about the behavior of things not
stated in standards.

Can You give reply to the following question too?
*Since ISO standard says nothing about
rvalue type array, am I invoking undefined behavior, and *not*
implementation-defined behavior?*

Does the case of accessing an Rvalue array which may or may not be
converted to pointer type as I mentioned earlier in this thread, a UB,
just because C99 does'nt say anything about Rvalue arrays?

But C standard says that a variable of array type is converted to
pointer type.
Isn't it applicable for Rvalue array type, just because nothing is
explicitly said about Rvalue array type?

I know that I'm too repetitious as I ask same question again and again
in different posts, But I want a response in terms of C99 standards
and I'm curious to know why an rvalue array type is converted to
pointer type in the expression
f().m[0]
but it isn't in the expression
f().m + 0

Is'nt it a violation of C99 which states any array type is converted
to pointer type?
Doesn't the interpretation of the word 'array type' include both
Lvalue and Rvalue types?

Please see the previous posts by others and me and do respond.

Sorry, if you think that, I am too repetitious in posing same question
again and again.
But I think I hadn't got the answer to my question.

Thanks in advance for the reply.

Mar 15 '07 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
0
by: mseeger | last post by:
My concrete problem is this (it is a bit special, but there may be other instances of it): I'd like to create a class for vectors (say: Vector), exporting math. methods, say v.foo(...). I'd like...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
1
by: Chris Fairles | last post by:
Take the following snip (pretend A its a std::pair if you wish ;) ) template <class T1,class T2> struct A { A(A const&){cout<<"A(const&) ";} A(A&&){cout<<"A(A&&) ";} A(T1 const&, T2...
1
by: George2 | last post by:
Hello everyone, I do not know how in the following code, rvalue -- return of X(), could result in a lvalue finally and binded to a non-const reference input parameter of function f. Any...
0
by: Frank Bergemann | last post by:
Using gcc-4.3.0 it silenty(!) invokes the Test::operator=(Test const& t) for Test a; Test b = std::move(a); Test c = std::moved(T()); - if i disable the Test::operator=(Test&& t) in source...
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.