how do you redim an array in c++?
how do you change its size?
i mean, if you have:
int names[10];
can you change the '10' later on in your code to whatever you want?
please answer :(
Printable View
how do you redim an array in c++?
how do you change its size?
i mean, if you have:
int names[10];
can you change the '10' later on in your code to whatever you want?
please answer :(
You can't redim an array like in VB. If you wish to dynamically allocate a array, you will need to use pointers and the malloc() function. Not too sure if malloc() is still used in C++ though.
well...
so there is FOR sure no way around this at all?
also...
why can't you do this
cin>>howmany;
int names[howmany];
that is basically what i need. i need the user to specify instead of me doing that.
any way around this at all?
No, you can't do allocation of arrays in any form without using pointers.
Before starting, do you have knowledge of pointers in the first place? Or else you will have sometime understanding the codes.
Try this:
Code:int howmany;
cin>>howmany;
int *names= new int[howmany];
//then
names[0] = 23423;
names[1] = ..;
..
Do remember to use the "delete" keyword to remove the array from the heap after finish using it :)
delete [] names;
If I'm not wrong with the syntax......
Hey guys, ever thought of using a linked-list? Do you think it is possible?
Linked lists are useful, but if you're only occasionally reallocating then their overhead is too high. However, if inserting into the middle of the list is frequently needed, then they're great. Check out the STL list template, or the slist template if you only need forward traversal of the list.
You don't usually need to do that, since it handles reallocation internally:
Code:vector<int> viMyArray;
viMyArray.push_back(5);
Maybe some on could spend the time and make a dll, header file, or class to do redim, ubound, etc easily.
<All eyes turn to look at parksie>
*menacing*
You talkin' ta me?
*growls*
UBound is nasty to do in C++ since you don't know which dimension you're using. You need to divide the dimension size by the size of it's parent (I think...this may be the wrong way around!)
LBound is easy:
:p:p:p:p:p:p:p:pCode:inline long LBound(void *pArray) { return 0; }
Hehehehehe
ReDim, well, normally you'd just delete and reallocate. Preserve is a little more complicated, but dead simple. Unfortunately they're pretty much specific to the situation...templates anyone?
*looks at Technocrat*
Double Dare time :D
ooooooooohhhhhhhhhh, rumble in the C++ jungle. ;)
I would try it if I had time. In fact I might when I get done with my current project. It would be nice to have those types of abilities that were built into arrays in VB. Redim, ubound, lbound were so handy.
Oh and dont make me get my pillow case of oranges!
:)