Mustafa Mujahid
Mustafa Mujahid

Reputation: 65

error : expression must be a modifiable lvalue

I'm getting an error:

expression must be a modifiable lvalue at line, obj.name = ptr->name

I have tried to make obj an array type object like so

 for(int j=0 ;j<1;j++)
{
obj[j].id = ptr->id;
obj[j].balance= ptr->balance;
obj[j].name = ptr->name;   //still getting error here.
obj[j].nic = ptr->nic;
}
return(obj);
}

but that has not worked either.

if i comment the error out and pass just three remaining values it should work but i receive garbage values after the first out put.

here is the original code:

#include<iostream>
using namespace std;

struct bank
{
  int id, nic;
  float balance;
  char name[20];


};
bank search(bank* );

void main()

{

    bank data[2],mobj;
    for(int i=0;i<2;i++)
    {
    cout<<"enter name: ";
    cin>>data[i].name;
    cout<<"enter id: ";
    cin>>data[i].id;
    cout<<"enter balance : ";
    cin>>data[i].balance;
    cout<<"enter nic : ";
    cin>>data[i].nic;

    }


    mobj=search(data);

    cout <<"balance of customer no. "<<mobj.balance<<endl;
    cout<<"id is" <<mobj.id<<endl;
    cout<< "nic is"<<mobj.nic<<endl;



    system("pause");
}




bank search(bank *ptr)
{   
    int id;
    cout<<"enter value you want to serch"<<endl;
    cin>>id;
    bank obj;
for(int i=0 ; i<2 ;i++)
{
    if(ptr->id == id)
    {
        break;
    }
    ptr++;
}

obj.id = ptr->id;
obj.balance= ptr->balance;
obj.name = ptr->name;    //error in this line(obj must be modifiable value)
obj.nic = ptr->nic;

return(obj);
}

please help as you see fit!

Upvotes: 4

Views: 26312

Answers (3)

CashCow
CashCow

Reputation: 31435

Your options are:

  • Have name as a std::string. Then it will work
  • use strcpy( obj.name, ptr->name );

If your code is C++ then the first has even more advantages that when you read it in with cin it will automatically ensure that the buffer is big enough for what the user enters.

Try entering a name of 20 or more characters now and see what happens. You will have undefined behaviour and your program may crash.

Those are the fixes anyway.

The reason your code does not work is that an array is not assignable. It is not an l-value. It is the start of a fixed array of bytes. I know it is confusing because you can use = to initialise it thus you can do:

char name[20] = "Frank Smith"; // legal. This is initialisation

but you can't do:

char name[20];
name = "Frank Smith"; // error. This is attempted assignment.

With std::string it works as string is a class in the standard library. (Actually the class is basic_string< char, char_traits<char>, allocator<char> > but assume it is a class for now). And it has methods that know how to assign to it, stream into it, etc. and manage the memory properly.

The only time the array in your struct is more useful is if you want to store it to disk or send it over a connection where you need "raw" data, or where you want to interface it with "C" or a scripting language that works with C structs.

Upvotes: 2

lkanab
lkanab

Reputation: 954

obj[j].name is an array. You can't copy an array like that.

Upvotes: 0

Ferenc Deak
Ferenc Deak

Reputation: 35408

obj.name is an array of char. You cannot do assignments on arrays. So if you want to stick to arrays:

  1. see c++ array assignment of multiple values
  2. use strcpy(obj.name, ptr->name);

but I'd recommend to convert to std::string ... it's much easier to work with than arrays and it seems to me you plan to use obj.name as string. So got with the proper strings.

Upvotes: 3

Related Questions