The code you provide is faster that the one that you assign test to every cell of the array? Are you sure? How to you measure the time?

An idea :

You said that you create an array inside the function and then copy by value to the charstate array.Why not doing these operation in one double for loop instead of two?

Example (in C code but it will be ok to understand )

Going through the loop twice and a temp array

Code:

#include <stdio.h>
void foo(int a[][5],int n)
{
int i,j;
int temp[n][n];
for( i = 0 ; i < n ; i++ )
for(j = 0 ; j < n ; j ++)
temp[i][j] = 15;
for( i = 0 ; i < n ; i++ )
for(j = 0 ; j < n ; j ++)
a[i][j] = temp[i][j];
}
int main(int argc, char *argv[]){
int array[5][5];
int i,j;
foo(array,5);
for( i = 0 ; i < 5 ; i++ )
{
for(j = 0 ; j < 5 ; j ++)
printf(" %d " ,array[i][j]);
printf("\n");
}
return 0;
}

Going with one double for loop and a temp array

Code:

#include <stdio.h>
void foo(int a[][5],int n)
{
int i,j;
int temp[n][n];
for( i = 0 ; i < n ; i++ )
for(j = 0 ; j < n ; j ++)
{
temp[i][j] = 15;
a[i][j] = temp[i][j];
}
}
int main(int argc, char *argv[]){
int array[5][5];
int i,j;
foo(array,5);
for( i = 0 ; i < 5 ; i++ )
{
for(j = 0 ; j < 5 ; j ++)
printf(" %d " ,array[i][j]);
printf("\n");
}
return 0;
}

But we do not need the array in this simple example.So improve space complexity too.

One double for loop ,no temp array.

Code:

#include <stdio.h>
void foo(int a[][5],int n)
{
int i,j;
for( i = 0 ; i < n ; i++ )
for(j = 0 ; j < n ; j ++)
a[i][j] = 15;
}
int main(int argc, char *argv[]){
int array[5][5];
int i,j;
foo(array,5);
for( i = 0 ; i < 5 ; i++ )
{
for(j = 0 ; j < 5 ; j ++)
printf(" %d " ,array[i][j]);
printf("\n");
}
return 0;
}