Fraizeraust wrote:I fixed it by myself with strcmp() function. Now it compares strings flawlessly. Thanks erkinalp!
@val: Well, you're kinda right and this is what I am doing. I just wanted someone to point out my mistake(s) in this code so I can be sure to not repeat it again.
i was not trolling, I adviced what I saw is the best for this level of knowledge. I've learnt C exactly that way.
pointers are a tricky topic, they should have been learnt about very thoroughly.
you messed with them a lot. that means you need something more educational than forum answers, cause noone's gonna write out several pages explanations on the topics as it's done in books.
scanf needs an address of a buffer where to store an input it scans for. that should be an address of an array of chars, and using scanf() already is a very bad pratice by the way, it's a direct way to stack buffer overflow errors - the most infamous source of the most severe vulnerabilities. using scanf() and gets() should be strongly discouraged even for educational purposes. honestly they should have been removed from the standard library at all. but, scanf() expects an address of a string buffer. this would be this one - char StringArgs[10]; in your code. In C, the name of an array variable without brackets, thus StringArgs, means an address of the array, exactly this you should pass to scanf(). this is the same as &StringArray[0] or even better - &*(StringArray + 0)
. You instead passed to it the address of an unintitialized pointer StringSTDIN, which happenned to lay before in the stack. This results in stack corruption of course, because your pointer would get char codes instead of some address. your program exited gracefully just by the luck, - you didn't provide any actions for the third variant of the branch (where the code flow got inevitably, since you've messed up with string literals even more and they don't have a chance to evalute to true anywhere), and thus it exited, reaching the function's exit point.
String literals like this "my string" in C mean constant data, which compiler puts in a read only data section, and when you use such quoted strings in the code, think of it, that instead the pointer to this string in the data section would be inserted by the compiler. it's true, compiler inserts addresses there. So when you do such a comparison like this:
if(Pointer == "my string");
it's kind of valid from the compiler point of view, because you compare 2 pointers values possibly of the same type, but this expression never ever will evaluate to true, because you just won't know the address of the string "my string", you only could know it this way:
Pointer = "my String";
but then the comparison won't have any sense, it's always true.
of course you should do that:
strcmp(Buffer, "Yes");
where Buffer is your char Buffer[BUFFER_LENGTH]; where scanf() (hopefully) has put its input;
But this random explanation won't be helpful for you. Compared to books. That's why I adviced reading them.