2
typedef struct {
    char manufacturer[SIZE];
    char model[SIZE];
    int size;
    int id;
    int qty;
    double cost;
    double price;
} tv;    

void firstSix(tv *tvarr[]);
void firstSix(tv *tvarr[])
{
    (*tvarr[0]).manufacturer = "Vizio";
}

I am making an inventory program. It consists of an array of structs that will store information about different televisions. In my program I am required to hardcode six entries into the array, so I am trying to make a function that will take a struct array pointer argument. In the above code, I included the struct declaration, the function prototype and function definition that I am trying to make. Everything is placed before and after main in the respective order. I don't understand why Visual Studio is highlighting the first parenthesis in the code inside the function definition and saying "expression must be a modifiable lvalue". I don't understand what it is that I am doing wrong. Please help.

1

2 Answers 2

2

You cannot assign an array like that. You need to do

strcpy ((*tvarr[0]).manufacturer, "Vizio");

Make sure that you don't go out of bounds when copying the string into the array.

You can either check the size of the string in advance or use strncpy which will limit the maximum number of characters to be copied.

An array is not a modifiable l-value. So basically you cannot have it on the left hand side of an assignment.

Or may be you also might want to define manufacture as char *manufacture and then dynamically allocate the string.

manufacturer = strdup ("Vizio"); //manufacturer is char *

Or depending on the length first allocate the buffer

manufacturer = malloc (sizeof (char) * needed_bytes);

Whenever you dynamically allocate the buffer, whenever you have finished working with it always remember to free it free (manufacturer).

Sign up to request clarification or add additional context in comments.

Comments

2

I think you want to do something like

strncpy((tvarr[0])->manufacturer, "Vizio", SIZE - 1);

Kevin has it; you can't assign a string to a pointer, you must copy the data to the array. I suggest strncpy to keep from running off the end of the allocated space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.