LValue problem 
Author Message
 LValue problem

I get an error during compilation that simply states "LValue Required".
So I looked it up, and found out that an lvalue is something like:
a+b=c and an rvalue is a=b+c.  Which is lovely, but it didn't really
help me solve my problem...Which is:  How do I assign a string to my
variable Weapon?  I'm a little limited in memory size.  I have a 64k
code segment to play with and that's all...So if there is a way to do it
without using anymore variables I'd appreciate it if you shared it with
me...Assuming you have time.

Sorry about those C++ comments...  :-)
:-)
Thanks,
Corvus.

Here is some code that ...ummm...almost works.  :-)

typedef unsigned char Byte;
typedef unsigned int Word;

struct UserRec {
  char Name[21];           //User Name
  char sex[7];             //Male / Female / It
  char Weapon[21];         //Name No Longer than 20 Chars
  char Armour[21];         //Name No Longer than 20 Chars
  char Stat[6];            //Dead Or Alive
  Byte Level, VD, Mel;     //0..255
  Byte DrugCount;          //0 Or 1, Depending If Used Today Or Not
  Byte LoseRaise;          //Fight Level Master And Lose?  0/1
  Byte Champ;              //Complete Game? 0 = No / 1 = Yes
  Byte InnSleep;           //Got A Room?  0 = No / 1 = Yes
  int Align;               //Good / Evil / 0 = Neutral
  Word Lays;               //The Amount Of Times You Had Sex With A
{*filter*}
  Word Fights, Chall;      //Hill Fights & Player Challenges
  Word Strength, Defence, Mana;     //Just What It Says
  long Kills, GHand, GBank, CurrHits;      //Gold in Hand/Bank etc.
  long CurrExp, ExpNeeded, MaxHits;        //see prev. comment

Quote:
} User;        //End Of User Structure.

int main(void)
{
  User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!
  return 0;
Quote:
}

--



Sun, 25 May 2003 08:44:01 GMT  
 LValue problem

Quote:

> I get an error during compilation that simply states "LValue Required".
> int main(void)
> {
>   User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!
>   return 0;
> }

You can't assign to an array like that; you have to use strcpy() instead:

strcpy(User.Weapon, "Bare Hands");

--
Eric Amick
Columbia, MD

--



Sun, 25 May 2003 03:00:00 GMT  
 LValue problem

Quote:

> I get an error during compilation that simply states "LValue Required".
> So I looked it up, and found out that an lvalue is something like:
> a+b=c and an rvalue is a=b+c.  Which is lovely, but it didn't really
> help me solve my problem...Which is:  How do I assign a string to my
> variable Weapon?  I'm a little limited in memory size.  I have a 64k
> code segment to play with and that's all...So if there is a way to do it
> without using anymore variables I'd appreciate it if you shared it with
> me...Assuming you have time.

> Sorry about those C++ comments...  :-)
> :-)
> Thanks,
> Corvus.

> Here is some code that ...ummm...almost works.  :-)

> typedef unsigned char Byte;
> typedef unsigned int Word;

> struct UserRec {
>   char Name[21];           //User Name
>   char sex[7];             //Male / Female / It
>   char Weapon[21];         //Name No Longer than 20 Chars
>   char Armour[21];         //Name No Longer than 20 Chars
>   char Stat[6];            //Dead Or Alive
>   Byte Level, VD, Mel;     //0..255
>   Byte DrugCount;          //0 Or 1, Depending If Used Today Or Not
>   Byte LoseRaise;          //Fight Level Master And Lose?  0/1
>   Byte Champ;              //Complete Game? 0 = No / 1 = Yes
>   Byte InnSleep;           //Got A Room?  0 = No / 1 = Yes
>   int Align;               //Good / Evil / 0 = Neutral
>   Word Lays;               //The Amount Of Times You Had Sex With A
> {*filter*}
>   Word Fights, Chall;      //Hill Fights & Player Challenges
>   Word Strength, Defence, Mana;     //Just What It Says
>   long Kills, GHand, GBank, CurrHits;      //Gold in Hand/Bank etc.
>   long CurrExp, ExpNeeded, MaxHits;        //see prev. comment
> } User;        //End Of User Structure.

> int main(void)
> {
>   User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!
>   return 0;
> }
> --


The problem is that User.Weapon is not an l-value. Loosely speaking, an
l-value is something that can appear on the left hand side of an equals
sign. User.Weapon is an array. An individual array element is an l-value
so you can assign a single character like: ``User.Weapon[0] = 'B';'' or
you should use strcpy() or strncpy() to populate a character array:
    strncpy(User.Weapon, "Bare Hands", sizeof(User.Weapon))
    strcpy(User.Weapon, "Bare Hands");
``strncpy'' has the distinct advantage of *never* overruning the
boundary
that you give it; ``strcpy'' will just copy until it find the
terminating
NUL character.

Enjoy!

Dave Shawley
--
--



Sun, 25 May 2003 03:00:00 GMT  
 LValue problem

Quote:

> I get an error during compilation that simply states "LValue Required".
> So I looked it up, and found out that an lvalue is something like:
> a+b=c and an rvalue is a=b+c.  Which is lovely, but it didn't really
> help me solve my problem...Which is:  How do I assign a string to my
> variable Weapon?  I'm a little limited in memory size.  I have a 64k
> code segment to play with and that's all...So if there is a way to do it
> without using anymore variables I'd appreciate it if you shared it with
> me...Assuming you have time.

> Sorry about those C++ comments...  :-)
> :-)
> Thanks,
> Corvus.

> Here is some code that ...ummm...almost works.  :-)

> typedef unsigned char Byte;
> typedef unsigned int Word;

> struct UserRec {
>   char Name[21];           //User Name
>   char sex[7];             //Male / Female / It
>   char Weapon[21];         //Name No Longer than 20 Chars
>   char Armour[21];         //Name No Longer than 20 Chars
>   char Stat[6];            //Dead Or Alive
>   Byte Level, VD, Mel;     //0..255
>   Byte DrugCount;          //0 Or 1, Depending If Used Today Or Not
>   Byte LoseRaise;          //Fight Level Master And Lose?  0/1
>   Byte Champ;              //Complete Game? 0 = No / 1 = Yes
>   Byte InnSleep;           //Got A Room?  0 = No / 1 = Yes
>   int Align;               //Good / Evil / 0 = Neutral
>   Word Lays;               //The Amount Of Times You Had Sex With A
> {*filter*}
>   Word Fights, Chall;      //Hill Fights & Player Challenges
>   Word Strength, Defence, Mana;     //Just What It Says
>   long Kills, GHand, GBank, CurrHits;      //Gold in Hand/Bank etc.
>   long CurrExp, ExpNeeded, MaxHits;        //see prev. comment
> } User;        //End Of User Structure.

> int main(void)
> {
>   User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!
>   return 0;
> }
> --


You can't just go assigning strings like that.  You need to use a
library function like strcpy [strcpy(User.Weapon, "Bare Hands"); or
strncpy(User.Weapon, "Bare Hands", 21);
--



Sun, 25 May 2003 03:00:00 GMT  
 LValue problem

Quote:

> I get an error during compilation that simply states "LValue Required".
> So I looked it up, and found out that an lvalue is something like:
> a+b=c and an rvalue is a=b+c.  

Wherever you looked it up and found *that* as an explanation: throw it
away. The explanation is rubbish.

The rule-of-thumb explanation of an 'lvalue' is "something that you
can assign another value (an rvalue) to", i.e.  a 'left value for
assignment operators'. In particular, a+b is not an lvalue, but a is.

Quote:
> Which is lovely, but it didn't really
> help me solve my problem...Which is:  How do I assign a string to my
> variable Weapon?  

In a nutshell: you don't assign strings at all, in C, you strcpy()
them.  The apparent exception is initialization of variables, where
you can use a string constant to initialize a char array. This looks a
bit like an assignment operation, but it isn't one.

        char mystring[] = "content of my string";

is legal C.

        char mystring[];
        mystring = "content of my string";

is a syntax error.

Quote:
> I'm a little limited in memory size.  I have a 64k
> code segment to play with and that's all...So if there is a way to do it
> without using anymore variables I'd appreciate it if you shared it with
> me...Assuming you have time.

You have to initialize the whole structure in one go:

Quote:
> struct UserRec {
>   char Name[21];           //User Name
>   char sex[7];             //Male / Female / It
[...]
}         //End Of User Structure.

struct UserRec User = {
        "User Name of some kind",
        "Male",
        "Bare Hands",
        /* ... */
Quote:
};

--

Even if all the snow were burnt, ashes would remain.
--



Sun, 25 May 2003 03:00:00 GMT  
 LValue problem

Quote:
> I get an error during compilation that simply states "LValue Required".

[snip]

Quote:
>   char Weapon[21];         //Name No Longer than 20 Chars
 ...
>   User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!

If you're going to set this variable to predetermined strings only
(i.e. you're not going to be doing any Weapon[3] = 'C' things), you should
probably declare it as:
    char *Weapon;
This is because User.Weapon is an array right now and you're trying to
assign to it a pointer to your text string.
By the way, this is why you're getting the error that a Lvalue
(i.e. "User.Weapon" in this case; Rvalue is the pointer to the string
"Bare Hands") is required here -- User.Weapon can't be assigned to.

If you're going to mess with your string, the correct command to use is:
    strncpy(User.Weapon, "Bare Hands", 21);
Or, if you're _absolutely sure_ you'll never try copying something larger
than your array into the string:
    strcpy(User.Weapon, "Bare Hands");

Good luck with your project. :)

-- Steven Andreason
   Help everyone!!!  Just never expect anything back...
--



Sun, 25 May 2003 03:00:00 GMT  
 LValue problem


Quote:

>> struct UserRec {
>>   char Weapon[21];         //Name No Longer than 20 Chars
>> } User;        //End Of User Structure.

>> int main(void)
>> {
>>   User.Weapon = "Bare Hands";      <-- Weapon type -- Error Here!
>>   return 0;
>> }

>you should use strcpy() or strncpy() to populate a character array:
>    strncpy(User.Weapon, "Bare Hands", sizeof(User.Weapon))
>    strcpy(User.Weapon, "Bare Hands");
>``strncpy'' has the distinct advantage of *never* overruning the
>boundary
>that you give it; ``strcpy'' will just copy until it find the
>terminating
>NUL character.

   Note, also, that strncpy will not just copy until it finds the NUL
character.  This runs the danger of not terminating the string.  In this
example, that isn't a problem, but the point of using strncpy was to
handle the case where the destination might not be able to hold the
entire source string, and this invocation actually misbehaves in this
case.  If sizeof(User.Weapon) were 10, you would successfully copy in all
characters of the string "Bare Hands", but the resulting string would not
be NUL terminated, making it awkward to handle in subsequent operations.
You want to do something like:

strncpy(User.Weapon, "Bare Hands", sizeof(User.Weapon)-1);

and I assume here that you have, at some time in the past, initialized
User.Weapon[sizeof(User.Weapon)-1] to zero.

--

 Home page:  http://caliban.physics.utoronto.ca/neufeld/Intro.html
 "Don't edit reality for the sake of simplicity"
--



Mon, 26 May 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. LValue problems

2. a macro that uses ( ? : ) conditional as an lvalue

3. Resulting lvalue of assignment operation

4. expression return value / lvalue

5. (lvalue)

6. lvalue in C

7. lvalue

8. Lvalue required in function main ?

9. lvalues and increment operator

10. ISO C forbids use of cast expressions as lvalues

11. modify const-qual obj via const-qual lvalue?

12. a macro that uses ( ? : ) conditional as an lvalue

 

 
Powered by phpBB® Forum Software