#include <stdio.h> #include <string.h> int main() { char **input_pass; char main_pass[] = "qwerty123"; scanf( "%s", &input_pass ); if( strcmp( &input_pass, &main_pass ) == 0 ) { return printf( "Equals" ); } else { return printf( "You made mistake" ); } return 0; } 

Although this code is compiled, the entered data falls into an arbitrary area of ​​memory. What are the errors here and how to solve this problem?

  • Maybe all the same just enough pointer when declaring input_pass ? - post_zeew
  • 2
    Read in the documentation what type of argument expects %s . See what you have. Draw conclusions. Repeat the same for the strcmp function. Read the warnings that the compiler gave you when compiling this. - VladD
  • The input_pass pointer indicates where it is unknown. Why don't you (for a start) just read the password in a character array? Write char input_pass[100]; and try. - avp
  • It turns out that the error was here char **input_pass; Thanks a lot! I will definitely take your advice into account. - Vladimir

2 answers 2

The principle of working with strings in C is that they represent an array of characters ( char ), the last one whose bounding element is the zero character \0 . And a string variable is essentially a pointer to the first element of this array. Another consideration is that the name of an array can be viewed as a (constant) pointer to its first character.

Proceeding from this, we consider what the function scanf("%s",???) needs. And she needs to specify a place in memory where to read data. Those. in fact, the address of the place where the first character will be located.

Recall what was said earlier. If we have an array, then its name is this address, so you can write

 char array[50]; ... scanf("%s",array); 

Memory can be allocated dynamically, for example:

 char * str = malloc(50); ... scanf("%s",str); 

After the first instruction, str points to allocated memory (that is, it contains the address of the first character in this memory).

The string comparison function strcmp , the pointers to the first characters of the strings, wants exactly the same arguments. So, as in scanf , in strcmp you need to pass pointers to the place in memory where strings are stored (and not to the place in memory where the addresses of the first elements are stored), i.e.

 strcmp(sr, array); 
  • When debugging, the twisted: Program "[3848] InputToSystem.exe" ended with code 6 (0x6). - Vladimir
  • @ Vladimir Give the full code of the program - what can we say without a code? ... - Harry

When entering strings into an array of type char, you should always give the input field size less than the length of the array to avoid going beyond its boundaries, given that zero is written to the end. Pass to the scanf with the address of the first element of the array:

 char buf[50]; scanf("%49s", buf); 

Never use %s without length.

According to POSIX.1-2008, you can also use the m modifier, so that scanf() itself places the input string in a dynamic array, which should be freed using free() . In this case, the address of the pointer to the first element of the byte array must be transferred:

 char *buf; scanf("%ms", &buf);