LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2002, 01:22 AM   #1
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
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?
 
Old 07-03-2002, 03:41 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,386

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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.
 
Old 07-03-2002, 09:47 AM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
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...
 
Old 07-04-2002, 03:11 AM   #4
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Original Poster
Rep: Reputation: 31
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)?

Last edited by tundra; 07-04-2002 at 03:13 AM.
 
Old 07-04-2002, 02:51 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
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.
 
Old 07-04-2002, 10:40 PM   #6
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Original Poster
Rep: Reputation: 31
i see what you mean now. thanks.
 
Old 07-19-2002, 01:00 AM   #7
edreddy
Member
 
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34

Rep: Reputation: 15
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.

Last edited by edreddy; 07-19-2002 at 04:32 AM.
 
Old 07-19-2002, 03:38 AM   #8
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Original Poster
Rep: Reputation: 31
wow that was more specific. thanks reddy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 11:43 AM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 10:47 AM


All times are GMT -5. The time now is 11:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration