Use getline(3) -instead of fgets(3)- which reads a dynamically allocated line.
typedef struct msgclient {
ssize_t msglen;
int msgtype;
char *cp;
}M1;
then in your main
function
M1 *m;
m=malloc(sizeof(M1));
if (!m) { perror("malloc"); exit(EXIT_FAILURE); };
m->msglen=0;
m->msgtype=6;
m->cp = NULL;
printf("Enter\t");
fflush(stdout);
size_t msgsize = 0;
m->msglen = getline(&msg->cp, &msgsize, stdin);
you might consider adding the allocated size of the buffer (i.e. msgsize
) as an additional field of struct msgclient
addenda:
Notice that you might perhaps consider using GNU readline. It offers edition and completion facilities (when reading from a terminal).