1

I have this code which function must return the given string without some symbols. The problem is I have problems printing the returned value of badWords function. It prints "????" and I don't know how.

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

char * badWord(int, char*);

int main(int argc, char **argv){
    char s[100]="O joao` foi as compra's, comprou um umb'igo! falante por £233";
    int n = sizeof(s);

    char* word = badWord(n, s);
    printf("%s\n", word);
}

char * badWord(int n, char s[n]){
    char palavra[n];
    int index=0;
    for(int i=0; i<n; i++){
        char c = s[i];
        char c2 = s[i+1];

        if(isalnum(c) || (ispunct(c) && isalpha(c2))){
            palavra[index]=c;
            index++;
        }
    }
    return palavra;
}
  • `palavra` is a local array...... – LPs Dec 16 '19 at 11:17
  • You can’t return locally allocated memory to a caller. It gets deallocated when function ends and is undefined behavior. – Sami Kuhmonen Dec 16 '19 at 11:18
  • @André Morais What have the function to do? – Vlad from Moscow Dec 16 '19 at 11:20
  • @SamiKuhmonen That's not correct. You can return locally allocated arrays; it is commonly done. What is wrong is returning an array defined as a local variable, because it is saved in the stack area and it will be overwritten as soon as the function returns. – Roberto Caboni Dec 16 '19 at 11:21
  • @Cubo78 Show me *locally allocated memory* that doesn’t get removed on function exit? Meaning it’s in stack. Not allocated *globally* in heap. – Sami Kuhmonen Dec 16 '19 at 11:23
  • It's probably a *lost in translation* case. I interpreted "locally allocated" with "(dynamically) allocated within a function. – Roberto Caboni Dec 16 '19 at 11:26
  • 1
    `char * badWord(int n, char s[n])` ---> `void badWord(int n, char s[n], char palavra[n]);` – LPs Dec 16 '19 at 11:26
  • 1
    @LPs correct. Or, even better: `int badWord(int n, char s[n], char palavra[n]);`, in which the function could check for bad parameters (e.g. NULL pointers) and return 0 on success and -1 on failure. – Roberto Caboni Dec 16 '19 at 11:28
  • @Cubo78 Correct, or simply return `Index` ;) but I jave no time to make that comment a good answer at the moment – LPs Dec 16 '19 at 11:29
  • thank all of you, it's solved! – André Morais Dec 16 '19 at 11:31

1 Answers1

0

Solved! Reading the comments, I turn local array "palavra" into global array, and it worked!

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

char * badWord(int, char*);

int main(int argc, char **argv){
    char s[100]="O joao` foi as compra's, comprou um umb'igo! falante por £233";
    int n = sizeof(s);

    char* word = badWord(n, s);
    printf("%s\n", word);
}
char palavra[10000000];
char * badWord(int n, char s[n]){
    int index=0;
    for(int i=0; i<n; i++){
        char c = s[i];
        char c2 = s[i+1];

        if(isalnum(c) || (ispunct(c) && isalpha(c2))){
            palavra[index]=c;
            index++;
        }
    }
    return palavra;
}