473,378 Members | 1,469 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,378 software developers and data experts.

lvalue required as increment operand

Hello,

I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution here?
I'm unable to find a way :(

Thanks.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Dec 24 '07 #1
9 8193
"Roman Mashak" <mr*@tusur.ruwrites:
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}
extern inline void copy(void *pSource_, void *pDest_, unsigned int dLength)
{
char *pSource = pSource_;
char *pDest = pDest_;
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {
*pDest = *pSource;
pDest++;
pSource++; /* error */
}
}

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Dec 24 '07 #2
Roman Mashak wrote:
Hello,

I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution here?
I'm unable to find a way :(
Ben Pfaff has shown how to fix the code, but I'll offer
another solution:

#include <string.h>
extern inline void copy(void *pSource, void *pDest,
unsigned int dLength)
{
memcpy (pDest, pSource, dLength); /* note arg swap */
}

If you're using a free-standing rather than a hosted
implementation, it's possible that <string.hand memcpy()
are not provided, but take a look anyhow: If they're present,
they'll likely be speedier than anything written in plain C.
Also, memcpy() will behave unpredictably if the source and
destination areas overlap; you could use memmove() to get
predictable behavior, but that behavior might not be precisely
what you get from the original. You need to study the "contract"
of the copy() function to see if it's a candidate for replacement.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 24 '07 #3
Hello, Eric!
You wrote on Mon, 24 Dec 2007 00:17:21 -0500:

[skip]
ES Ben Pfaff has shown how to fix the code, but I'll offer
ESanother solution:

ES #include <string.h>
ES extern inline void copy(void *pSource, void *pDest,
ES unsigned int dLength)
ES {
ES memcpy (pDest, pSource, dLength); /* note arg swap */
ES }
[skip]

Thanks a lot!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Dec 24 '07 #4
"Roman Mashak" <mr*@tusur.ruwrites:
I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution here?
[...]

Another possibility is to change the two "++"s to:

pDest = (char*)pDest + 1;
pSource = (char*)pSource + 1;

The void* values are explicitly converted to char* so you can
increment them, then the char* result is implicitly converted back to
void* by the assignment.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 24 '07 #5
Eric Sosman wrote, On 24/12/07 05:17:
Roman Mashak wrote:
>Hello,

I'm porting code, previously compiled by IAR compiler, for ARM-gcc
compiler. This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution
here? I'm unable to find a way :(

Ben Pfaff has shown how to fix the code, but I'll offer
another solution:

#include <string.h>
extern inline void copy(void *pSource, void *pDest,
unsigned int dLength)
{
memcpy (pDest, pSource, dLength); /* note arg swap */
}
Unless you explicitly need the function version I would use a macro instead.
If you're using a free-standing rather than a hosted
implementation, it's possible that <string.hand memcpy()
are not provided, but take a look anyhow: If they're present,
At least some free-standing implementations provide memcpy (and as much
of the rest of the standard library as they sensibly can).
they'll likely be speedier than anything written in plain C.
Also, memcpy() will behave unpredictably if the source and
destination areas overlap; you could use memmove() to get
predictable behavior, but that behavior might not be precisely
what you get from the original. You need to study the "contract"
of the copy() function to see if it's a candidate for replacement.
Indeed. Personally I would want to change the code that calls copy to
call memmove or memcpy as appropriate. Then there is one less function
for the next maintainer to learn.
--
Flash Gordon
Dec 24 '07 #6
On Dec 24, 11:24*pm, "Roman Mashak" <m...@tusur.ruwrote:
Hello,

I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest * Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
* * unsigned int dI;

* * for (dI = 0; dI < dLength; dI++) {

* * * * *((char *) pDest) = *((char *) pSource);
* * * * ((char *) pDest)++; * */* error */
* * * * ((char *) pSource)++; * */* error */
* * }

}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution here?
I'm unable to find a way :(

Thanks.

With best regards, Roman Mashak. *E-mail: m...@tusur.ru
void* mcpy(void *dst, void *src, size_t n)
{
size_t i;
char* ldst = (char*)dst;
char* lsrc = (char*)src;
for (i = 0; i < n; ++i)
{
*ldst++ = *lsrc++;
}
return dst;
}
Dec 24 '07 #7
Roman Mashak wrote:
>
I'm porting code, previously compiled by IAR compiler, for ARM-gcc
compiler. This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength) {
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {
*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any
other operations can't be applied to rvalue. So, what is the
proper solution here? I'm unable to find a way :(
/* copies a structure in a buffer. */
extern inline void copy(void *psor, void *pdst, size_t lgh) {
char *cs = psor, cd = pdst;

while (lgh--) *cd++ = *cs++;
} /* untested */

Notice the absence of casts, which are usually errors. Also note
the change in the type of the lgh parameter. The above should drop
in wherever you were calling the old version. Any reasonable
compiler will absorb the extra data items.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

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

Dec 25 '07 #8
Roman Mashak wrote:
Hello,

I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
{
unsigned int dI;

for (dI = 0; dI < dLength; dI++) {

*((char *) pDest) = *((char *) pSource);
((char *) pDest)++; /* error */
((char *) pSource)++; /* error */
}
}

As I understand, casting ends up with 'rvalue', and '++' or any other
operations can't be applied to rvalue. So, what is the proper solution here?
I'm unable to find a way :(

Thanks.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru

Too complicated. First, if copy() would use pSourse and pDest as char*
it should declare them such. :

void copy(char *pSource, char *pDest, unsigned dLength)

Even if you call it with void* the compiler will do the conversions to
char* for you.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 25 '07 #9
Joe Wright <jo********@comcast.netwrites:
Roman Mashak wrote:
>I'm porting code, previously compiled by IAR compiler, for ARM-gcc
compiler. This snippet results in error, posted in subject:

/*
* copies a structure in a buffer
* pSource Data to copy
* pDest Buffer in which to copy the data
*/
extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
[...]
Too complicated. First, if copy() would use pSourse and pDest as char*
it should declare them such. :

void copy(char *pSource, char *pDest, unsigned dLength)

Even if you call it with void* the compiler will do the conversions to
char* for you.
I disagree. This kind of thing is exactly what void* is for (see the
standard memcpy() and memmove() functions, for example). If the
parameters are declared as char* (why not unsigned char*?), the
function can't be used with, say, int* arguments without a cast. Any
required explicit conversions should be done inside the function, not
imposed on the user.

Of course, just using memcpy() or memmove() directly is probably
better than re-implementing it.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 25 '07 #10

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

Similar topics

9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
5
by: Sai Krishna M | last post by:
Lvalue Required......Why? Hello all, the following code is not compiling in Turbo C and the error is "Lvalue Required" . main() {
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...
9
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the...
14
by: Akhil | last post by:
plz c d following code #include<stdio.h> void main() { int x=4,y=1; y=x++++; //gives error message lvalue required y=x++ + ++y;//no errors
3
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that...
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...
54
by: Rahul | last post by:
Hi Everyone, I have the following piece of code, and i expected an error, however i don't get an error, int main() { int aa=0,b=0; 1>0?aa:b = 10; printf("value %d %d\n",aa,b);
11
by: markryde | last post by:
Hello, Followed here is a simplified code example of something which I try to implement; in essence , I want to assign a value to a return value of a method is C. I know, of course, that in this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.