Thread: segmentation fault with strlen

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    segmentation fault with strlen

    Hey guys I have a function that looks like this:int check(const char str[])

    I am trying to count the size of the string length using strlen but it keeps giving me a segmentation error. Does anyone know why this is happening and how I can fix this?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int check(const char str[])
    {
    int a;
    
    
    a=strlen(str)
    
        if(a==10)
        {
          /*execute this block of code */
        }
    
    
    }
    Last edited by Thegame16; 02-05-2011 at 12:13 AM.

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    You need to make sure str[] is terminated by a 0, otherwise strlen() will run off the end and cause the seg fault.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Boxknife View Post
    You need to make sure str[] is terminated by a 0, otherwise strlen() will run off the end and cause the seg fault.
    how do i do that?

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Maybe you've declared your argument wrong. Try using a const char * instead. Also, it would help to post how you're using the check() function.

    Also, check() doesn't return anything, so change it's return type to void.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check the code that calls the function. The function itself cannot sensibly perform such a check.

    Quote Originally Posted by Babkockdood
    Maybe you've declared your argument wrong. Try using a const char * instead.
    It is the same thing in this context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM