0

I'm trying to Write a function that splits a string and returns an array of each word of the string. That array should finish with a NULL pointer. But when I run the program I'm having a segmentation fault Error.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

char **_strtok(char *str, const char *delim);

int main(void)
{
        char *str = "one,two,three/four-five six seven";
        char **ptr = _strtok(str, ",- /");
        int i;

        for (i = 0; ptr[i]; i++)
        {
                printf("%s\n", ptr[i]);
        }
        free(ptr);
        return (0);
}

/**
 * _strtok - function that splits a string and returns
 * an array of each word of the string
 * @str: the string to be parsed
 * @delim: the delimeter
 * Return : an array of each word of the string
 */

char **_strtok(char *str, const char *delim)
{
        char **ptr, *temp;
        int i, j, m, n, k = 0, l = 0;

        temp = malloc(strlen(str) + 1);
        if (temp == NULL)
        {
                printf("ERROR: Memory allocation failed\n");
                exit(1);
        }
        ptr = malloc(strlen(str) * 2);
        if (ptr == NULL)
        {
                printf("ERROR: Memory allocation failed\n");
                exit(1);
        }
        for (i = 0; str[i] != '\0'; i++)
        {
                for (j = 0; delim[j] != '\0'; j++)
                {
                        if (str[i] == delim[j])
                        {
                                temp[l] = '\0';
                                for (m = 0; temp[m] != '\0'; m++)
                                {
                                        ptr[k][m] = temp[m];
                                }
                                ptr[k++][m] = '\0';
                                free(temp);
                                temp = malloc((strlen(str) + 2 - l));
                                if (temp == NULL)
                                {
                                        printf("ERROR: Memory allocation failed\n");
                                        exit(1);
                                }
                                l = 0;
                                break;
                        }
                }
                if (delim[j] == '\0')
                {
                        temp[l++] = str[i];
                }

        }
        temp[l] = '\0';
        for (n = 0; temp[n] != '\0'; n++)
        {
                ptr[k][n] = temp[n];
        }
        ptr[k++][n] = '\0';
        ptr[k] = NULL;
        free(temp);
        return (ptr);
}
        

The expecting result is an array of string that should be printed in the standard output. But I'm getting a segmentation default.

  • 2
    Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Feb 20 '23 at 23:19
  • 2
    Probably has something to do with `ptr` being the array of pointers, but the `malloc` that creates it does not have `sizeof(char*)` multiplier. – Dialecticus Feb 20 '23 at 23:22

1 Answers1

1

You are allocating 1D array to ptr,

ptr = malloc(strlen(str) * 2);

But it is accessing in 2D

ptr[k][m] = temp[m];

Which definitely cause an issue.