0

cant solve the warning in c called s1 s2 is used uninitialized this function

int main()
{
    char *s1, *s2;
    printf("Please enter string s1 and then s2:\n");
    scanf("%s %s", s1, s2);
    printf("%s %s", *s1, *s2);
return 0;
}

1 Answers1

1

You have to allocate for s1 and s2:

// do not forget to include the library <stdlib.h> for malloc function
s1 = malloc(20); // string length of s1 ups to 19;
if(!s1) {return -1;}
s2 = malloc(20) // // string length of s2 ups to 19 also;
if(!s2) {return -1;}

The in scanf function, should change to (Disadvantages of scanf):

scanf("%19s %19s", s1, s2); // or using fgets

Or you can use the array of character instead of using pointer:

char s1[20], s2[20];
// Or you can define a maximum length MAX_LEN, then using:
// char s1[MAX_LEN], s2[MAX_LEN]; 
Hitokiri
  • 3,607
  • 1
  • 9
  • 29