Question

I'm developing a chat in C for the college and I have a problem while sending the text message to the server from the client. The client sends me a segmentation fault error 11. I read about this kind of errors, but I can't find in my code what I'm doing wrong.

  char *row;
  receive(s,answer);
  sprintf(command,"NAME %s\0",name);
  send(s,command);
  printf("***** CHAT: %s *****\n\r",answer);
  printf("COMMANDS:\n\r");
  printf("Users list: send \"u\"\n\r");
  printf("Refresh Chat:send \"r\"\n\r");
  printf("Users Name:send \"c\"\n\r");
  printf("Disconnect: send \"esc\"\n\r");
  printf("******************\n\r\n\r");

  int success=0;

  while(1){

      printf("%s: ",name);

      fgets(row,1024,stdin);

      row[strlen(row)-1]='\0';


      sprintf(row,"TEXT %s",row);

      success=send(s,row);

      if(success==-1){

          fprintf(stderr, "Error sending the message\n\r");
          close(s);

       }*/

  }
Was it helpful?

Solution

char *row;

/* ... */

fgets(row,1024,stdin);

You didn't initialize row pointer. You need to allocate memory (for example using malloc) and have row pointing to that object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top