Strcmp with pointers not working in C

Why is this code not working. Just try to check if user input is the same as password

char *pass; printf("Write the password: "); scanf("%s", pass); // Because is a pointer the & is out ? if( strcmp( pass , "acopio") == 0) 
+5
c pointers strcmp
Nov 06
source share
6 answers

In fact, you did not allocate space for the data. The definition of a pointer defines only a variable that can contain the address of a data block; it does not allocate a block.

You have a couple of options, allocate dynamic memory from the heap to write it and make a pointer to it. Or use the statically allocated memory on the stack and pass its address to your calls. In this case, a small benefit for dynamic memory (because it is temporary in use and small). You would have more work if you used dynamic memory - you need to make sure that you get what you asked for when distributing it and make sure you return it when you're done and make sure you don't use it after how you gave it back (difficult in a large application, believe me!) This is just more work and you don’t need extra effort.

The examples below will also require significant error checking, but give a general idea.

eg.

 char *pass = malloc (SOMESIZE); printf("Write the password: "); scanf("%s", pass); if( strcmp( pass , "acopio") == 0) 

or

 char pass[SOMESIZE]; printf("Write the password: "); scanf("%s", pass); if( strcmp( pass , "acopio") == 0) 
+8
Nov 06
source share

pass is a uniform pointer, and you are trying to write to it. You must allocate enough memory to store the string. For example, char pass[SIZE] will work better.

+3
Nov 06
source share

You need to allocate pass , so scanf will have a place to store input. Otherwise, you have memory corruption.

+1
Nov 06
source share

Yes, the pointer was not initialized. If you debug it, you get an access violation or segmentation fault . The code can be changed as follows.

  char pass[22];//22 can be replaced with other number printf("Write the password: "); scanf("%s", pass); if( strcmp( pass , "acopio") == 0) printf("fu");//just to check 
0
Nov 06
source share

You did not initialize pass to specify a buffer or other place to store input.

For something simple, you can declare pass as a char array instead of a pointer:

 char pass[N]; // where N is large enough to hold the password plus a 0 terminator scanf("%s", pass); if (strcmp(pass, "acopio") == 0) { ... } 

_Alignof it is an operand of the sizeof , _Alignof or unary & or string literal operators to initialize another array in the declaration, an expression of type "N-element array" from T will be converted ("decay") to an expression of type "pointer to T ", and the value of the expression will be the address of the first element of the array.

When you pass pass as arguments to scanf and strcmp , the type of the pass expression is converted from "N-element array of char " to "pointer to char ", and the value of the expression is the address of the first pass element or &pass[0] . Therefore, you do not need to use the & operator in a scanf call.

Similarly, in the strcmp call strcmp string literal "acopio" is converted from an expression of the type "7-element char array" ( const char in C ++) to "pointer to char ",

0
Nov 06
source share
 #include<stdio.h> main() { int mystrcmp(char *,char *); char s1[100],s2[100]; char *p1,*p2; p1=s1; p2=s2; printf("Enter the first string..?\n"); scanf("%s",p1); printf("Enter the second string..?\n"); scanf("%s",p2); int x=mystrcmp(p1,p2); if(x==0) printf("Strings are same\n"); else printf("Strings are not same..\n"); } int mystrcmp(char *p1,char *p2) { while(*p1==*p2) { if(*p1=='\0' || *p2=='\0') break; p1++; p2++; } if(*p1=='\0' &&as *p2=='\0') return(0); else return(1); } 

simple code for beginners ....

0
Jan 26 '16 at 20:34
source share



All Articles