473,399 Members | 3,106 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,399 software developers and data experts.

Invalid lvalue in assignment while trying to swap pointers

while executing this code i was getting a error Invalid lvalue in assignment. Can any one tell how to correct this.

Expand|Select|Wrap|Line Numbers
  1. dataItem* d=(dataItam*)malloc(10*sizeof(dataItem));
  2. dataItem* temp;
  3. temp=(d+6);
  4. (d+6)=(d+8);//error line
  5. (d+8)=temp;//error line
Please some on help on this
Apr 1 '13 #1
3 2431
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. dataItem* d=(dataItam*)malloc(10*sizeof(dataItem));
you have a typing error?
and you should use CODE-tags when posting code....
Apr 1 '13 #2
donbock
2,426 Expert 2GB
The C Standard uses the term lvalue to refer to something that can be on the left side of the assignment operator (=). You want to pay special attention to what's on the left side of the equals sign when the error message refers to "invalid lvalue". Therefore, I am immediately suspicious of (d+6) and (d+8) on lines 4 and 5.

d is a pointer. The term pointer arithmetic is used for exprssions that add a pointer and an integer. (d+6) is equivalent to &d[6]. Notice that the expression evaluates to the address of an array element. By the way, one of the biggest differences between pointer arithmetic and array notion is that you are allowed to subtract integers from a pointer but you are not allowed to have a negative array index.

Indeed, the expressions on the left side of the equals sign in lines 4 and 5 are not valid lvalues. There are two ways to fix this: dereference the pointer arithmetic expressions (*(d+6)) or use array notation.

However, fixing the invalid lvalue errors will provoke additional errors. You want to swap the contents of two array elements, not their addresses.
Apr 1 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. temp=(d+6);
  2.   (d+6)=(d+8);//error line
  3.  (d+8)=temp;//error line
  4.  
The problem here is d+6 is an expression which is either true or false. An expression cannot be an LVAL. You need an actual variable on the left of the assignment operator. d[6] would be that variable.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. temp=(d+6);
  2.   d[6]= *(d+8);  //assign actual variable
  3.   d[8] = *temp;  //dereference temp to get the dataItem
  4.                  //then assign the dataItem t d[8]
  5.  
Apr 1 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a...
4
by: bob | last post by:
I have little C experience and am concurrently trying to tackle C and LKM's (a little too ambitious maybe) anyway here is the problem I'm having with an example module I found. static int...
1
by: bob | last post by:
let's say you want a function to swap pointers. is this how you take two pointers by reference: void swap(char* &p1, char* &p2);
5
by: A. Farber | last post by:
Hello, I call readv() and writev() in several spots of a program which I run under Linux, OpenBSD and Cygwin. Since it always the same way (check the return value; then check errno and retry if...
1
by: Java Guy | last post by:
I'm trying to view a web page. IE tells me there are (Java?) errors on the page. Here they are: Line: 15 Char: 7 Error: Wrong number of arguments or invalid propert assignment Code: 0 URL:...
3
by: Suyash Upadhyay | last post by:
Hello all, I was writing a code regarding offset of structure elements. struct a { struct b { int i; float f; char ch; }x;
6
by: Paul Edwards | last post by:
The following code: int main(void) { char *x; (void **)x += 1; return (0); }
4
by: mdh | last post by:
May I ask why this works: given: char s; char *posbfr = s; char *endbfr = s + MAXOP; void(...){ if (posbfr >= endbfr) printf("......");
1
by: yyhkitty | last post by:
Hi, I keep getting two invalid lvalue in assignment errors. here's what my code looks like: pthread_t msg_receive(char *sbuffer, int *maxlen) { // get the values for sbuffer and maxlen ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.