473,668 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invalid lvalue in unary `&'

34 New Member
Hello all,
I was writing a code regarding offset of structure elements.
Expand|Select|Wrap|Line Numbers
  1. struct a
  2. {
  3.     struct b
  4.     {
  5.         int i;
  6.         float f;
  7.         char ch;
  8.     }x;
  9.     struct c
  10.     {
  11.         int j;
  12.         float g;
  13.         char ch;
  14.     }y;
  15. };
  16.  
  17.  
  18. main()
  19. {
  20.     int *p;
  21.     struct a z;
  22.     fun(&z.y);
  23.     printf("\n %d %f %c",z.x.f,z.x.i,z.x.ch);
  24.     getch();
  25. }
  26.  
  27. fun(struct c* p)
  28. {
  29.     int offset;
  30.     struct b * address;
  31.     offset=(char *)&((struct c *)(&((struct a *) 0)->y)->j)-(char *)((struct a *)0);    
  32.                 address=(struct b*)((char *)&(p->j)-offset);
  33.     address->i=400;
  34.     address->f=5.34;
  35.     address->ch='c';
  36. }
I am getting error "invalid lvalue in unary `&' " on underlined fragment of code. I am not getting what wrong with it.
Please help me.
Thanx


Suyash.Upadhyay
Mar 25 '07 #1
3 7619
Ganon11
3,652 Recognized Expert Specialist
The compiler probably thinks you are trying to use '&' as a binary operator with the char* cast to the left and the value to the right. Try enclosing the section you want the address of in parentheses and see if this fixes the issue.
Mar 25 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
This line is overly complex

Expand|Select|Wrap|Line Numbers
  1.     offset=(char *)&((struct c *)(&((struct a *) 0)->y)->j)-(char *)((struct a *)0);    
  2.  
The main problem is that you have not taken account of the fact that -> has a higher precedence than casting so

(struct c *)(&((struct a *) 0)->y)->j

is equivalent to

(struct c *)((&((struct a *) 0)->y)->j)

that this actually takes an object of type c and tries to treat it as a pointer you need

((struct c *)(&((struct a *) 0)->y))->j

This takes an object of type c, dereferences it to get a pointer to object of type c, unnecessarily casts it to a pointer to an onject of type c (since it already has this type) and the references it to access a member.

This is the same as accessing an integer variable like this

Expand|Select|Wrap|Line Numbers
  1. int ix;
  2.  
  3. *(&ix) = 5;
  4.  
i.e. quite unnecessary

So get rid of the cast

(&((struct a *) 0)->y)->j

Get rid of the dereference (&) followed by reference (->) and the extra set of parenthises

((struct a *) 0)->y.j


No I know that you are trying to access x given a pointer to y so what you are looking for here is the address of y so that you can calculate the offest to x. y is an addressable object itself, there is no need to access j in it and if fact this only leaves room for later errors as the calculation would go wrong if someone later added a member to y before j. Since you are going to deference this expression you can drop the .j giving

((struct a *) 0)->y

Putting that back into the line gives

Expand|Select|Wrap|Line Numbers
  1.     offset=(char *)&(((struct a *) 0)->y)-(char *)((struct a *)0);    
  2.  
So this is the answer right?


Wrong, this is doesn't address the biggest problem of all which is that since this references the NULL pointer ( ((struct a *) 0)->y ) the code causes undefined behaviour. You have no guarantee it will work and just because it works in 1 place does not mean it will work everywhere.

If you have to do this (it is nasty but sometimes necessary) then all in all you would be better off declaring a variable of type a and using that to perform your calculations on.
Mar 25 '07 #3
Suyash Upadhyay
34 New Member
Thanx Banfa,
I got my fault. And I also understood your advise regarding ((struct a*) 0)
thanx very much
Mar 25 '07 #4

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

Similar topics

10
5032
by: Stub | last post by:
The following code gives error C2105: '++' needs l-value. char buf; buf++ = 'A';//buf++ isn't a lvalue Why buf++ isn't a lvalue? Is this according to C Standard or is there some reason for this? If buf++ isn't a lvalue, then why the following code works?
19
3841
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 pointer to a pointer in the .c: extern struct example_struct **pointer; and somewhere in the code i tried: pointer = &ex_ptr;
4
6205
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 myintArray = { -1, -1 }; static int arr_argc = 0;
9
3157
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. My answer to these questions are & and * respectively. Is/Are there any other operator(s) satisfying these criteria?
10
2924
by: the_init | last post by:
Hi friends, I read about Lvalue in previous posting and Googled it but I'm not understood it completely. There is a small doubt. int a; a=20; // here a is Lvalue But
14
4366
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
16
3398
by: lovecreatesbea... | last post by:
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix Incrementation Operators say that "The result is not an lvalue". Is this correct? I don't see an errata on these points. If below the p++ or ++p is not an lvalue, then *p++ or *++p is a syntax error. When it talks about Unary Operators, why doesn't the secA7.4 collect both the prefix and postfix incrementation operators together? int main(void) {
33
2971
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
2
1764
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 http://groups.google.com/group/comp.lang.c/browse_thread/thread/ce93f8bf3e61aede/cfeed3fda1d0ee46?hl=en&lnk=gst&q=convert+lvalue+to+rvalue#cfeed3fda1d0ee46 "Mathematically speaking, unary `&' and `*' are inverse functions. Unary `&' takes an lvalue of type `T' and produces an rvalue of type `pointer to T'; unary `*' takes an...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8791
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.