How to fill a dynamically allocated 2D array? for example:
#define n 100
char **array;
array = new char*[n];
for(int i=0; i<n; i++)
{
array[i] = new char[n];
}
How to fill this 2D array with * value?
How to fill a dynamically allocated 2D array? for example:
#define n 100
char **array;
array = new char*[n];
for(int i=0; i<n; i++)
{
array[i] = new char[n];
}
How to fill this 2D array with * value?
array[i] is an n length character array.'*' to its j-th entry all you have to do is:for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
array[i][j] = '*';
}
}
'a' is not '*'as expected by the OP, and why not using memset ?Both the answers you got so far are correct. You can see it as an 2D array or as an array of strings. You can store data on it char by char or directly writing a string. Maybe using constants instead of define would be a better practise.
const int n = 100;
char **array;
array = (char **)malloc(sizeof(char *) * n);
bool storeString = true;
for(int i=0; i<n; i++)
{
array[i] = (char *)malloc(sizeof(char) * n);
if(storeString)
array[i] = "Im striiinging in the rain";
else
for(int j = 0; j < n-1; j++)
{
array[i][j] = '0';
}
}
I'm using C type memory allocation but that shouldn't be an issue, I just don't remember how to do it precisely on C++ type, just import <cstdlib>