0

I am trying to figure out a simple program, but I can't quite get it to work. What am I doing wrong here?

char* getpass()
{
    char password[10];
    scanf("%s\n", password);
    return password;
}
sshashank124
  • 31,495
  • 9
  • 67
  • 76
speroni1
  • 63
  • 1
  • 4
  • Thank you for the response. @sshashank124 I apologize for being a newb, and I'm still sorting how to navigate this site... So it looks like the password is a local variable and so inaccessible by main. But if password is in the return line, why doesn't that make it accessible outside of this function? Doesn't that rather defeat the purpose of the return line? – speroni1 Jan 05 '20 at 04:55
  • Yes, did you read the answers in the linked duplicate at the top of your question? – sshashank124 Jan 05 '20 at 04:56
  • Yes, I have to do some other memory manipulation to make it accessible. I'm just unsure why the return line itself doesn't make it accessible. That doesn't seem to be explained in the other answers. – speroni1 Jan 05 '20 at 04:57
  • You aren't returning a string. You're returning a char pointer to a sequence of chars. If the sequence of chars was only allocated within the function, and the function has now exited, what does your pointer refer to? – sshashank124 Jan 05 '20 at 04:59
  • 1
    That makes sense. Thank you very much. – speroni1 Jan 05 '20 at 05:04

0 Answers0