-1

Why don't the lines people[i].name = "something" work?

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

typedef struct {
    char name[100];
    char number[100];
}
person;

int main(void)
{
    person people[2];
    people[0].name = "Brian";
    people[0].number = "+1-617-495-1000";
    people[1].name = "David";
    people[1].number = "+1-949-468-2750";
}

My IDE says I should use strcpy but I'm very confused as to why.

2
  • 2
    The confusion might be that you can initialize an array char name[100] = "Brian"; in the declaration. But that is a special rule, and despite the use of =, this is not an assignment. Commented May 14, 2023 at 10:44
  • The = operator is not defined for arrays of any type. You can assign to individual array elements, but not whole arrays at once. Arrays lose their "array-ness" under most circumstances. There are legitimate reasons for this behavior, but they won't fit in the space of a comment. Commented May 14, 2023 at 11:58

1 Answer 1

0

Arrays are non-modifiable lvalues, i.e. you can't assign to them. You can always assign to individual array elements (assuming they are not const). But you cannot assign something to the whole array.

The compiler is correct in suggesting strcpy().

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.