2

I am trying to test some code from "Pointers On C", the find_char function, which searches for a specified character. I added something of my own, that is, the main() function that calls the find_char and includes the initialized data to be searched(an array of pointers to char). I managed to fix all errors and warnings at compile time, but while trying to run the a.out file, I got the segmentation fault error.

I know segmentation fault is mostly concerned with array and pointers which so often happens when types are incorrectly translated. But I really can't find what is wrong with my code.

Thank you so much.

#include <stdio.h>
#define TRUE  1
#define FALSE 0

int find_char( char **strings, char value);

int main(void)
{
    int i;
    char c = 'z';
    char *pps[] = { 
            "hello there",
            "good morning",
            "how are you"
    };  
    i = find_char(pps, c); 
    printf("%d\n", i); 
    return 0;
}

int find_char (char **strings, char value)
{
    char *string;
    while (( string = *strings++) != NULL)
    {   
            while (*string != '\0')
            {   
                    if( *string++ == value )
                            return TRUE;
            }   
    }   
    return FALSE;
}
2
  • 5
    If you're going to test for a null string pointer in your array of string pointers, perhaps you should actually put one at the end of the list. I.e. "how are you", NULL. Right now your code hits the last string (which is obviously non-null), then just continues marching into the ether in search of exactly what you asked it to: a NULL. Commented Aug 2, 2017 at 5:26
  • Or pass the number of strings as a parameter to find_char, e.g. int find_char( char **strings, size_t n, char value); and iterate with a for (size_t i = 0; i < n; i++) loop or while (n--) to only iterate over the proper number of pointers. Commented Aug 2, 2017 at 5:42

1 Answer 1

2

Well your problem come from your outer while loop. It loops over the strings in the array until it finds a NULL string. However, you didn't terminate the array with such string, so the loop will go on after the 3rd string and end up trying to access memory it isn't supposed to.

A solution to your problem: You can modify your function's signature by giving it a size parameter so that it becomes

int find_char(const **char strs, int size, char ch);

Then you need to include this size value in the looping process to find your stop condition.

char *s = NULL;
int i = 0;
while (i++ < size && (s = *strs++)) {
    // ...
}

The point to remember here is that, in C programming it is required to play with memory and arrays carefully. When dealing with arrays, one usually want to pass the array's size as a parameter to each processing functions.

I hope that helped.

Sign up to request clarification or add additional context in comments.

2 Comments

Or simply while (size-- && (s = *strs++)), or eliminate s and just use indexing, e.g. for (int i = 0; i < size && strs[i]; i++). All are fine as is your answer.
Yes there are many ways of doing it. C is very permissive on that regard. Thanks for completing my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.