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)
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)
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)
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.
char
), but it is useless here. –
Kulun You need to allocate the pass
so the scanf
will have a place to store the input. Otherwise you have memory corruption.
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
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
".
#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....
© 2022 - 2024 — McMap. All rights reserved.