int main()
{
int n = 56;
char buf[10]; //want char* buf instead
sprintf(buf, "%i", n);
printf("%s\n", buf);
return 0;
}
This piece of code works, my question is what if i want the same behaviour, with buf being a char* ?
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.
bufis an array statically allocated on the stack. For it to be of typechar *you need to allocate dynamically the memory. I guess what you want is something like this:EDIT: As pointed out by ‘Thomas Padron-McCarthy’ in the comments, you should not cast the return of
malloc(). You will find more info here. You could also remove completely thesizeof(char)as it will always by1.