whats the relationship between Arrays and Pointers?
im a little bit confused.
arrays and pointers
Page 1 of 18 Replies - 769 Views - Last Post: 09 December 2009 - 02:08 AM
Replies To: arrays and pointers
#2
Re: arrays and pointers
Posted 08 December 2009 - 11:11 PM
Well first lets ask what is a pointer. a Pointer is basically an integer (a numeric data type) that holds the memory address of something else.
An array is a block of memory. In C/C++ an array variable is "basically" just a pointer to the first element in the array.
So when you write a line of code like:
int iArray[100];
The value of iArray is the address of a block of memory big enough to hold 100 integers. To access these elements you use a derefference operator [] -- iArray[5] which turns out to simply be an alias for the pointer syntax *(iArray+5)
It is important to note though, iArray looks like a pointer, it kind of acts like a pointer, it mostly behaves like a pointer -- but it is not actually a pointer. Because unlike a pointer, the compiler actually knows the SIZE of the block of memory pointed to! This can be important when for example creating a 2D array.
For the most part this does not make much difference to us but there are times when it is important (foe example double indirection).
You can use the array[n] operator with arrays or pointers (because it is the same as *(array+n) ) and you can allocate dynamic array using pointers, so the difference can be a little hard to tell unless the compiler is complaining that it can't convert one into the other for you.
An array is a block of memory. In C/C++ an array variable is "basically" just a pointer to the first element in the array.
So when you write a line of code like:
int iArray[100];
The value of iArray is the address of a block of memory big enough to hold 100 integers. To access these elements you use a derefference operator [] -- iArray[5] which turns out to simply be an alias for the pointer syntax *(iArray+5)
It is important to note though, iArray looks like a pointer, it kind of acts like a pointer, it mostly behaves like a pointer -- but it is not actually a pointer. Because unlike a pointer, the compiler actually knows the SIZE of the block of memory pointed to! This can be important when for example creating a 2D array.
For the most part this does not make much difference to us but there are times when it is important (foe example double indirection).
You can use the array[n] operator with arrays or pointers (because it is the same as *(array+n) ) and you can allocate dynamic array using pointers, so the difference can be a little hard to tell unless the compiler is complaining that it can't convert one into the other for you.
#3
Re: arrays and pointers
Posted 08 December 2009 - 11:14 PM
Well, there is a relation of sorts but they aren't exactly the same thing. I'm still new-ish to C++ where a lot of this comes into play but I will give it a go.
In most general senses it can be said that they are equivalent but certainly not the same. The equivalence comes from the idea that an array[] will typically decay to a pointer to the first element of that array. There are exceptions but not entirely important at the moment.
This is effectively an array in that you have a chain of elements that are contiguous in memory and more or less equivalent. It however is not an array.
I think another important difference would be that an array is declared and allotted to a certain block of memory and can't be resized or reallocated. A pointer is initialized but then must then be manually allocated a block of memory for its contained data. They can also be repointed to another object of appropriate type and have more general uses than an array.
I'm burnt out tired but here is a quote from Sun that sums things up nicely.
EDIT:: Doh, been beat.
In most general senses it can be said that they are equivalent but certainly not the same. The equivalence comes from the idea that an array[] will typically decay to a pointer to the first element of that array. There are exceptions but not entirely important at the moment.
This is effectively an array in that you have a chain of elements that are contiguous in memory and more or less equivalent. It however is not an array.
I think another important difference would be that an array is declared and allotted to a certain block of memory and can't be resized or reallocated. A pointer is initialized but then must then be manually allocated a block of memory for its contained data. They can also be repointed to another object of appropriate type and have more general uses than an array.
I'm burnt out tired but here is a quote from Sun that sums things up nicely.
Quote
The difference between pointers and arrays is that a pointer variable refers to a separate piece of storage that contains the integer address of some other storage. An array variable names the array storage itself, not the location of an integer that in turn contains the location of the array.
EDIT:: Doh, been beat.
This post has been edited by SpeedisaVirus: 08 December 2009 - 11:53 PM
#4
Re: arrays and pointers
Posted 08 December 2009 - 11:18 PM
Quote
int pt* = 1;
what is that?
#5
Re: arrays and pointers
Posted 08 December 2009 - 11:28 PM
#7
Re: arrays and pointers
Posted 08 December 2009 - 11:34 PM
Well if we assume that
then we still have a problem... that code will cause an access violation or, on an older 8086 system would surly crash something since it sets the interrupt service routines for int 0 and int 1 to indeterminate values...
Not the best example one could pull out of ones hat!!!
Quote
int *pt = 1; (*pt+1) = 2; (*pt+2) = 3;
then we still have a problem... that code will cause an access violation or, on an older 8086 system would surly crash something since it sets the interrupt service routines for int 0 and int 1 to indeterminate values...
Not the best example one could pull out of ones hat!!!
#8
Re: arrays and pointers
Posted 08 December 2009 - 11:53 PM
I'm pretty sure I have done that on our sparc system and on my home PC. Hrm...nope sure haven't. Damnit I'm just deleting my code. Teach me to post something I didn't check first
This post has been edited by SpeedisaVirus: 08 December 2009 - 11:53 PM
#9
Re: arrays and pointers
Posted 09 December 2009 - 02:08 AM
This page has some pretty concise explanations on the relationship between arrays and pointers
http://c-faq.com/aryptr/index.html
If you're using C++, have a look at containers and iterators; conceptually the relationship between these is exactly the same.
More good pointer links:
http://www.daweidesi...in/pointers.php
http://www.eternally...t_pointers.aspx
http://www.augustcou...torial/ptr.html
http://cslibrary.stanford.edu/104/
http://c-faq.com/aryptr/index.html
If you're using C++, have a look at containers and iterators; conceptually the relationship between these is exactly the same.
More good pointer links:
http://www.daweidesi...in/pointers.php
http://www.eternally...t_pointers.aspx
http://www.augustcou...torial/ptr.html
http://cslibrary.stanford.edu/104/
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|