• 0

[C++] Creating an array of variable length


Question

hi all, i'm really new to C++, and i want to be able to create an array of a variable length. for example:

int myarray;

i know this cannot be done as i've typed above, but i've heard there is another way to do it. does anyone know how? it's crucial to my project.

also, i know that strings are just arrays, and i want to make a string of variable length, with an underscore for each place.(hangman game)

i wrote this loop, but the compiler won't accept it. why?

for(int i=0; i<len; i++)
{
word[i]='_';
}

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

Yes, you need to use a pointer to an int and a malloc, like this:

// This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1
int* myarray;

[...] // code here until it's time to declare

// This declares the number of bytes you want to use, thats why we multiply the size you want by the number of bytes used for each int
myarray = malloc(sizeof(int) * size);

[...] // code here until you're done with it

// Don't forget to free the memory.
free(myarray);

Hope this helps :)

Link to comment
Share on other sites

  • 0
Yes, you need to use a pointer to an int and a malloc, like this:

int* myarray;

[...] // code here until it's time to declare

myarray = malloc(sizeof(int) * size); // This declares the number of bytes you want to use, thats why we multiply the size you want by the number of bytes used for each int

[...] // code here until you're done with it

free(myarray); // Don't forget to free the memory.

that looks good, but what's a malloc? also, i heard something about the "new" command, which is supposed to be C++ specific. is that any better?

Link to comment
Share on other sites

  • 0

As for your other snippet, what compiler are you using?

You might need to declare all of your variables at the top of each function so you'd:

// declare i
int i;

[...]

// Then use it
for(i = 0; i < len; i++)
{
	word[i] = '_';
}

Also, how did you declare word? and where/how are you getting len?

Link to comment
Share on other sites

  • 0
that looks good, but what's a malloc? also, i heard something about the "new" command, which is supposed to be C++ specific. is that any better?

Oh ok, C++ specific you can use new, it's a bit easier to use and can be used to declare objects as well. I'm really not that familiar with the differences between the two for arrays.

// This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1
int* myarray;

[...] // code here until it's time to declare

// This declares the array of size size
myarray = new int [size];

[...] // code here until you're done with it

// Don't forget to free the memory.
delete[] myarray;

Link to comment
Share on other sites

  • 0
Oh ok, C++ specific you can use new, it's a bit easier to use and can be used to declare objects as well. I'm really not that familiar with the differences between the two for arrays.

// This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1
int* myarray;

[...] // code here until it's time to declare

// This declares the array of size size
myarray = new int [size];

[...] // code here until you're done with it

// Don't forget to free the memory.
delete[] myarray;

ok, i think i figured it out, your code helped a lot! thank you, sir!

Link to comment
Share on other sites

  • 0

ok, now that i've figured that out, i've run into another hitch. i know how to replace a character in a string with another character, but how do i replace multiple occurrences of that character within the same string?

Link to comment
Share on other sites

  • 0
ok, now that i've figured that out, i've run into another hitch. i know how to replace a character in a string with another character, but how do i replace multiple occurrences of that character within the same string?

Loop through the string and check each instance, if it matches what you want to replace then replace it.

Link to comment
Share on other sites

  • 0
A vector is way to a complex of a solution for such a simple problem.
The implementation of vector is complex, but using it? You just need to declare it, and you're done. One line of code and problem solved.
Link to comment
Share on other sites

  • 0
A vector is way to a complex of a solution for such a simple problem.

I had a class with Dr. Stroustrup teaching it (creator of C++). Even he advocated use of std::vector whenever possible over pointer arrays or arrays period.

There are very few cases of where using an array or pointer array is better than using a vector as vectors offer similar performance to arrays and are much easier to use.

Edited by Xilo
Link to comment
Share on other sites

  • 0
Yeah, but a vector class for a string? Why not just use a string class?
True. vector is the general alternative to arrays, string is better specifically for character arrays.

Wonder how I missed that. :blush:

Link to comment
Share on other sites

  • 0

yeah i just used a for() loop to find and replace some stuff after converting the string to an array. thanks for the suggestions, though, i think they'll help me out later on.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.