• Advertisement
Sign in to follow this  

Redeclaring an array

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there anyway in C++ for me to redeclare an array? I want the array to have (x*y)+1 elements but the thing is being declared in a class file privately and so won't let me declare it like that. So is there anyway to declare the array as 10 and then change it at a later point to (x*y)+1?

Share this post


Link to post
Share on other sites
Advertisement
In C++ array types have a fixed size. You want to consider using a std::vector instead of a normal array.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
any header files needed for that? <apvector.h> isn't it?


#include <vector>

note the lack of a file extension

the vector is a template type in namespace std.

std::vector<int> myVector;

Share this post


Link to post
Share on other sites
std::vector<int> maparray[length];

is that the correct initialisation (sorry I haven't used vectors before)

Share this post


Link to post
Share on other sites
If you declare it as a local variable:

std::vector<int> maparray(length);

As a member variable it's just

std::vector<int> maparray;

You can change the size of the vector with the .resize() member function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
std::vector<int> maparray[length];



is that the correct initialisation (sorry I haven't used vectors before)


almost...
std::vector<int> maparray(length);

this will contain length ints.

use this if you want to start with them zeroised

std::vector<int> maparray(length,0);


note that the vector is a dynamic array. it will ensure it has enough room for anything you try to do.

eg.



vector<int> vec;

// note that even though we dont specify the length, the vector will ensure there is enough
for( int i = 0 ; i < 10 ; ++i )
vec.push_back(i);// adds the number to the back of the vector

for( int i = 0 ; i < vec.size() ; ++i )
std::cout << vec << '\n';



Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
use this if you want to start with them zeroised

std::vector<int> maparray(length,0);

The 0 is unncessary. When you specify an initial size with the constructor, it automatically zeros the vector elements when the vector is of int type.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by rip-off
use this if you want to start with them zeroised

std::vector<int> maparray(length,0);

The 0 is unncessary. When you specify an initial size with the constructor, it automatically zeros the vector elements when the vector is of int type.


sweet.
specialised templates i assume.
didnt know that.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
sweet.
specialised templates i assume.
didnt know that.


No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());

The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());

The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.


suppose it is my fault for assuming stuff.

i assumed that because:

int i;

doesn't guarentee that i is any value, that int() wouldn't either( not that i've often thought about how "int()" would work )

thanks for the info

Share this post


Link to post
Share on other sites
Yeah. Weird, isn't it?


int main() {
int i; // not initialized to anything
int j = 0; // initialized to 0
int k(0); // initialized to 0
int l = int(); // initialized to 0
int m(); // not an int, but instead a function declaration :)
m();
}

int m() {
return 42;
}

Share this post


Link to post
Share on other sites
And it gets better:


float f;
int i(f); // Warning: converting from float to int
int j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*
int k((int)f); // Ok
int m(int()); // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
And it gets better:


float f;
int i(f); // Warning: converting from float to int
int j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*
int k((int)f); // Ok
int m(int()); // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...


That's not... oh, you mean parentheses around the parameter name in a function declaration. I guess that's indeed what's going on there - it couldn't be declaring a function that accepts a function pointer, because that would be int j(int(*)(f)). :S Yeah, a serious annoyance, it is. Any word on whether 0x will clean this mess up?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
That's not... oh, you mean parentheses around the parameter name in a function declaration.


It seems to work in any variable declaration, not just within function argument lists.

Quote:
Any word on whether 0x will clean this mess up?


There certainly exist proposals to that end, the last word in the matter is this paper by Stroustrup and Dos Reis. Basically, they seek to generalize the use of {} initializers and also enable constructing user-defined types via initializer lists. The proposal seems very reasonable and probably has quite a good chance to be included at least in some form.

[Edited by - Sharlin on January 20, 2006 8:38:28 PM]

Share this post


Link to post
Share on other sites
The C++ way has been mentioned.

In C (or if you really want to just use a plain array) you can do it like so:

to initialize
char *array = (char*)malloc(sizeof(char)*x);

to make bigger
x++;
array = (char*)realloc(sizeof(char)*x);


Let me say right now that if I catch you doing this in C++ (without a good reason) I'll break your kneecaps, but it is important to know how to do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement