473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a method as an lvalue and "invalid lvalue in assignment"comp ilation error

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 example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)= iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
or like:
ip_hdr(newskb)= (struct iphdr*)iphdr;
and got the same error.
Any ideas?
Regards,
Mark
Sep 8 '08 #1
11 2491
On 8 Sep, 19:25, "markr...@gmail .com" <markr...@gmail .comwrote:
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 example I
can
* * * * get this by newskb->iph = iphdr (this also appears in acommented
* * * * line in the example below) ; but I want to achieve the same where
* * * * the left side is : ip_hdr(newskb). Alas, if I try this , I get
* * * * a compilation error about line 25.
* * * * line 25 is:
* * * * * ip_hdr(newskb)= iphdr;
* * * * the error I get is:
* * * * lval.c:25: error: invalid lvalue in assignment
* * * * I use gcc-4.1.2-33c, and I compile without any flag.

* * * * I tried casting,
* * * * like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
* * * * or like:
* * * * ip_hdr(newskb)= (struct iphdr*)iphdr;
* * * * and got the same error.
* * * * Any ideas?
* * * * Regards,
* * * * Mark
Could you explain what you're actually trying to do here?

If ip_hdr is a function that takes one value in and returns one value
out, then ip_hdr(newskb) will run the function and give a result. For
some reason, you seem to want a different result. Do you want the
ip_hdr function to run, or not? What are you going to do with (a) the
result it actually gives, and (b) the result that you appear to want
it to give?

If you want a different result to come out of the function, you
probably need to alter the function and get it to return the desired
result. Eg by putting "return iphdr;" into it. Alternatively, do
something like:

if (someflag)
{ result = iphdr; }
else
{ result = ip_hdr(newskb); }

Hope that helps.
Paul.
Sep 8 '08 #2
"ma******@gmail .com" <ma******@gmail .comwrites:
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 example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)= iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
or like:
ip_hdr(newskb)= (struct iphdr*)iphdr;
and got the same error.
Any ideas?
C doesn't have "methods". If you really want to ask about methods,
you probably want comp.lang.c++ (<OT>the C++ standard refers to them
as member functions, but it's common to refer to them as methods
anyway</OT>).

The result of a function call is not an lvalue. For example, an
attempt to set the square root of 2.0 to 1.5:

sqrt(2.0) = 1.5; /* illegal */

cannot succeed.

If a function returns a pointer value, dereferencing that value does
yield an lvalue. For example:

#include <stdio.h>

int obj = 0;

int *func(void)
{
return &obj;
}

int main(void)
{
*(func()) = 42;
printf("obj = %d\n", obj);
return 0;
}

(The extra parentheses are for clarity; they're not strictly
necessary.)

You'll have to exercise the usual care about the lifetime of the
object to which the result points; for example, returning a pointer to
a local object is bad.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 8 '08 #3
ma******@gmail. com wrote:
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.
It is possible in C++ to use the value returned by a function call as
the left operand of an assignment operator; this is not possible in
C.

So, are you programming in C or in C++? Your use of the term "method"
makes me think you're using C++ - neither language actually uses the
term "method", but it is in common use to describe what C++ call
member functions; something that doesn't exist in C.

C and and C++ are two significantly different languages. If your
question is actually about C++, then this is the wrong newsgroup. You
should go to comp.lang.c++. There will be a much larger number of
people there who are competent to answer your question.
... I know, of course, that in this example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)= iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
or like:
ip_hdr(newskb)= (struct iphdr*)iphdr;
and got the same error.
Any ideas?
Yes - I've got the idea that you have no concept of the kind of
information that is needed to answer your question. You should provide
the following information:

What is the definition of ip_hdr()? How is it related to iphdr? What
is newskb? And what in the world are you actually trying to do?

If you are writing in C++, the existence of a "struct iphdr"
declaration somewhere in your code makes "iphdr" a type name. In all
three statements you've provided, the final use of 'iphdr' is in a
context where a type name is not allowed. This makes it very unclear
what it is that you're actually trying to do. If you're writing in C,
iphdr is not necessarily a type name - you're allowed to define a
variable with the same name, but it's not a good idea to actually do
so.

I would strongly recommend creating a complete program, as small as
possible, that actually demonstrates what it is that you're trying to
do. Compile it, and then post the COMPLETE TEXT of the program, and
the COMPLETE TEXT of the compiler's error messages. Please make sure
that you post it to the appropriate newsgroup.
Sep 8 '08 #4
On Mon, 08 Sep 2008 11:25:37 -0700, ma******@gmail. com wrote:
ip_hdr(newskb)= iphdr;
short answer: I guess you mean:

*(ip_hdr(newskb ))= iphdr;

but it could be anything because you didn't include the code.
Also, please don't use tabs. This is really ugly:
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 example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line
in the example below) ; but I want to achieve the same where the
left
side is : ip_hdr(newskb). Alas, if I try this , I get a
Sep 8 '08 #5
Hello,
First, thanks a lot for you (and the others who replied to
my question).
Second, I simply forgot to insert the little program I already wrote
into my post. Sorry about it. Here it is:
// lval.c
#include <stdlib.h>

struct iphdr
{
int i;
};
struct sk_buff
{
struct iphdr *iph;
};

struct iphdr* ip_hdr(struct sk_buff *skb)
{
return skb->iph;
};

int main()
{
struct iphdr* iphdr = malloc(sizeof (struct iphdr));
struct sk_buff *newskb = malloc(sizeof (struct sk_buff));
ip_hdr(newskb)= iphdr;
}
compiling this program gives:
lval.c: In function ‘main’:
lval.c:26: error: lvalue required as left operand of assignment

where line 26 is:
ip_hdr(newskb)= iphdr;

And I am talking (solely) about "C", not C++.

Regards,
Mark

On Sep 8, 10:24*pm, jameskuy...@ver izon.net wrote:
markr...@gmail. com wrote:
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.

It is possible in C++ to use the value returned by a function call as
the left operand of an assignment operator; this is not possible in
C.

So, are you programming in C or in C++? Your use of the term "method"
makes me think you're using C++ - neither language actually uses the
term "method", but it is in common use to describe what C++ call
member functions; something that doesn't exist in C.

C and and C++ *are two significantly different languages. If your
question is actually about C++, then this is the wrong newsgroup. You
should go to comp.lang.c++. There will be a much larger number of
people there who are competent to answer your question.
... I know, of course, that in this example I
can
* *get this by newskb->iph = iphdr (this also appears in a commented
* *line in the example below) ; but I want to achieve the same where
* *the left side is : ip_hdr(newskb). Alas, if I try this , I get
* *a compilation error about line 25.
* *line 25 is:
* * *ip_hdr(newskb) =iphdr;
* *the error I get is:
* *lval.c:25: error: invalid lvalue in assignment
* *I use gcc-4.1.2-33c, and I compile without any flag.
* *I tried casting,
* *like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
* *or like:
* *ip_hdr(newskb) =(struct iphdr*)iphdr;
* *and got the same error.
* *Any ideas?

Yes - I've got the idea that you have no concept of the kind of
information that is needed to answer your question. You should provide
the following information:

What is the definition of ip_hdr()? How is it related to iphdr? What
is newskb? And what in the world are you actually trying to do?

If you are writing in C++, the existence of a "struct iphdr"
declaration somewhere in your code makes "iphdr" a type name. In all
three statements you've provided, the final use of 'iphdr' is in a
context where a type name is not allowed. This makes it very unclear
what it is that you're actually trying to do. If you're writing in C,
iphdr is not necessarily a type name - you're allowed to define a
variable with the same name, but it's not a good idea *to actually do
so.

I would strongly recommend creating a complete program, as small as
possible, that actually demonstrates what it is that you're trying to
do. Compile it, and then post the COMPLETE TEXT of the program, and
the COMPLETE TEXT of the compiler's error messages. Please make sure
that you post it to the appropriate newsgroup.
Sep 9 '08 #6
Hello,
Would it be correct to do it thus:
*(ip_hdr(newskb ))=*iphdr;

this compiles OK.

Regards,
Mark



On Sep 9, 8:41*am, "markr...@gmail .com" <markr...@gmail .comwrote:
Hello,
First, thanks a lot for you (and the others who replied to
my question).
Second, I simply forgot to insert the little program I already wrote
into my post. Sorry about it. Here it is:
// lval.c
#include <stdlib.h>

struct iphdr
{
* * * * int i;

};

struct sk_buff
{
* struct iphdr *iph;

};

struct iphdr* ip_hdr(struct sk_buff *skb)
{
* * * * return skb->iph;

};

int main()
{
struct iphdr* iphdr * *= malloc(sizeof (struct iphdr));
struct sk_buff *newskb = malloc(sizeof (struct sk_buff));
ip_hdr(newskb)= iphdr;

}

compiling this program gives:
lval.c: In function ‘main’:
lval.c:26: error: lvalue required as left operand of assignment

where line 26 is:
ip_hdr(newskb)= iphdr;

And I am talking (solely) about "C", not C++.

Regards,
Mark

On Sep 8, 10:24*pm, jameskuy...@ver izon.net wrote:
markr...@gmail. com wrote:
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.
It is possible in C++ to use the value returned by a function call as
the left operand of an assignment operator; this is not possible in
C.
So, are you programming in C or in C++? Your use of the term "method"
makes me think you're using C++ - neither language actually uses the
term "method", but it is in common use to describe what C++ call
member functions; something that doesn't exist in C.
C and and C++ *are two significantly different languages. If your
question is actually about C++, then this is the wrong newsgroup. You
should go to comp.lang.c++. There will be a much larger number of
people there who are competent to answer your question.
... I know, of course, that in this example I
can
* *get this by newskb->iph = iphdr (this also appears in a commented
* *line in the example below) ; but I want to achieve the same where
* *the left side is : ip_hdr(newskb). Alas, if I try this , I get
* *a compilation error about line 25.
* *line 25 is:
* * *ip_hdr(newskb) =iphdr;
* *the error I get is:
* *lval.c:25: error: invalid lvalue in assignment
* *I use gcc-4.1.2-33c, and I compile without any flag.
* *I tried casting,
* *like : (struct iphdr*)ip_hdr(n ewskb)=iphdr;
* *or like:
* *ip_hdr(newskb) =(struct iphdr*)iphdr;
* *and got the same error.
* *Any ideas?
Yes - I've got the idea that you have no concept of the kind of
information that is needed to answer your question. You should provide
the following information:
What is the definition of ip_hdr()? How is it related to iphdr? What
is newskb? And what in the world are you actually trying to do?
If you are writing in C++, the existence of a "struct iphdr"
declaration somewhere in your code makes "iphdr" a type name. In all
three statements you've provided, the final use of 'iphdr' is in a
context where a type name is not allowed. This makes it very unclear
what it is that you're actually trying to do. If you're writing in C,
iphdr is not necessarily a type name - you're allowed to define a
variable with the same name, but it's not a good idea *to actually do
so.
I would strongly recommend creating a complete program, as small as
possible, that actually demonstrates what it is that you're trying to
do. Compile it, and then post the COMPLETE TEXT of the program, and
the COMPLETE TEXT of the compiler's error messages. Please make sure
that you post it to the appropriate newsgroup.

Sep 9 '08 #7
ma******@gmail. com wrote:
Hello,
First, thanks a lot for you (and the others who replied to
my question).
Second, I simply forgot to insert the little program I already wrote
into my post. Sorry about it. Here it is:
Look over the following rewrite and see whether it does what you want.
There may be further gotchas in it to be fixed.

#include <stdlib.h>
#include <stdio.h>

struct iphdr
{
int i;
};
struct sk_buff
{
struct iphdr *iph;
};
/* note changes */
struct iphdr **ip_hdr(struct sk_buff *skb)
{
return &(skb->iph);
}

/* removed stray ';' */

int main(void)
{
struct iphdr *iphdr = malloc(sizeof *iphdr);
struct sk_buff *newskb = malloc(sizeof *newskb);
if (!iphdr || !newskb) {
fprintf(stderr, "At least one allocation attempt failed,\n"
"Giving up.\n");
free(iphdr);
free(newskb);
exit(EXIT_FAILU RE);
}
*ip_hdr(newskb) = iphdr; /* note change */
free(iphdr);
free(newskb);
return 0;
}

Sep 9 '08 #8
"ma******@gmail .com" wrote:
>
First, thanks a lot for you (and the others who replied to my
question). Second, I simply forgot to insert the little program
I already wrote into my post. Sorry about it. Here it is:
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/ (taming google)
<http://members.fortune city.com/nnqweb/ (newusers)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 9 '08 #9
ma******@gmail. com wrote:
Hello,
Would it be correct to do it thus:
*(ip_hdr(newskb ))=*iphdr;
No. That has several problems. See below.

....
On Sep 9, 8:41 am, "markr...@gmail .com" <markr...@gmail .comwrote:
>Hello,
First, thanks a lot for you (and the others who replied to
my question).
Second, I simply forgot to insert the little program I already wrote
into my post. Sorry about it. Here it is:
// lval.c
#include <stdlib.h>

struct iphdr
{
int i;

};

struct sk_buff
{
struct iphdr *iph;

};

struct iphdr* ip_hdr(struct sk_buff *skb)
{
return skb->iph;

};

int main()
{
struct iphdr* iphdr = malloc(sizeof (struct iphdr));
struct sk_buff *newskb = malloc(sizeof (struct sk_buff));
Note that at this point, newskb points at an uninitialized sk_buff. This
means that newskb->iph is an uninitialized pointer. As a result, the
behavior of ip_hdr(newsk) is undefined.

I presume that you want your code to have the same effect as if you had
written

newskb->iph = iphdr;

Well, the result of a function call is not an lvalue in C, and therefore
can never be the left operand of an assignment operator. However,
*ip_hdr(newskb) is an lvalue; the only problem is that it's an lvalue of
type iphdr; and at this point no object of type iphdr has yet been
associated with newskb. What is associated with newskb is an object of
type "struct iphdr*", so you need to make sure that *ip_hdr(newskb) has
that type. this means that ip_hdr needs to return a pointer to a pointer:

struct iphdr** ip_hdr(struct sk_buff *skb)
{
return &skb->iph;
}

With that definition, you can now write

*ip_hdr(newskb) = iphdr;
Sep 9 '08 #10

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

Similar topics

0
4597
by: Brian Morris | last post by:
I'm new to .NET and just trying a few things out, like emailing. I created a form in Visual Studio .Net to input some information for generating an email and I'm getting the following error when it executes both on my server or on the server of the .NET service provider. I'm obviously missing something very basic and I'm hoping one of you can...
7
17867
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new...
0
1876
by: Nicola George | last post by:
Hi all, I hope someone can help me as I'm going a bit metal with this problem. I have a project in ASP.NET, within this project I have Crystal Report called Catalogue. On an asp page I have a report viewer control (vwrReportViewer) and a report document control (reportDocument1). On the load of the page I have the following code. ...
2
3189
by: Vinod I | last post by:
Hi Team, When I tryed following code, I am getting the Runtime Error as "The View State is invalid for this page and might be corrupted." Exception Details: System.Web.HttpException: The View State is invalid for this page and might be corrupted. Stack Trace: System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +150...
0
1939
by: dbuchanan | last post by:
Hello, Why am I getting this error? I don't know how to isolate it. It occurs intermittently. Has anyone seen this before? Here is the full text \\ System.IO.FileLoadException was unhandled
5
7209
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem is. Last time the problem was that "using namespace std" apparently had a "link" class/object defined already. Now I get: error: no matching...
13
1539
by: Jim in Arizona | last post by:
I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also did this conditional formatting for the column next to it. Here's my code. in the aspx file =============================== <asp:GridView ID="gvData" runat="server" OnRowDataBound="doColor">...
1
8012
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: http://(address.of.my.webcam):port/LiveView.html and
5
14305
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that consistently gets an "invalid operand" IOError when write() is called on the pipe. So I switched to subprocess. It works fine when executed...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8216
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...
0
6614
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...
1
5710
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...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.