8

I defined my struct as:

struct taxPayer{
  char name[25];
  long int socialSecNum;
  float taxRate;
  float income;
  float taxes; 
};

My main function contains:

taxPayer citizen1, citizen2;

citizen1.name = "Tim McGuiness";
citizen1.socialSecNum = 255871234;
citizen1.taxRate = 0.35;

citizen2.name = "John Kane";
citizen2.socialSecNum = 278990582;
citizen2.taxRate = 0.29;

The compiled gives me an error (C3863 array type char[25] is not assignable, expression must be a modifiable lvalue) on citizen1.name = "Tim McGuiness"; as well as on citzen2.name = "John Kane";

How do I remove this error and set citizen1.name to a name and citizen2.name to a different name?

Jens
  • 8,423
  • 9
  • 58
  • 78
Zack Sloan
  • 133
  • 1
  • 1
  • 8
  • 2
    Just change `char name[25]` to `const char*`. Or use `std::string` – DeiDei May 04 '16 at 01:22
  • Perfect, switched `char name[25]` to `char *name` and works exactly like it should. – Zack Sloan May 04 '16 at 01:29
  • @ZackSloan Switching to `char* name` doesn't work as you think. You need to allocate memory for it, otherwise it may seem to work, but you bump into undefined behaviour and the code will most likely crash. Stick to `std::string` if possible, and all these nightmares will be over. – vsoftco May 04 '16 at 01:33
  • Ahh, I didn't say switch to `char*`, I said `const char*`. Big difference. This will only make sense if the `name` is known at compile time. If you want to enter the `name` at runtime, then use `std::string`. – DeiDei May 04 '16 at 01:35
  • Or, if you really want, you can use `char*` as the type of `taxPayer::name`, in combination with [`strdup`](http://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c), which takes care of the memory allocation for you: `citizen1.name = strdup("Tim McGuiness");`. But again, stick to C++ if you write C++. – vsoftco May 04 '16 at 01:45

3 Answers3

6

You cannot assign to an array. What you can do is either use a std::string or use std::strcpy/std::strncpy, like

std::strncpy(citizen1.name,"Tim McGuiness", sizeof(taxPayer::name));

Since you use C++, I'd recommend using a std::string,

struct taxPayer
{
    std::string name;
    // the rest
};

then you can simply assign to it as you did in your code

citizen1.name = "Tim McGuiness";
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Make sure that the [`strncpy`](http://linux.die.net/man/3/strcpy) receives the maximum allowable length of the copy, i.e. the length of the `name[25]` array. Otherwise, a name longer than 25 characters (e.g. `"William Longhorn Emil Wastekovich"`) will create problems. – Jens May 04 '16 at 01:22
  • @Jens Sure, that's why I think OP should go with `std::string` (if using C++ and not C), otherwise of course `strncpy` with max length is the way to go for safe code. – vsoftco May 04 '16 at 01:25
  • If that is an option, then ok. If it is not and the OP has to use an array then extra attention is required. – Jens May 04 '16 at 01:26
  • Modified to `strncpy` just so OP avoids funky buffer overflows ;) – vsoftco May 04 '16 at 01:28
3

In c, an array is assignable only in the initialization period, citizen1.name is an array of char type. To solve your problem, you may use this:

strcpy(citizen1.name, "Tim McGuiness");

or:

memcpy(citizen1.name, "Tim McGuiness", strlen("Tim McGuiness"));
citizen1.name[strlen("Tim McGuiness") + 1] = '\0';
weixt
  • 41
  • 1
0

I have done a bit of research, maybe it's a bit late but it may help someone over here.

#include <iostream>
#include <string.h>
using namespace std;

int main() {
int i, j;

char cuvinte[5][10] = { "a", "b", "c",
                        "da", "e" };
char temp[10];
int lungime = sizeof(cuvinte) / sizeof(cuvinte[0]);

for (i = 0; i < lungime - 1; i++) {
    for (j = 0; j < lungime - 1; j++) {
        if (strcmp(cuvinte[j], cuvinte[j+1]) < 0) {
            strcpy_s(temp, cuvinte[j]);
            strcpy_s(cuvinte[j],cuvinte[j+1]);
            strcpy_s(cuvinte[j+1],temp);
        }
    }
}

for (i = 0; i < lungime;i++) {
    cout << cuvinte[i] << " ";
}


system("pause");
}