0
char* reverse(char*);

main()
{
    printf("\n%s", reverse("computer"));
}

char* reverse(char* p)
{
    int l, i;
    char t;

    for (l = 0; *(p + l) != '\0'; l++)
        ;

    for (i = 0; i < l / 2; i++)
    {
        t = *(p + i);
        *(p + i) = *(p + l - 1 - i);
        *(p + l - 1 - i) = t;
    }

    return (p);
}  

Not getting output. output shows Process returned -1073741819 (0xc0000005).

bolov
  • 72,283
  • 15
  • 145
  • 224
changer
  • 3
  • 1
  • 5
    In C all literal strings (like the `"computer"` you pass as argument) are *read only* and can't be modified. Attempting to do so will lead to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). Therefore, when dealing with such strings, always consider them as `const char *`. – Some programmer dude Nov 18 '18 at 08:23
  • 1
    Please properly indent your code. There was someone kind enough to do it for your, but he/she did it wrong. So please indent your own code. It's unreadable as it is. – bolov Nov 18 '18 at 08:29
  • You must write `int main()` and not `main()`. The former is an obsolete non-standard form. You also must `#include` relevant system headers. [See here](https://ideone.com/JvATss). – n. m. could be an AI Nov 18 '18 at 08:33
  • @bolov I am new here. I learnt how to intend code from you. – changer Nov 18 '18 at 08:39
  • ok, I've formatted the code from you. From now own it's your responsibility to do so. – bolov Nov 18 '18 at 08:42
  • also `main()` needs to be `int main()` in standard C – bolov Nov 18 '18 at 08:44

1 Answers1

0

You should use char st[] = "computer"; reverse(st); not reverse("computer"); for "computer" is literal string.

The following code could work:

#include <stdio.h>

char* reverse(char*);

int main() {
  char st[] = "computer";
  printf("%s\n", reverse(st));
  return 0;
}

char* reverse(char* p) {
  int l, i;
  char t;
  for (l = 0; *(p + l) != '\0'; l++)
    ;
  for (i = 0; i < l / 2; i++) {
    t = *(p + i);
    *(p + i) = *(p + l - 1 - i);
    *(p + l - 1 - i) = t;
  }

  return p;
}
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20