PDA

View Full Version : pointers in c


kenna
06-03-2007, 05:37 AM
Hi, newb programmer here! How do I use pointers to make an array access only a certain range of elements within another array?


* foo is an array with 8 elements
* bar is an array with 4 elements, and points to foo[4]
* thus:
bar[0] = foo[4]
bar[1] = foo[5]
bar[2] = foo[6]
bar[4] = foo[7]


Pointers kind of make my brain go "duuuuuuuuuuuuuh...." ^^

Wernaeh
06-03-2007, 06:55 AM
Heyho there :)

Well actually, there is no way to make C pointers just access a "range" of elements within another array.

It is, however, possible to use a pointer to change to a different starting index within the array.

i.e.


// Array and associated pointer
int myarray[8];
int *myinteriorpointer;

// Now, have the pointer point to the 4th (zero based!) array element
myinteriorpointer = &myarray[3]; // Is equivalent to = myarray + 3;

// Now the following line pairs are semantically equivalent
myarray[3] = 100;
myinteriorpointer[0] = 100;

myarray[5] = 1043;
myinteriorpointer[2] = 1043;

// You still can exceed the array "range" ! Both of these (should) crash.
myarray[10] = 500;
myinteriorpoiter[7] = 500;


Generally, a good way to think of pointers is as memory cells (little blocks in main memory), that contain the index of another memory cell relative to main memory.

Thus, the above statements first declare an array, which is located at some place within main memory. Then, the given pointer is set to the index of the memory cell of the fourth array element (i.e. the pointer now contains where in main memory the fourth array element is).
This index may, for example, be 0x3f4f958a (hexadecimal notation is more common for pointers than decimal notation). By now accessing the pointer with [0] or alternatively with (*pointer) , the compiler takes the pointer (that is, the index 0x3f4f958a) and returns the int that is at main memory at that index.
By accessing the pointer with [offset], the compiler takes the pointer, and returns the int that is at main memory <offset> integers from the actual target pointed to.
Thus, pointers essentially work just like arrays, and vice versa, arrays work like pointers. A pointer identifier (myinteriorpointer) can be used just the same way as an array identifier (myarray).

Hope this helps,

Cheers,
- Wern

poita
06-04-2007, 04:03 AM
As Wernaeh pointed out you cannot simply have an array that is synonymous to a range in a different array because you cannot have arrays of references.

Alternatively, you could have an array of pointers:

int *bar[4];
int foo[8];

And assign each element of bar to an element of foo:

bar[0] = &foo[4];
bar[1] = &foo[5];
bar[2] = &foo[6];
bar[3] = &foo[7];

Then, by dereferencing an element of bar, you can change elements of foo:

*bar[1] = 42; // Equivalent to foo[5] = 42;

Hope that helps.

.oisyn
06-04-2007, 06:59 AM
This is ugly and evil so I probably should not tell you this, but...

As Wernaeh pointed out you cannot simply have an array that is synonymous to a range in a different array because you cannot have arrays of references.

You can, however, have a reference to an array

int foo[8];
int (&bar)[4] = reinterpret_cast<int(&)[4]>(foo[4]);

Nevertheless, I would go for int * bar = foo + 4; anyway

.edit: oops, this is a C thread, not a C++ thread. Disregard this post except for the last line.