Reputation: 319
Why this code isn't working. Just trying to check if the user input is the same as a password
char *pass;
printf("Write the password: ");
scanf("%s", pass); // Because is a pointer the & is out ?
if( strcmp( pass , "acopio") == 0)
Upvotes: 8
Views: 23141
Reputation: 1
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello" ;
char str2[] = "HeLLo" ;
char str3[10];
int *s; // make "s" an integer pointer to string
/* if you want to scanf in a string to compare */
printf("Enter your password: ");
scanf("%9s", str3); //don't overrun the string size!
s = strcmp(str3, str1);
/* anything other than zero returned is not a match */
if( s == 0 ) {
printf(" The two strings are the same\n"); }
else printf(" The two strings are different\n");
/* print the value of s */
printf(" The value of s is %d", s);
return 0;
}
Upvotes: -2
Reputation:
#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....
Upvotes: -2
Reputation: 7798
You've not actually allocated any space to put data. Defining a pointer just defines a variable that can hold the address of a block of data, it doesn't allocate the block.
You have a couple of options, allocate dynamic memory off the heap to write into and make the pointer point to it. Or use statically allocated memory on the stack and pass the address of it to your calls. There's little benefit to dynamic memory in this case (because it's temporary in use and small). You would have more work to do if you used dynamic memory - you have to make sure you got what you asked for when allocating it and make sure you've given it back when you're done AND make sure you don't use it after you've given it back (tricky in a big app, trust me!) It's just more work, and you don't seem to need that extra effort.
The examples below would also need significant error checking, but give you the general idea.
e.g.
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)
Upvotes: 12
Reputation: 123458
You haven't initialized pass
to point to a buffer or other location to store the input.
For something simple like this, you can declare pass
as an array of char
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)
{
...
}
Except when it is the operand of the sizeof
, _Alignof
, or unary &
operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of 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 an argument to scanf
and strcmp
, the type of the expression pass
is converted from "N-element array of char
" to "pointer to char
", and the value of the expression is the address of the first element of pass
, or &pass[0]
. That's why you don't need to use the &
operator in the scanf
call.
Similarly, in the strcmp
call, the string literal "acopio" is converted from an expression of type "7-element array of char
" (const char
in C++) to "pointer to char
".
Upvotes: 0
Reputation: 39
Yes the pointer was not initialized. If you debug it you will get a 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
Upvotes: 0
Reputation: 18338
You need to allocate the pass
so the scanf
will have a place to store the input. Otherwise you have memory corruption.
Upvotes: 1
Reputation: 23699
pass
is an unitialized pointer, and you attempt to write into it. You have to allocate enough memory to hold a string. For example, char pass[SIZE]
will work better.
Upvotes: 5