#include <stdio.h&开发者_开发百科gt;
#include <stdlib.h>
void copyint(char *i,char** temp);
int main()
{
char* a="00313";
char* temp;
int inte;
temp=(char*)malloc(sizeof(char));
copyint(a,&temp);
inte=atoi(temp);
printf("%s\n",temp);
system("PAUSE");
free(temp);
}
void copyint(char *i,char** temp)
{
*temp=i;
}
no question so point what is wrong on the first sight: - copyint copies single char into pointed memory. so what is being done: temp value is '0', random, random....
'0',random random is parsed by atoi - undefined since we don't know what under this pointer resides. and then it's printed out...
use strlen malloc strcpy
sequence instead
Edit: it doesn't compile since you pass a (char*) into function which accepts (char). (copyint(a,&temp);
)
Your code is bugged.
temp = malloc(sizeof(char));
allocates one byte to temp.
copyint(a, &temp);
passes the ADDRESS of "temp". "temp" then gets overwritten so it no longer points to the allocated memory. Hence it cannot be freed.
Secondly, the first parameter to copyint is a char
but you are passing a char *
. Lastly, what on earth are you doing with atoi()
?
I think you need to find out what copyint()
actually does. What are you trying to do anyway?
of
You are allocationg memory for 1 char, but copying 4 bytes to that pointer in copyint function.
Even if the question is not so clear, I believe that including malloc.h will solve the problem, at least if you're using a VC compiler.
HTH
精彩评论