Mail Archives: djgpp/1994/12/10/12:14:42
Hirling Endre wrote :
> 2. What's wrong with the following function?
> char* string(int n,char c)
> {
> char* ts;
> ts=(char*)malloc(n+1);
> memset(ts,c,n);
> ts[n]=0;
> return(ts);
> }
I think the wrong stuff here is the way the variable is declared.
If you want to return an allocated variable you should make it static.
Variables declared in functions are stored in the stack. So when the
function returns the space allocated in the stack is released.
Hope this helps.
frankie
- Raw text -