Strncmp:不寻常的分段错误

时间:2014-10-09 15:21:41

标签: c fgets getchar strncmp

我是C的新手,但我已经写了一段时间了。我正在写一个客户端服务器聊天程序。在他们输入用户名后,我需要在会话开始时提示用户提供几个不同的选项。起初我试图使用getchar()函数,但由于某种原因,以下模式的任何语句都不会产生预期的结果:

int x = getchar();
if (x == '2') doSomething();

如果用户输入2,则永远不会进入" doSomething"区域。所以我尝试使用fgets和strncmp代替。但是现在,我一直在strncmp上遇到分段错误。这是代码中最相关的部分,其中一些注释掉了我尝试使用getchar的部分。不可否认,这有点混乱,因为我只是把它作为一个测试。我想可能为字符串分配额外的空间有助于防止seg故障,但当然它没有。

for( ; ; )
{
  printf("\r\n1.List Users \r\n2.Chat \r\n3.Exit \r\n \r\n \r\n");

  char *x = malloc(5);

  fgets(x, 2, stdin);

  if (x[0] != NULL)
    {

      if (strncmp (x[0],"a",1) == 0)
        {
          printf("yay");
        }
    }


/* int x = getchar();
  if(x == 'a') // Compare input to 'q' character
    break;
  fprintf(stdout, "%d\n", x);*/

  /*x = c - '0';

  if (x == 1)
    getUsers(sockfd);

  if ( x == 2 )
    {

      pthread_create(&sndThread, NULL, do_send, (void *) sockfd);
      pthread_create(&rcvThread, NULL, do_recv, (void *) sockfd);

      pthread_join(sndThread, NULL);
      pthread_join(rcvThread, NULL);
    }

  if ( x == 3 )
    {
    close(sockfd);
    exit(0);
    }*/
}

您可以在剩余的注释中看到尝试执行操作的剩余部分,例如使用减法将char转换为int。这来自我在互联网上找到的东西。我还在互联网上听说getchar离开了输入缓冲区。

所以这里是我的客户端的全部代码,所以你可以把它放在上下文中:

int main(int argc, char **argv)
{
  int sockfd, i;

  char *myName = malloc(MSGSIZE);

  char c;

struct sockaddr_in servaddr;

int status;

pthread_t sndThread;
pthread_t rcvThread;

if(argc != 2)
  {
    printf("Error: expected IP address argument");
    exit(1);
}
  if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{

  error("Socket error");
}

 memset(&servaddr, 0, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORTNUM);

if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <=0)
{
  printf("inet_pton error for %s \n", argv[1]);
  exit(3);
}

if(connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
{
  error("Connect error");
}

printf("Type in a username: \r\n");

while ( fgets(myName[i],MSGSIZE,stdin ) == NULL){}


printf(">%s<\n",myName);

send_userName(myName,sockfd);

for( ; ; )
{
  printf("\r\n1.List Users \r\n2.Chat \r\n3.Exit \r\n \r\n \r\n");

  char *x = malloc(5);

  fgets(x, 2, stdin);

  if (x[0] != NULL)
    {

      if (strncmp (x[0],"a",1) == 0)
        {
          printf("yay");
        }
    }


/* int x = getchar();
  if(x == 'a') // Compare input to 'q' character
    break;
  fprintf(stdout, "%d\n", x);*/

  /*x = c - '0';

  if (x == 1)
    getUsers(sockfd);

  if ( x == 2 )
    {

      pthread_create(&sndThread, NULL, do_send, (void *) sockfd);
      pthread_create(&rcvThread, NULL, do_recv, (void *) sockfd);

      pthread_join(sndThread, NULL);
      pthread_join(rcvThread, NULL);
    }

  if ( x == 3 )
    {
    close(sockfd);
    exit(0);
    }*/
   }

}

3 个答案:

答案 0 :(得分:2)

x[0]是一个字符,但xchar*strncmp应该x作为参数,而不是x[0]。也就是说,你不想要

strncmp(x[0],"a",1)

而是

strncmp(x,"a",1)

或者,如果你真的想强调你是从x的第一个角色开始,你可以做以下任何一个:

strncmp(x+0,"a",1)

strncmp(&x[0],"a",1)

答案 1 :(得分:0)

getchar()的行为取决于终端的模式。大多数人在&#34;煮熟&#34;模式,表示在您输入整行(并按下回车键)后getchar()返回。终端执行此操作以允许行编辑。要使getchar()立即返回,您需要将其切换为&#34; raw&#34;模式。

接下来,您应该启用所有编译器警告,因为它会告诉您上面的代码有什么问题:

strncmp()期望char*作为第一个参数,但您通过了char。这意味着代码将从任意内存中读取。

x[0] != NULL也没有意义(将字符与空指针进行比较)。要知道fgets()是否没有返回任何内容,请查看其返回码。

char * success = fgets(x, 2, stdin);
if(success == null) { ... error handling... }

if (strncmp (x,"a",1) == 0) {
    printf("yay");
}

答案 2 :(得分:0)

对未来的评论

从完整源代码中删除#include行通常没有帮助。如果我想去编译你的代码,我现在必须烧几分钟把它们拉到一起。这是浪费时间。

这是我需要添加的额外标题:

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MSGSIZE 100
#define PORTNUM 12

#define SA struct sockaddr

void error(const char *);
void send_userName(const char *, int);

现在......

因此,如果我添加这些标题,并尝试编译您的代码,我会收到一些严重可怕的警告。让我们来看看第一类警告。

此类是您将变量传递给寻找不同类型变量的函数的地方。

foo.c:59:19: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with & [-Wint-conversion]
    while ( fgets(myName[i],MSGSIZE,stdin ) == NULL){}
                  ^~~~~~~~~
                  &
/usr/include/stdio.h:236:30: note: passing argument to parameter here
char    *fgets(char * __restrict, int, FILE *);
                                ^
foo.c:74:18: warning: comparison between pointer and integer ('int' and 'void *')
        if (x[0] != NULL)
            ~~~~ ^  ~~~~
foo.c:77:26: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Wint-conversion]
            if (strncmp (x[0],"a",1) == 0)
                         ^~~~
                         &
/usr/include/string.h:84:26: note: passing argument to parameter here
int      strncmp(const char *, const char *, size_t);
                             ^

第二类是使用未初始化变量的地方:

foo.c:59:26: warning: variable 'i' is uninitialized when used here [-Wuninitialized]
    while ( fgets(myName[i],MSGSIZE,stdin ) == NULL){}
                         ^
foo.c:18:18: note: initialize the variable 'i' to silence this warning
    int sockfd, i;
                 ^
                  = 0

您应该尝试修复这些问题,因为否则您的代码会被破坏。如果你不明白为什么会发出特定的警告,那么你应该问一下。