8 Replies - 1107 Views - Last Post: 09 December 2009 - 02:08 AM Rate Topic: -----

#1 k8Le3n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 25-November 09

arrays and pointers

Post icon  Posted 08 December 2009 - 10:45 PM

whats the relationship between Arrays and Pointers?
im a little bit confused.
Is This A Good Question/Topic? 0
  • +

Replies To: arrays and pointers

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#3 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

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.

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

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: arrays and pointers

Posted 08 December 2009 - 11:18 PM

Quote

int pt* = 1;


what is that?
Was This Post Helpful? 0
  • +
  • -

#5 Spencer William  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 14-June 09

Re: arrays and pointers

Posted 08 December 2009 - 11:28 PM

View PostNickDMax, on 8 Dec, 2009 - 10:18 PM, said:

Quote

int pt* = 1;


what is that?


Looks like a typo. :)
Was This Post Helpful? 0
  • +
  • -

#6 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: arrays and pointers

Posted 08 December 2009 - 11:30 PM

lol, sure is.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: arrays and pointers

Posted 08 December 2009 - 11:34 PM

Well if we assume that

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!!! :P
Was This Post Helpful? 0
  • +
  • -

#8 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

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 :P

This post has been edited by SpeedisaVirus: 08 December 2009 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

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/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1