• ### Announcements

#### Archived

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

# Array variables: What are they? C++ question

## Recommended Posts

I''ve had this question in my mind for a while, and I thought I''d ask the community(or whatever your politically correct term is ). In C++ and C primer books, static arrays were always to be created through the following syntax:  TYPE variable_name[indice count];  What kind of variable would variable_name be? I''m pretty sure that I can treat it as a pointer, but I want to be sure. It''s sad that I''ve been programming for 5 years (C++), and still I am not completely comfortable with static arrays. Any help would be greatly appreciated! Thanks. "The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum -Brent Robinson

##### Share on other sites
In your example, variable_name is of type TYPE*[index count]. However, it can be treated as if it were a TYPE* (but it is not exactly a pointer variable).

Edited by - merlin9x9 on July 30, 2001 3:56:44 PM

##### Share on other sites
quote:
Original post by merlin9x9
In your example, variable_name is of type TYPE*[index count] . However, it can be treated as if it were a TYPE* (but it is not exactly a pointer variable).

No, it''s of type "TYPE * const", "TYPE *[]" is a pointer to a pointer, which this is clearly not. You can do anything with the variable that you can do with normal constant pointers (that is, you can do anything at all, except assign it to a different address).

War Worlds - A 3D Real-Time Strategy game in development.

##### Share on other sites
Yip - treat it like a pointer,
variable_name is the same as
*(variable_name + i)

Which is the nicer syntax?
As a side note, if you malloc some memory you can treat the pointer to the memory as though it was an array, cool.

##### Share on other sites
Whoops! I accidentally put that asterisk in there. =)

Statically-allocated arrays are not pointer variables, therefore there are limits. For example, let's say you've just declared an array called p. What's the result of &p? It's nonsense because p is not a variable. But if p had been declared as, say, an int*, taking the address of p would be perfectly legal because it's a pointer variable.

So, although statically-allocated arrays behave as though they're pointers, they're not, and because of that, you can't perform all of the same operations.

Dynamically-allocated are a different beast because you access them through a pointer variable.

Edited by - merlin9x9 on July 30, 2001 11:35:03 PM

##### Share on other sites
Thanks all for your help! I''m pretty sure I have it now, but only practice will tell.

"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum

-Brent Robinson