Archived

This topic is now archived and is closed to further replies.

Zoomby

arrays on the stack

Recommended Posts

Zoomby    122
hi Imagine a local array: int array[5]; //array++; //not possible, no pointer. int* pArray=array; pArray++ //now it works Is it possible to save the pointer declaration step, and directly get a pointer, so that the second line of code works? (only local arrays, no arrays on the heap!) bye chris

Share this post


Link to post
Share on other sites
Zipster    2365
An array isn't a pointer, even though it tends to behave like one, so you can't increment it or anything. If you want the address of a certain element, it's &array[i], where i is the element. Put that in a loop and you won't need an extra pointer.

Why, by the way, do you need to do this? What are you trying to do?

[edited by - Zipster on April 30, 2003 3:58:03 AM]

Share this post


Link to post
Share on other sites
Zoomby    122
I just wanted to know if it''s possible to directly get a pointer when making a array declaration on the stack (when creating arrays on the heap you get the pointer directly). Sometimes you need just an array-pointer (to first element) and not the "array-variable" itself.

bye
chris

Share this post


Link to post
Share on other sites
Sneftel    1788
No, it isn't. You have to manually declare the pointer, since in doing so you're telling the compiler to allocate you a little more space to put the pointer in (an array doesn't need a pointer to tell it where it is; the compiler knows).

EDIT: other points:

1. Even if you don't plan to use the array for anything, it's still useful to have it named, for instance to look through in the debugger.
2. the variables, in a well-constructed program, should not be called "array" and "pArray". "pArray" is most likely not being used as a pointer to an array, but rather as an iterator through that array. if it were me, the code would look something like this:

int heights[5];
int* ih = &heights[0];

calling it "ih" is a good compromise between tending to write it a lot (since it's most often a loop variable) so wanting it short, and wanting to keep in mind what it's actually describing. Whether I'd actually do the &..[0] thing varies based on my mood at the time.

How appropriate. You fight like a cow.

[edited by - sneftel on April 30, 2003 4:38:21 AM]

Share this post


Link to post
Share on other sites
dagarach    169
I think he called it "array" because that is a full and thorough description of what it is. It is called "array" precisely to escape from the distractions of describing it in context. This is much like a beginner''s book teling you how to declare variables:

SomeType SomeVariable;

It isn''t suggesting that you call your classes "SomeType" or your variables "SomeVariable"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Sneftel

int heights[5];
int* ih = &heights[0];




ih = &heights[0] is the same as ih = heights and it''s not true that you can''t increment pointers! You can do ih++ and it will point on the second element in the array!

Share this post


Link to post
Share on other sites
Sneftel    1788
quote:
Original post by Anonymous Poster
ih = &heights[0] is the same as ih = heights

Which is why I said that the only difference is how I was feeling. But &heights[0] makes it clear that you intend to point to a particular element, not the array as a whole. Oftentimes, what is important is not only what your code does, but what it looks like. I''m assuming you''ve never coded in a large team environment. Eventually, you will learn why idioms such as this are useful.

quote:
and it''s not true that you can''t increment pointers!

Errrrrrrrr...... huh? How did you take my post to mean that you couldn''t?


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites