Am having trouble getting my references to change the size values of an array.
The goal is to accept a user input and use that to assign a size to my array (with the eventual aim of being able to output shapes of a size input by a user, within an array)
Sorry if any of my lingo's off or I haven't provided enough info, newbie! I've attached the entirety of my code to hopefully minimize confusion!
Just gonna start by pointing out some lines that need deleted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
char display [1][1]; // not sure why this exists
{ // what's this for?
char* display [1][1] = newchar[size][size]; /* this line */
/* causes problems? */
for(int r = rowStart; r < rowMid; r++ )
{
for(int c = colStart; c < colEnd ; c++)
{
display[r][c] = '*';
}
colStart--;
colEnd++;
}
} // what's this for?
}
Now as for dynamically allocating a 2D array, it's possible but you should really use a vector if you know what those are. Here's how to allocate a 2D character array:
1 2 3
char** display = newchar*[size]; //declare a pointer to a char pointer
for (int i = 0; i < size; i++) //loop through this array
display[i] = newchar[size];//each index becomes a new char array
After that you can use display like a normal 2D array, like display[i][k] or whatever you want to do. Be sure to delete all the allocated memory when you're done though (you'll need to loop through the array again).