-2
#include <stdio.h>
#include <string.h>

int scomp(char* x, char* y);

int main(void)
{
    printf("gimme a string: ");
    char* str1;
    scanf("%s",str1);
    printf("gimme another string: ");
    char* str2;
    scanf("%s",str2);
    printf("comparing them......\n__________\n");
    if(scomp(str1,str2) == 0)
    {
        printf("yup, they are the same\n");
    }else if(scomp(str1,str2)== 1){
        printf("They are different buddy..\n");
    }
    return 0;
}

int scomp(char* x, char* y)
{
    int n=0;
    while(x[n] != '\0')
    {
        if(x[n] == y[n])
        {
            n++;
        }else{
            return 1;
        }
    }
    return 0;
}

It gives me segmentation fault 11, there must be something wrong with the function I wrote in the last part which is supposed to compare strings. what's the problem?

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
  • What is the return value of your scanfs and what does it mean according to spec? – Yunnosch Sep 08 '17 at 15:31
  • 5
    You're reading data into unallocated memory (`str1` and `str2`), which is not a good idea. But there are also logic issues with your code, but not that would casue seg fault. – AntonH Sep 08 '17 at 15:31
  • 1
    What do str1 and str2 point to? – Warrior Sep 08 '17 at 15:31
  • 3
    In addition to unallocated pointers, function to compare strings should be rewritten: `while(x[n] != '\0')` -> `while(x[n] && y[n])` – unalignedmemoryaccess Sep 08 '17 at 15:31
  • OP, what if the strings are not same length? – Yunnosch Sep 08 '17 at 15:31
  • See https://stackoverflow.com/questions/45162908/difference-between-char-array100-and-char-array-when-calling-functions. – jarmod Sep 08 '17 at 15:32
  • @Yunnosch my solution corrects this. – unalignedmemoryaccess Sep 08 '17 at 15:32
  • @tilz0R I was not addressing you with this critc, edited to be clearer. Yours is convincing. – Yunnosch Sep 08 '17 at 15:34
  • @warrior excuse me if I have to allocate space with malloc when I create a string with char *, then why if I simply write: char* string; scanf("%s",string); printf("%s",string); it works and doesn't give me the same error? – Giannandrea Vicidomini Sep 08 '17 at 15:35
  • scanf expects a pointer and I don't know C enough to answer why it accepts a single character. However since str1 and str2 point to an invalid location, program will crash. – Warrior Sep 08 '17 at 15:41
  • 1
    @GiannandreaVicidomini It doesn't work, it just ***looks*** like it works. Because a pointer points to a random section of memory (that in your case isn't initialised), using `char *` causes error. But if you declare it `char string;`, it has a place and address on the stack and behaves as if it works, but you face possible undefined issues further down. – AntonH Sep 08 '17 at 15:41
  • @AntonH I I misspelled I intended to put the * symbol on char it works if I do char* string; scanf("%s",string); printf("%s",string); – Giannandrea Vicidomini Sep 08 '17 at 15:48

1 Answers1

0
char* str1;
scanf("%s",str1);

char* str2;
scanf("%s",str2);

str1 and str2are not initialised, and since they are pointer, you are basically writing the results of your your scanf into random places in memory.

This is why you get a Segmentation fault (your program tries to write on a memory segment where it is not authorized to write)

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • Also provide the solution. – unalignedmemoryaccess Sep 08 '17 at 15:35
  • @tilz0R I'd rather flag this question as duplicate. – Pac0 Sep 08 '17 at 15:37
  • Then leta do it :) – unalignedmemoryaccess Sep 08 '17 at 15:38
  • you are telling me that I should malloc some space right? nut why this work without giving the same error: char* string; scanf("%s",string); printf("%s",string); – Giannandrea Vicidomini Sep 08 '17 at 15:45
  • 1
    There are several reasons why your code is wrong. But this is clearly the first one to me. Your ocmment makes the same mistakes. **be careful with badly initialized pointers**. Your program **might** work sometimes, if you are lucky (like in your example in comment) ! Theses errors are one of the worst kind of mistakes you can make in C, because it can be very hard to debug if you are not careful. Please read some basic books on memory allocations and strings in C, for your own safety :) . – Pac0 Sep 08 '17 at 15:49
  • (either use malloc, either declare a precise array of char instead) – Pac0 Sep 08 '17 at 15:50
  • @dude, my example might be wrong, but it runs despite I didn't malloc any space. It takes input, it reads it and store in the variable and it prints it out again.. and it always work, why doesn't it give me the same set fault error that the other code gives me? that's what I want to know – Giannandrea Vicidomini Sep 08 '17 at 16:01
  • It runs *on your machine at the time you try*. The fact that your program runs is **not**, unfortunately, a proof that it is a correct C program! **This is called undefined behavior and it should be your worst enemy in C** . Because, this is a good example, it can work correctly, now. And then you forget about it. And suddenly, on another machine, on another date and time, at your customer's home, with some other lines of code following this, it doesn't work anymore. – Pac0 Sep 08 '17 at 16:12
  • and to answer more precisely your last question, I have no way of knowing if this is the "real" cause of your Runtime error. I can't know, because it is undefined behavior. First, fix this. Then look on the problems that you can encounter into with `scanf` in general (there are a lot ! ) , and the problems you can encounter with `strcmp` (a bit less, but there are some nasty too). And then you can search / ask a new more precise and different question. – Pac0 Sep 08 '17 at 16:16
  • @Pac0 so you are telling me that the only reason why my example works is that because of PURE luck, it doesn't write my string in a denied part of my memory thus not giving me sex fault error? and some other time could suddenly happen? – Giannandrea Vicidomini Sep 08 '17 at 16:21
  • Worst. It's not even required to fail. It can also pretty well *always* work with your compiler, because your compiler handle this precise case correctly. This is undefined behavior in the C standard. The compiler is free to do whatever it wants. It can spawn dragons on your screen once over 10000 compilations of your code, maybe, and still always prints the correct string you got with scanf. – Pac0 Sep 08 '17 at 16:31