can a pointer represent a whole array?

the tittle says it all. but if you believe that a pointer can not or can be representing a whole array cell then please tell me why and the logic behind it
Define "represent".
Define "whole array cell".
"represent" as in can you set a pointer to have the adress data of a array
"whole array cell" as in the array's content's/ the data stored in the array
Last edited on
You can have a pointer to the first element of an array (or any other element).
Using this pointer, you can also reach every other element in the array via pointer arithmetic.
can you give me a example of this array arithmetic?
Sure. :)

1
2
3
4
5
6
7
int* p = new int[2]; //Allocate an array of 2 ints.
*p = 0; //Change first element of the array;
++p; //Shift p one step up.
*p = 1; //Now it changes the second element of the array!
--p; //Shift p one step back.
std::cout << *p; //This will output 0, not 1. Do you see why?
delete[] p; //Mandatory safety measure to be used whenever one uses 'new'. 


Pointer arithmetic and pure dynamic allocation (new/delete) are classified as "evil" practices in C++, so be careful when using them.

-Albatross
Last edited on
wow, my mind just exploded because i am having a difficult time understanding the way this work's.but let's see if i am interpreting this the right way .

First you create a pointer that goes by the varriable p and that pointer creates a new integer array that has 5 element's/data/memory slot's.

then you deference the pointer to hold a value of 0 in the "first" array element/data/memory .

then by using p[0] your stating that p is now in the first array element again(where we stored the 0).

after that you use ++p to add one to where p has the adress so there for now we are in p[1].

then you dereference the pointer and give it a value of 1.

and after all that you do --p to bring the pointer's , pointing adress down to p[0] and you print it to the console?. . . please tell me I was atleast somewhat correct and if so then explain why on line 3 we had to declare that we where in p[0]? why was it nessecary because to my understanding we where already in p[0](when we assigned 0 to the array spot 0)?..
Last edited on
Step 1: I changed the array size to 2 after you started writing your post, but... details. :)

Step 2: Right.

Step 3: p[0] is much like a normal deference operator. The only difference is that I believe it has a higher precedence. It should not move the pointer around. p[1] is like *(p+1) except with a higher priority. That line p[0] basically has no effect.

Step 4: ...sort-of, yes.

Step 5: Right!

Step 6 & 7: Yes and yes.

That is, assuming I drank enough tea...

-Albatross
well my brain still hurts and im kind of confused but I hope it goes away with experimenting
any ways thanks for the help , your very nice to share your own knowledge :)
While it's OK for illustrative purposes, I would avoid moving a pointer that I used to allocate memory. Instead I'd set another pointer to the same address and use that. So I don't accidentally (in more complicated code) do:

1
2
3
4
5
6
7
int* p = new int[2];
*p = 0;
++p;
*p = 1;
// forget to move back to beginning
std::cout << *p;
delete[] p; // will blow up as p no longer points to the beginning of the allocated block 


Andy

P.S. delete isn't really a "safety measure". It's just that if you don't call delete, and access to the allocated memory is lost (when the pointer goes out of scope), then the memory is still there somewhere, but unavailable for reuse. i.e. it's leaked.
Last edited on
@Factors! Dont worry about pointers and reference operations. Its widely considered one of the most abstract parts of programming / difficult for beginners to understand...

I would recommend reading different explanations of them that way you get to see all sides. Also for me i found Bjarnes Stroustrups explanation of pointers very nice and helpful in "Programming principles and practice using c++" if you really want i can scan it for you i guess, though im not sure about the rules about that kinda thing here on this forum.
I've found that the best explanation of pointers was in K&R. Since it's an old book, you can probably just download it as PDF. Make sure it's the second edition too, the explanation of pointers in the first one isn't particularly good.
@andy: I never recommend the use of pointer arithmetic with dynamically allocated arrays. In fact, I never do it myself, not without ensuring that I always have at least one pointer pointing to the beginning of the array.

Also, I called delete a "safety measure" because a) I didn't want to spend too much time explaining dynamic allocation and b) at the time, I honestly couldn't think of a better term. Now that I'm not as sleepy, I probably could have put that better.

@Factors: I think codeproject does a decent job of explaining the concepts behind pointers.
http://www.codeproject.com/KB/cpp/pointers.aspx

-Albatross
Topic archived. No new replies allowed.