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)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
or