MV94
MV94

Reputation: 75

Expression must be a modifiable lvalue (pointer to struct)

I've looked at similar questions but haven't really found an answer to my problem.

In my program, I have a function, sortdata, as follows:

void sortdata(Person *arr[], int noElements)
{
    /* temporary pointer to Person data type to aid with swapping */
    Person *tempptr = (Person *)malloc(sizeof(Person));

    int i = 0;

    /* loop I am working on to sort my data */
    if (arr[i]->zip > arr[i + 1]->zip) 
    {
        /* stores value in index i for array inside of temporary pointer */
        tempptr->name = arr[i]->name;
    }
}

I'm receiving the error described in the question at this line:

  tempptr->name = arr[i]->name;

temp is not recognized as a modifiable lvalue. Why is this? I have this code in another function within my program:

while ((gets(teststring)) != NULL && i < 50)
{
    /* dynamically allocates memory for each index of the array */
    arr[i] = (Person*)malloc(sizeof(Person));

    /* takes in data from user/ input file */

    strcpy(arr[i]->name, teststring);
    gets(arr[i]->address);
    gets(arr[i]->citystate);
    gets(arr[i]->zip);
    printf("\n");
}

I haven't previously initialized arr[] (arr[] is an array of pointers to a structure passed from elsewhere in the program).

How do I make it so that I can store the values in arr[i] within tempptr?

Here is my structure definition in case it is needed:

/* structure defintion with typedef */
typedef struct person{
    char name[50];
    char address[50];
    char citystate[30];
    char zip[10];
}Person;

This program is for a class assignment so while I appreciate any efforts to help, I am only concerned with how to be able to store values in tempptr so I can perform swaps. Thank you.

Upvotes: 0

Views: 4343

Answers (2)

Tim3880
Tim3880

Reputation: 2583

You need use strcpy to modify char[].

Upvotes: 0

JS1
JS1

Reputation: 4767

You need to use:

strcpy(tempptr->name, arr[i]->name);

You can't assign a char[50] array, you have to copy to it instead.

Upvotes: 3

Related Questions