seg fault with strcmp

This is a discussion on seg fault with strcmp within the C Programming forums, part of the General Programming Boards category; does anyone know of common reasons to get seg faults with strcmp I am reading a string of characters into ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    27

    seg fault with strcmp

    does anyone know of common reasons to get seg faults with strcmp

    I am reading a string of characters into a buffer, When I try to compare that buffer with the string "BYE" I get a seg. fault.
    I know that "BYE" is in the buffer because I print it right before hand and that works fine. I was thinking that I can't just pass "BYE" straight into strcmp, but first setting a variable to old BYE and then passing that in didn't seem to work either. I have used strcmp and strncmp.

    I am running FBSD Unix and using gcc to compile


    Code:
        fprintf(stderr,"recieved from socket: %s\n",buf);
    
        if(strncmp(buf,"BYE",3) == 0){
    	 fprintf(stderr,"exiting the program\n");
    	 break;
        }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    27
    buf is declared like
    char buf[100];

    I am reading into it from a socket using recv()

  3. #3
    Enixia
    Guest
    Most likely is not null terminated then, place something like this in your code:

    buf[99] = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21