0

I'm new to the C language, and I can't understand why my code gives an error, please help me

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
char* d(char*);
int main(){

char* str = "Google";
d(str);
return 0;


}
char* d(char* str){
int x = 0;
int len = strlen(str);
while (x != len){
if (str[x] == str[x+1]){
str[x] = 0;
str[x+1] = 0;
}
x++;
}
str = '\0';
return str;
}

I need to draw a field without repeating letters

kaylum
  • 13,833
  • 2
  • 22
  • 31
oppo
  • 13
  • 1
  • Does this answer your question? [Why do I get a segmentation fault when writing to a "char \*s" initialized with a string literal, but not "char s\[\]"?](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a) – kaylum Nov 19 '20 at 00:49
  • 1
    That's some seriously weird looking C#. It [doesn't compile](https://rextester.com/WUTGO34964). Please provide a [mcve] that demonstrates the issue. – ProgrammingLlama Nov 19 '20 at 00:49

1 Answers1

1

The statement char *s = "Google" creates a string literal. The string literal is stored in the read-only part of memory by most of the compilers.

Try this:

int main()
{
//    char *str = "Google";
    char str[] = "Google";
    d(str);
    return 0;
}

It should do what you want.

See What’s difference between char s[] and char *s in C? for more details.

ioworker0
  • 46
  • 2