LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C Question: arrays and pointers (http://www.linuxquestions.org/questions/programming-9/c-question-arrays-and-pointers-24916/)

tundra 07-03-2002 01:22 AM

C Question: arrays and pointers
 
apparently i read somewhere that arrays and pointers amount to the same thing eventually.

so why do we have to allocate/free memory for pointers but not for arrays?

acid_kewpie 07-03-2002 03:41 AM

because if you have an int[10] array then obviously it's size is known, but a pointer is more flexible and as such all you define for a pointer is a starting place, and so you'd then need to give the program some idea of how long that memory space is meant to be.

Mara 07-03-2002 09:47 AM

In fact, dynamic or static variable, it's nearly the same. It's all about allocation (maybe a bit different, but still..).
A pointer is an address in memory in fact. But when ou use a normal variable, compiler changes its nice name into an adress...

tundra 07-04-2002 03:11 AM

kewp: yes, come to think of it, that makes sense...

mara: i don't get you. care to elaborate(as in how it relates to my question)?

Mara 07-04-2002 02:51 PM

I'd try..
When you use a pointer, you get a place in memory enough to keep an address. Then you use a function to assign space. One of the parameres will be the size you need. After using it, you have to free the memory (AFAIK in Java you don't have to, am I right?).
Then an array. There's also allocation, but the size is taken directly from your code (during compilation, when running, you can't change it). After use, you don't have to free it (because of the defaults).
So, pointers and arrays - it's the same. When processing an array, your compiler uses some defaults, that's the only difference.

tundra 07-04-2002 10:40 PM

i see what you mean now. thanks.

edreddy 07-19-2002 01:00 AM

tundra,
using the combination of pointers and memory allocations & free, you can achieve the concept of dynamic arrays & stretchable arrays, where as using arrays in 'C' you can implement only static arrays. ofcourse with pointers execution time is high, where as with arrays we can realise superfast programs.
so ,
if you want super compact programs, go for pointers.
if you want super fast programs , go for arrays.
(with arrays address binding takes place at compilation time, where as with pointers address binding takes place at run time.)
there exactly lies the difference between the pointers and arrays.

tundra 07-19-2002 03:38 AM

wow that was more specific. thanks reddy


All times are GMT -5. The time now is 05:26 AM.