Why doesn't that work ??? ... (the red term)
... and itoc is defined :Code:{ ... int ierg = <s.th.>; char *zkerg; zkerg = itoc(ierg); ... }
Code:char* itoc(int zahl) { char *temp; _itoa(zahl, temp, 10); return temp; }
Why doesn't that work ??? ... (the red term)
... and itoc is defined :Code:{ ... int ierg = <s.th.>; char *zkerg; zkerg = itoc(ierg); ... }
Code:char* itoc(int zahl) { char *temp; _itoa(zahl, temp, 10); return temp; }
You'll have to allocate memory, either on the stack or heap, to store the string pointed to by char *temp. If you use stack memory then it will go out of scope if it's created in the function and this function returns (you wont be able to access it after the function's returned).
zen
OK!
So far so good ... now i wrote
but when should i delete temp ?Code:char *temp = new char; _itoa(zahl, temp, 10); return temp;
after return ?
You should call delete before the return to the main function. As the *temp is a local variable, it will be out of scope after the return and you will not be able to release the memory
You may have to allocate more room if the number you're converting is longer than one digit and need include space for the terminating NULL, eg. for 111 you'd need to do char *temp = new char[4]. It may be easier just to use an array on he stack and pass it into the function -
char temp[10];
int number = 123;
//You'd have to change the prototype of itoc to take a char* as
//a parameter.
itoc(number,temp);
Then you wouldn't have to allocate or deallocate the memory.
zen