void* myfunction() {
char *p;
*p = 0;
return (void*) &p;
}
I know the problem is in the return statement, but it’s confusing. Any hints?
Thanks.
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 have a problem before the return statement in the
*p = 0;. You haven’t initializedp, so this writes to whatever random location that unitialized pointer happens to hold. That gives undefined behavior, so nothing afterwards has any meaningful interpretation at all.Assuming you fixed that, then yes, the return statement would be a problem as well — you’d be returning a pointer to data that’s destroyed before the return completes, so any attempt at using the pointer you returned would cause undefined behavior again.