0

I want to assign a struct value (that can be max 50 characters) from standard input but am getting the error:

Incompatible types when assigning to type 'char[50]' from type 'char *'

#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN 50

struct msgbuf {
 char mtext[MAX_LEN];
};


int main (int argc, char *argv)
{
    struct msgbuf m;
    char in[MAX_LEN];

    scanf ("%s", in);
    m.mtext = in;

}
Jebathon
  • 4,310
  • 14
  • 57
  • 108

1 Answers1

1

Arrays have no the copy assignment operator. You have to copy arrays element by element. You can use standard function strcpy declared in header <string.h> that to copy strings. For example

#include <string.h>

//...

strcpy( m.mtext, in );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335