LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-27-2010, 08:46 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Why are arrays in C pointers?


I wonder why arrays in the C programming language are pointers to the first element of the array, not the first element of the array itself?
 
Old 03-27-2010, 08:51 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Arrays in C are just consecutive allocated memory addresses, so all you need to access the array is a pointer to the first element, then you increment to get to the rest. And that's why it seg faults if you go over the number of allocated elements in the array (at least on Linux, on Window$ it probably crashes the system).

I'm not exactly sure what you are asking, but you must have a pointer (memory address) to the first element for the above stated reason, otherwise if it's just the value at a memory address that won't do you any good, because you can't access the rest of the array using just this.

Last edited by H_TeXMeX_H; 03-27-2010 at 08:54 AM.
 
Old 03-27-2010, 08:54 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Why is it a pointer to the first element?

Why is *(array+1) the 2nd element, not *((&array)+1)?
 
Old 03-27-2010, 12:59 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Addressing an array by its first (zeroth?) element is merely a convenient shorthand provided by the language. If you want to be completely pedantic, you could always specify an array as
Code:
  &(array[0])
but this seems less readable. It is a great construct of the language, but is a significant stumbling block for those learning the language, especially for someone unacquainted with lower level machine architecture and principles that are learned in the likes of assembly language programming.
---- rod.
 
1 members found this post helpful.
Old 03-27-2010, 01:06 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Not sure if this will help, but I know it was useful to me (which is why I bookmarked it a while ago): http://c-faq.com/aryptr/index.html.
 
1 members found this post helpful.
Old 03-27-2010, 01:08 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
You still don't understand my question.

Why is it like this:

Code:
Stack:

int a
int* array --\
char c       |
...          |
             |
Heap:        |
...          |
element 0 <--/
element 1
element 2
...
Not like this:

Code:
Stack:

int a
int array (element 0)
int (element 1 of array)
int (element 2 of array)
char c
And why does the array[n] operator do this:

Code:
*(array+n)
instead of this:

Code:
*((&array)+n)

Last edited by MTK358; 03-27-2010 at 01:11 PM.
 
Old 03-27-2010, 01:11 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
If you allocate an array dynamically, then it is all on the heap (this is the purpose of the heap). If you allocate the array statically, then it is all on the stack. This is true, of course, unless you find a reliable source saying otherwise. This is how I've always understood it.
 
Old 03-27-2010, 01:16 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by MTK358 View Post
And why does the array[n] operator do this:

Code:
*(array+n)
instead of this:

Code:
*((&array)+n)
Because "array" is already a pointer, you don't care where its stored ("&array"), you only care what it points to. Say "array" points to address 5. The second element is then the value pointed to by address 5+2=7, or in other words "*(array+2)".

Note: Some things above are stated for simplicity, versus correctness.
 
Old 03-27-2010, 01:20 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by nadroj View Post
Because "array" is already a pointer, you don't care where its stored ("&array"), you only care what it points to. Say "array" points to address 5. The second element is then the value pointed to by address 5+2=7, or in other words "*(array+2)".

Note: Some things above are stated for simplicity, versus correctness.
What if it wasn't a pointer? What if arrays in C were plain variables?

I am not talking about C programming techniques, but why is the C language designed this way in the first place?
 
Old 03-27-2010, 01:31 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Window$ it probably crashes the system
No, it does not.
Instead spits out an error that does not pertain to a SEGFAULT: xxx.exe has stoped working.
 
Old 03-27-2010, 01:31 PM   #11
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
I dont know the specification details or the reasoning for the design decisions made in the language. I imagine they aren't "plain variables" because then you don't know where the next element is. Arrays elements are, of course, sequential in memory. This makes working with the elements ("array arithmetic") very easy. If arrays were "plain variables", then basically for an array of size n, you have n different variables, say
Code:
int i0;
int i1;
// ...
If you say "well, no, it would just be one variable", then your basically saying/understanding why it should be the way it is now, with one variable (ie, "int array[n];"). It is a single variable, which is just the first element in the array. The added benefit is that you can determine the addresses of the other elements in the array with simple math.

I can't really explain what I'm trying to say here, well. What I'm mainly trying to say that its like this because it actually simplifies things. I imagine it might also make some underlying memory management stuff relatively simpler. For example, when you request an array of 5 ints, your requesting, say 5x4 = 20 bytes. The OS then just has to look for a space of 20 continuous bytes of free memory, which is basically a constant time calculation (O(1)). This is opposed to a linear time calculation of O(n). Of course there are other good reasons, I imagine.
 
Old 03-27-2010, 01:36 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think I know why an erray is a pointer now.

It's probably because not all CPU architectures pass function parameters sequentially on the stack, eliminating the ability of passing an array to a function.

But OTOH, why not manually make a pointer of the array and pass it to the function?

(Unless some CPU architectures don't even store stack variables sequentially).

But again, maybe C's design makes it easier to implement dynamic arrays?

Last edited by MTK358; 03-27-2010 at 01:38 PM.
 
Old 03-27-2010, 01:51 PM   #13
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
I don't understand what the CPU architecture has to do with it. Also, I think all function parameters are always "passed on the stack". When a function is called, the instruction after the instruction to call this function is pushed on the stack. Next, all the parameters to the function are pushed on the stack. Then control enters the function, and the parameters are popped off the stack, and the top of the stack is now the address of the instruction to execute after this function is done. Next the function executes and returns (control). The OS knows where to return control to, because that address at the top of the stack.

("entire") Arrays are never passed to functions. As stated many times here, an "array" variable is just a variable storing the address of the first element in the array. This is the variable that's passed to any function, not the "entire array" itself, as that is horribly inefficient.

The link I posted above wasn't for show. You certainly could not have read it all between the time I posted it and the time you said I don't understand what your asking. I posted it because it is very informative, and if you go through it all, you will probably get a better understanding.
 
Old 03-27-2010, 01:55 PM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I thought that x86_64 and ARM among others pass some parameters in registers and some on the stack based on certain rules.
 
Old 03-27-2010, 02:07 PM   #15
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
I dont know anything about specific architectures. If you are doing ASM programming, I know that system calls require you to manually put variables in certain registers before you call the function. In user-defined functions I think you push them on the stack. But I dont have enough experience in that to give any facts.

But I still dont think that the decision is based solely on CPU architectures, but that it was done for simplicity and ease of use/maintenance. I think there are two major paths to go from here, to find what your looking to understand. Theres the low-level answer/reasons, involving ASM and hardware, and the higher-level annswer/reasons, involving languages like C.
 
  


Reply



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
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration