0

I'm new in C programming. I want to scan some names and then put them in an array only if this name does not already exists and if it doesn't put this name in an empty cell. Here is the code that I wrote but when it comes to the if statement the systems crashes.

char sing_in[100];
char username[20];
char *x1[2];
int z;

for(z=0; z<2; z++)
{
    printf("dwse to username\n");
    scanf(" %s", &username);

        if (strcmp(username,sing_in[z]) == 0 )
        {
            printf("this username already exists\n");
        }
        else if( sing_in[z] == 0)
        {
            *x1[z] = username;
            *x1[z] = sing_in[z];
            printf("new username added\n");
        }
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 2
    The variable `x1` is an array of pointers, but you don't make them point anywhere before you dereference them. That leads to *undefined behavior*. I assume you're *not* supposed to actually dereference them? Not dereferencing brings other problems though, like why you make `x1[z]` point to somewhere and the directly make it point somewhere else? I think you need to sit down and think about what you are trying to accomplish. – Some programmer dude Jan 05 '17 at 06:32
  • There are also other problems, like you using `sing_in[z]` (which is a single character) as a string. – Some programmer dude Jan 05 '17 at 06:36
  • And you have never set the contents of `sing_in` so it contains garbage. – kaylum Jan 05 '17 at 06:37
  • Also in scanf(" %s", &username); Don't use `&` – Shashi Kundan Jan 05 '17 at 06:39
  • 1
    @ShashiKundan: use back-ticks ```…`code`…``` around code in comments – Jonathan Leffler Jan 05 '17 at 06:41
  • What are you trying to achieve? The variable sign_in does not have any values are you are comparing it and if it is 0 you are assigning that value to a pointer ? This is not correct. Also as i have checked Pointer is not initialized and you are trying to dereference it. That is not a good idea as far as i know as it would crash or create undefined behavior. – Vimal Bhaskar Jan 05 '17 at 07:03
  • You need to check that the return value of scanf is one. – August Karlstrom Jan 05 '17 at 09:22

0 Answers0