![]() |
|
#1
|
|||
|
|||
lvalue compile errorIn MS C (ver6), I am trying to copy data between two structures, i.e. from record read in to record to be written out.
I have them declared like this CPP / C++ / C Code:
First I tried directly from structure to structure, then put in intermediary work variables (as shown) CPP / C++ / C Code:
but still get same error "error C2106: '=' : left operand must be lvalue" Any help appreciated! Thanks, Bob |
|
#2
|
|||
|
|||
Re: lvalue compile errorThe same mistake recurs throughout the program, so I will take one line as a "for example".
WKAcCC = pi->GlAcCC; WKAcCC and pi->GIcCC are both declared as arrays. Which in C means that the variables WKAcCC and pi->GIAcCC, without any subscript, are pointers to those arrays. As it stands, if the above line was compiled, WKAcCC would end up pointing to the same array (same area in the computer's memory) as pi->GIAcCC. There are two ways you can move data from one to the other. Firstly (since there are only two elements) you can either do: WKAcCC[0] = (pi->GIAcCC)[0]; WKAcCC[1] = (pi->GIAcCC)[1]; or else: memcpy( WKAcCC, pi->GIAcCC, 2 ); Either copies data from the area of memory pointed to by pi->GIAcCC to the area of memory pointed to by WKAcCC. |
|
#3
|
|||
|
|||
Re: lvalue compile errorTo underline the point, because they are pointers WKAcCC[1] = (pi->GIAcCC)[1]; is equivalent to *( WKAcCC + 1 ) = *( Pi->GIAcCC + 1 );
|
|
#4
|
|||
|
|||
Re: lvalue compile errorMany thanks for that, and for taking the trouble to explain it.
Using memcpy as per your example, I now get a clean compile and link. Cheers, Bob |
|
#5
|
|||
|
|||
Re: lvalue compile errorQuote:
You can copy structs with an assignment statement: CPP / C++ / C Code:
Output: Code:
The entire struct is copied. You can't copy arrays with a single assignment, but you can copy structs. This has been part of the C language specification "forever", and has carried into the current standards for C and C++ languages. (And it works with compilers from Microsoft, Borland, and GNU to which I have access --- including Visual C++ version 6.) Note that the entire struct is copied (all of its members). Note particularly, that if a struct has a pointer as one of its members, the value of the pointer is copied, not whatever it is that the pointer is pointing to. The example that I showed uses an array, not a pointer, as a member (as does your program, I think). Regards, Dave |
|
#6
|
|||
|
|||
Re: lvalue compile errorMore new info, I always learn something on this site.
Would assignment only work if the structures are identical? |
|
#7
|
|||
|
|||
Re: lvalue compile errorQuote:
Quote:
But don't take my word for it: Try it with structs declared differently but with identical members. Try it for other kinds of structs. Etc. CPP / C++ / C Code:
Regards, Dave |
|
#8
|
|||
|
|||
Re: lvalue compile errorI will add a postscript of my own. Since memcpy is a generic function it has no idea of the size of the objects it is being asked to copy; therefore it copies bytes. If, for example, you wanted to copy 5 ints from one location to another, in order to ensure that the right number of bytes got copied you would need:
memcpy( intp1, intp2, sizeof( int ) * 5 ); |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 16:41 |
| Winsock error when compiling FLTK 2.0 Projects | mauriciorossi | FLTK Forum | 3 | 16-Aug-2005 11:18 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 21:30 |
| What is "Ambigious symbol" ??*( a compilation error) | small_ticket | C++ Forum | 2 | 07-Jan-2005 22:10 |
| Can enum have same name as class? | crystalattice | C++ Forum | 3 | 08-Dec-2004 17:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The