cpointersmemorymallocfree

local char pointers inside a function


I'm trying to code a function to filter non-alphabetic characters from a string in C.

So inside the function wordFilter I initialize a char array in order to store there the filtered string and then assign it to the pointer so I can return it.

However, it turns out that whenever the function is executed after the first time, it seems that the char array "reference" still keeps the value it when the last execution was done. Is there a way to make the "reference" array to be empty every time at the beginning of the execution of the function.

Here is my code:

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

char *wordFilter(char *string) {
    char reference[strlen(string)];
    int index = 0;
    while (*string) {
        if (isalpha(*string)) {
            reference[index] = *string;
            index++;
        }
        string++;
    }
    char *ptr = malloc(sizeof(char) * index);
    ptr = reference;
    return ptr;
}

int main() {
    char input[256];
    char *ptr = input;
    char input2[256];
    fgets(input, 256, stdin);
    
    char reference[256];
    for (int pos = 0; sscanf(ptr, "%s%n", input2, &pos) == 1; ptr += pos) {
        printf("%s ", wordFilter(input2));
    }
    return 0;
} 

Solution

  • The main problem is this line: ptr = reference; That doesn't copy the characters of reference into ptr. As Eugene pointed out, that line causes the variable ptr to point to the stack-allocated reference array, leaking the memory you malloced.

    Here are the changes I recommend:

    1. Allocate reference like char reference[strlen(string) + 1]; to allow room for the terminating null.
    2. After the while loop, null-terminate reference like this: reference[index] = '\0';
    3. Instead of using a malloc and copying the string, use strdup(reference) to heap-allocate a duplicate of reference, and then you can return what you get from strdup.