xdr library

H

heyerdaal

Hi,

I'm getting the error:

error: invalid lvalue in unary `&'

from lines '//*' when compiling:
-----------------------
#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>

int f(void)
{
int x,y,s;
FILE *outfile;
XDR xdrs;
char filename[]="\0";

lba_MakeFilename(filename,"chkpt","xdr",nt);
outfile = fopen(filename, "w");
//Set up an XDR stream
xdrstdio_create(&xdrs, outfile, XDR_DECODE);

for(x=1; x<=(par->nx); x++) {
for(y=1; y<=(par->ny); y++) {
for(s=0; s<nvecs; s++) {
xdr_float(&xdrs, &((float)N[x][y].n_r)); //*
xdr_float(&xdrs, &((float)N[x][y].n_b)); //*
xdr_float(&xdrs, &((float)N[x][y].n_s)); //*
}
for(s=0; s<dim; s++) {
xdr_float(&xdrs, &((float)N[x][y].d)); //*
}
xdr_int(&xdrs, &(N[x][y].wall_state));
}
}
}
xdr_destroy(&xdrs);
fclose(outfile);
return 0;
}
------------------
where in my code I also #include'd header files defining relevant vars
and functions.

I've used this construct before in another code of mine without probs.

Any clues?

Many thanks!
Neil.
 
C

Christian Kandeler

Hi,

I'm getting the error:

error: invalid lvalue in unary `&'

from lines '//*' when compiling:

[ ... ]
xdr_float(&xdrs, &((float)N[x][y].n_r)); //*


The address operator can only be applied to lvalues. A cast expression is
not an lvalue. Therefore you cannot take the address of the expression
(float) N[x][y].n_r.
I've used this construct before in another code of mine without probs.

The compiler was probably not invoked in standard conforming mode.


Christian
 
K

Keith Thompson

Hi,

I'm getting the error:

error: invalid lvalue in unary `&'

from lines '//*' when compiling:

try this, xdr_float(&xdrs, (float *) &(N[x][y].n_r));


That doesn't do the same thing. The original version converts the
value of N[x][y].n_r to type float. I don't know what the type of
N[x][y].n_r is, but if it's an arithmetic type, the cast does an
arithmetic conversion, which should preserve the value (possibly with
some loss of precision). The expression is illegal because the result
of a cast is not an lvalue, and therefore doesn't have an address.
<OT>I think a cast result is an lvalue in C++.</OT>

The modified version takes the address of N[x][y].n_r and converts
that address to type float*. That's a pointer conversion, not a
numeric conversion. If N[x][y].n_r is already of type float, then
both casts are unnecessary. If it's of type double, for example, then
the pointer conversion will result in the bits being interpreted as if
they were of type float. For example, if double is 64 bits and float
is 32 bits, this will cause the first 32 bits of the double object to
be interpreted as a float, which most likely will give you complete
garbage.

If N[x][y].n_r is not of type float, and you need the address of a
float object with the value of N[x][y].n_r, you need to create
a temporary object:

float tmp = N[x][y].n_r;
...
xdr_float(&xdrs, &tmp);

If N[x][y].n_r is of type float, just drop the cast.
 
C

Christian Kandeler

Keith said:
The expression is illegal because the result
of a cast is not an lvalue, and therefore doesn't have an address.
<OT>I think a cast result is an lvalue in C++.</OT>

<OT>No, it's not.</OT>


Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top