devxlogo

Assigning C-strings

Assigning C-strings

Question:
I am just beginning to learn C++ and I’ve stumble on an error that I cannot figure out despite all my efforts. Here is the code where I’m having trouble with. Thank you for any help that you can provide.

 //a sub function to sort a global arrayvoid sortArray(phoneAddress info[]){   //declare variables   char lastName[25] = "";   char swap ='Y';   while (swap ='Y')   {      swap ='N';      for (short x = 0; x < 6; x++)      {         if (stricmp(info[x].nameL, info[x + 1].nameL)==-1)         {            swap ='Y';         lastName = info[x].nameL;// error            info[x].nameL = info[x+1].nameL;// error            info[x+1].nameL = lastName;// error         }   //end if      }   //end for   }   //end while   for (short x = 0; x < 6; x++)        // sub function   displayInfo(info, x);}   // end function

Error Message on all 3 is this: "'=':left operand must be I-value."

Answer:
You cannot assign a C-string using operator =. Instead, use strcpy() for that purpose.

See also  lenso.ai - an AI reverse image search platform
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist