Can i allocate memory to an array of it or char using malloc ? if yes, then why is it so ? And if no then also why ? I am confused a little bit. thanks in advance to james sir ;)

Edited 4 Years Ago by deceptikon: Changed the title to something meaningful

I_m_rude
Deleted Member

Yes, you can use malloc() to allocate a simulated array of int or char. What were you trying to do that prompted this question?

no, what i am tryong to ask is that if there is an array of

char s[100]={0};


s=malloc(sizeof(char)*20);

is it valid to do so ? But why is it valid ? because s is an array already.

is it valid to do so ?

No, it's not valid. Arrays are not pointers, they're arrays. You can use malloc() and a pointer to fake an array, but it's not natively an array. Likewise, in many cases an array will be converted into a pointer, but it's not natively a pointer.

but in initial post, u said u can so ? why ?

:'( sir, if i allocate memory to a pointer and make it an array , then is it now an array ? means it is just same like an array ?

if i allocate memory to a pointer and make it an array , then is it now an array ?

No, but it can largely be used to simulate an array.

what simulate means here ?

It's a fake. It acts like an array for the most part, but isn't an array. Have you considered looking up the definition of "simulate"? That's exactly what it means here.

Edited 4 Years Ago by deceptikon

Can you reply on my thread "request to daniweb members" ? it's a request. If you ill do, that will motivate others. thanks

This article has been dead for over six months. Start a new discussion instead.