GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2006, 06:33
emanresu emanresu is offline
New Member
 
Join Date: Nov 2006
Location: Essex, UK. (Near London)
Posts: 24
emanresu is on a distinguished road

lvalue compile error


In 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:
struct GLD0DATA
   {
   char GlSt	;       /* 0    */
   char	Filler1 [20];	/* 1    */
   char GlAcCC  [2];    /* 21   */
   char GlAcCX  [1];    /* 23   */
<snip>
   char GlMatDt [6];    /* 442  */
   char GlFill5 [1416]; /* 448  */
   char GlDrac  [7];    /* 1864 */
   char GlCrac	[7];    /* 1871 */
   char GlFill6 [170];  /* 1878  fill out till end  */
   };                   /* 2048 */

struct ICRSEX1A
   {
   char ICAcCC  [2];  
   char ICAcCX  [1];  
   char ICAcMM  [2]; 
   char ICAcno  [6];
   char ICAcSS  [2];	
   char ICCrInt [9];    
   char ICDrInt [9];    
   char ICMatDt [6];    
   };      

char WKAcCC  [2];  
char WKAcCX  [1];  
char WKAcMM  [2]; 
char WKAcno  [6];
char WKAcSS  [2];	
char WKCrInt [9];    
char WKDrInt [9];    
char WKMatDt [6];    

int             IN_BUF_LEN;
int             OUT_BUF_LEN;
struct GLD0DATA REC_BUF,  *pi;    /*  initially I did not use pointers   */
struct ICRSEX1A OUT_BUF,  *po;   /*  with pointers still get same error */   
char            IN_POS_BLK[128];
char            OUT_POS_BLK[128];
char            IN_KEY_BUF[2];
char            OUT_KEY_BUF[2];
char            FIL_NAME[60]= "";
char            OUT_NAME[60]="";

main ()
{
   pi = &REC_BUF;
   po = &OUT_BUF;


First I tried directly from structure to structure, then put in intermediary work variables
(as shown)

CPP / C++ / C Code:
	    if (strncmp(str1,"     ",5) != 0)
	    {
		WKAcCC  =  pi->GlAcCC;
		WKAcCX  =  pi->GlAcCX;
		WKAcMM  =  pi->GlAcMM;
		WKAcno  =  pi->GlAcno;
		WKAcSS  =  pi->GlAcSS;
		WKCrInt =  pi->GlCrInt;
		WKDrInt =  pi->GlDrInt;
		WKMatDt =  pi->GlMatDt;


		po->ICAcCC  =  WKAcCC;
		po->ICAcCX  =  WKAcCX;
		po->ICAcMM  =  WKAcMM;
		po->ICAcno  =  WKAcno;
		po->ICAcSS  =  WKAcSS;
		po->ICCrInt =  WKCrInt;
		po->ICDrInt =  WKDrInt;
		po->ICMatDt =  WKMatDt;

but still get same error
"error C2106: '=' : left operand must be lvalue"

Any help appreciated!

Thanks,
Bob
  #2  
Old 16-Nov-2006, 06:57
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 131
mathematician will become famous soon enough

Re: lvalue compile error


The 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  
Old 16-Nov-2006, 07:05
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 131
mathematician will become famous soon enough

Re: lvalue compile error


To underline the point, because they are pointers WKAcCC[1] = (pi->GIAcCC)[1]; is equivalent to *( WKAcCC + 1 ) = *( Pi->GIAcCC + 1 );
  #4  
Old 16-Nov-2006, 07:45
emanresu emanresu is offline
New Member
 
Join Date: Nov 2006
Location: Essex, UK. (Near London)
Posts: 24
emanresu is on a distinguished road

Re: lvalue compile error


Many 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  
Old 16-Nov-2006, 09:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: lvalue compile error


Quote:
Originally Posted by emanresu
In MS C (ver6), I am trying to copy data between two structures, i.e. from record read in to record to be written out.

You can copy structs with an assignment statement:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>


struct St {int x;
           char y[100];
};

int main ()
{
    struct St a, b;

    a.x = 1234;
    strcpy(a.y, "Hello"); 
    printf("After first strcpy:\n");
    printf("   a.x = %d, a.y = %s\n\n", a.x, a.y);

    b = a;
    printf("After assignment:\n");
    printf("   b.x = %d, b.y = %s\n\n", b.x, b.y);

    strcpy(a.y, "Goodbye");
    printf("After second strcpy:\n");
    printf("   a.x = %d, a.y = %s\n\n", a.x, a.y);
    printf("   b.x = %d, b.y = %s\n\n", b.x, b.y);


    return 0;
}


Output:
Code:
After first strcpy: a.x = 1234, a.y = Hello After assignment: b.x = 1234, b.y = Hello After second strcpy: a.x = 1234, a.y = Goodbye b.x = 1234, b.y = Hello

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  
Old 16-Nov-2006, 09:53
emanresu emanresu is offline
New Member
 
Join Date: Nov 2006
Location: Essex, UK. (Near London)
Posts: 24
emanresu is on a distinguished road

Re: lvalue compile error


More new info, I always learn something on this site.

Would assignment only work if the structures are identical?
  #7  
Old 16-Nov-2006, 10:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: lvalue compile error


Quote:
Originally Posted by emanresu
I always learn something on this site
Hey! That's my line. (But I released it as "uncopylefted free" text, so no royalty is required). http://www.gnu.org/copyleft/

Quote:
Originally Posted by emanresu
Would assignment only work if the structures are identical?
Yes.

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:
struct St {int x;
           char y[100];
};

struct Ss {int x;
           char y[100];
};

.
.
.

    struct St a;
    struct Ss c;
.
.
.
    c = a;


Regards,

Dave
  #8  
Old 16-Nov-2006, 11:22
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 131
mathematician will become famous soon enough

Re: lvalue compile error


I 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 GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 14:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.