0

Where

char member[50];

Inside

Struct structure_variable

Why am I unable to initialise structure_variable.member='Dev'; in C?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • Related: [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/q/3683602/1275169) – P.P Feb 17 '22 at 10:18
  • 1
    1. That's not intialisation. Initialisation occurs at the point you define the (struct) variable. So you can initialise with a string but you can't assign a string. Why? That's how the language is defined. Use `strcpy` in that latter case. 2. `'Dev'` should be `"Dev"` if you want it to be a string. – kaylum Feb 17 '22 at 10:20
  • 1
    Please post the actual code, using code formatting. – Lundin Feb 17 '22 at 10:28

1 Answers1

2

What you need is an assignment, not an initialization.

The correct way for assigning a null-terminated char array (a string) is using strcpy():

strcpy(structure_variable.member, "Dev");

meaning that you copy all the characters of the string "Dev" (included the string terminator '\0' at the end) in the memory location starting from the char pointer member.

Note: I assumed that the use of 'Dev' in your question is a typo, as strings need to be enclosed by double quotes as you correctly stated in your question.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39