|
-
Feb 20th, 2001, 11:52 PM
#1
Thread Starter
Addicted Member
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
-
Feb 21st, 2001, 12:06 AM
#2
Lively Member
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.
-
Feb 21st, 2001, 12:09 AM
#3
Thread Starter
Addicted Member
well...
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?
-
Feb 21st, 2001, 12:31 AM
#4
Lively Member
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.
-
Feb 21st, 2001, 05:56 AM
#5
Frenzied Member
Try this:
Code:
int howmany;
cin>>howmany;
int *names= new int[howmany];
//then
names[0] = 23423;
names[1] = ..;
..
-
Feb 21st, 2001, 09:51 AM
#6
Lively Member
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......
-
Feb 21st, 2001, 01:51 PM
#7
Lively Member
-
Feb 21st, 2001, 03:12 PM
#8
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 07:37 AM
#9
Monday Morning Lunatic
You don't usually need to do that, since it handles reallocation internally:
Code:
vector<int> viMyArray;
viMyArray.push_back(5);
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 11:08 AM
#10
Frenzied Member
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>
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Feb 23rd, 2001, 01:18 PM
#11
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 23rd, 2001, 01:29 PM
#12
Frenzied Member
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.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Feb 23rd, 2001, 01:41 PM
#13
Frenzied Member
Oh and dont make me get my pillow case of oranges!
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|