-1

I'm trying to write a code in c that return 1 if there is "&" in the string and 0 otherwise. In addition, the char* that I receive in the function I want to put it in an array of chars and NULL in the end. My code is like this:

char** isBackslash(char* s1, int *isFlag) {
    int count = 0;
    isFlag = 0;
    char **s2[100];
    char *word = strtok(s1, " ");
    while (word != NULL) {
        s2[count] = word;
        if (!strcmp(s2[count], "&")) {
            isFlag = 1;
        }
        count++;
        word = strtok(NULL, " ");
    }
    s2[count] = NULL;
    return s2; 
}

For example, if the original string (s1) is "Hello I am John &".
So I want s2 to be like:

s2[0] = Hello
s2[1] = I
s2[2] = am
s2[3] = John
s2[4] = &
s2[5] = NULL

And the function will return '1'. What is wrong with my code? I debugged it and unfortunately, I don't find the problem.

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
Pedro Gómez
  • 214
  • 1
  • 8
  • 1
    `char *s2[50];` You are shadowing the parameter `s2`. – 001 Mar 26 '21 at 16:36
  • `char *s2[50]` is a local variable; it vanishes when the function returns. You also have an an argument `char *s2` — that is hidden by the local variable. You have some serious thinking to do. It is likely that you need the argument to become `char *s2[]` or `char **s2` and you delete the local version. – Jonathan Leffler Mar 26 '21 at 16:37
  • @JonathanLeffler So know I edited it to char **s2 but it still don't working – Pedro Gómez Mar 26 '21 at 16:38
  • One problem is that you're doing two completely separate things in one function – klutt Mar 26 '21 at 16:39
  • Err; why are you doing a `malloc()`? You need to pass in an array of pointers. If you're going to allocate memory in the function, you'll need `char ***s2` and to allocate `*s2 = malloc(50 * sizeof(**s2));` and so on. Beware [Three Star Programmers](http://c2.com/cgi/wiki?ThreeStarProgrammer). And I see you're reallocating as well — be cautious. – Jonathan Leffler Mar 26 '21 at 16:41
  • 1
    It might be easier to change to `char ** isBackslash(char* s1, int *isFlag)` – 001 Mar 26 '21 at 16:42
  • @JohnnyMopp I edited it again, now the code should work? – Pedro Gómez Mar 26 '21 at 16:47
  • 1
    If you want to use dynamoc allocation, you need to go back to the 2nd version that uses `malloc/realloc`. Just change `s2` to be a local variable and return it at the end. – 001 Mar 26 '21 at 16:49
  • 1
    Hello Pedro, see my answer for a working example, please do not edit too much or also keep the old version, else my answer will make less sense – Antonin GAVREL Mar 26 '21 at 16:50
  • @AntoninGAVREL Yeah I see, it's help me. Thanks! – Pedro Gómez Mar 26 '21 at 16:54
  • @JohnnyMopp When I want to change the parameter of `int *isflag` I need to do `isFlag=1` or something else? – Pedro Gómez Mar 26 '21 at 17:35
  • Since it is a pointer, you need to dereference it: `*isFlag = 1;` – 001 Mar 26 '21 at 17:54

1 Answers1

1

You were shadowing your own parameter. See a working example below:

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

#define BUFF_SIZE 256

int isBackslash(char* s1, char s2[][BUFF_SIZE]) {
    int isFlag = 0;
    int i = 0;
    int j = 0;
    int n = 0;

    while (s1[i] != '\0') {
        isFlag |= !('&' ^ s1[i]); // will set the flag if '&' is met
        if (s1[i] == ' ') {
            while (s1[i] == ' ')
                i++;
            s2[n++][j] = '\0';
            j = 0;
        }
        else 
            s2[n][j++] = s1[i++];
    }

    return isFlag;
}

int main(void) {
    char s2[BUFF_SIZE/2+1][BUFF_SIZE];
    memset(s2, 0, sizeof(s2));

    char s1[BUFF_SIZE] =  "Hello I am John &";
    int c = isBackslash(s1, s2);


    printf("%d\n", c);

    int i = 0;
    while (s2[i][0] != '\0')
        printf("%s\n", s2[i++]);
}
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81