-1

i'd like to know why this code works fine with char tab[100] but doesn't work if I use char *tab ? fgets function takes a char* array as a parameter right ?

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

 Int Palindrome(char* str, int i, int j);

 int main()
{
    char tab[100];
    printf("Enter your string : \n");
    fgets(tab, 100, stdin); 
    int j = strlen(tab);
    printf("%d\n", Palindrome(tab, 0, j - 2));
    return 0;
}

int Palindrome(char* str, int i, int j)
{
    if (i >= j)
    {
        printf("My word is a Palindrome !\n");
        return printf("<(^w^)>\n");
    }
    else if (str[i] != str[j])
    {
        printf("My word is not a Palindrome !\n");
        return printf("<(X.X)>\n");
    }
    else 
    {
        return Palindrome(str, i + 1, j - 1);
    }
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 3
    where is that `char tab` thingy? – Sourav Ghosh Jul 18 '17 at 09:37
  • 4
    It will work with `char *tab= malloc(100);` – Paul Ogilvie Jul 18 '17 at 09:39
  • 1
    "doesn't work" How? What error did you get? Where is this `char *tab` declared? Did you ever bother to allocate memory for it? Put more effort into asking a usable question. – underscore_d Jul 18 '17 at 09:40
  • 1
    What do you mean "but doesn't work if I use char *tab " ? Have you allocated your string? – LotoLo Jul 18 '17 at 09:41
  • 1
    An array always has actual storage space, (neglecting the trivial [0]), a pointer does not. They are different things, so it's not surprising that one will work when the other does not, (or invokes UB). – Martin James Jul 18 '17 at 09:42
  • 2
    Read the chapter dealing with pointers in your C textbook. – Jabberwocky Jul 18 '17 at 09:43
  • 1
    It was a massive error to allow passing arrays by implicit pointer. C would have been a much better language if array passing required an explicit address operator, eg 'Palindrome(&tab, 0, j - 2)', (or indeed, an actual 'real' string type with a length, but that's another ball of wibbly-wobbly). Now we're stuck with this 'arrays and pointers are the same' fallacy that has to continually explained away:( It's fairly clear that Dennis was on tequila when defining this language feature. – Martin James Jul 18 '17 at 09:46
  • Possible duplicate of [Char array vs char pointer in C](https://stackoverflow.com/questions/10186765/char-array-vs-char-pointer-in-c) – Paul Roub Aug 04 '17 at 15:38

1 Answers1

7

With "not work" you probably mean you get some serious error reported like a segmentation fault.

The difference between char tab[100] and char *tab is that the first has storage allocated and the second hasn't. When you call a function with an array as a parameter, then the compiler passes a pointer to the first element of the array, so for the function that got called it doesn't see the difference whether it is called with an array-parameter or with a pointer-parameter.

So to let your program work with char *tab; you must first allocate storage to this pointer, such as with char *tab=malloc(100); Now that there is valid storage allocated (and the pointer now points to it), you can call your function with this tab as parameter.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41