I wrote a function that do some some computation and returns result as
a char* [5] type. After, I used this function in other program that
executes the same function 10 times, then store each result in each
array's element with type char* big_array[5], so the total length of
big_array must be (5+1)*10, where 1 bit is added to store the '\0'
character .
To show result I used printf (see code 1) and I noticed that if I use
printf in the loop, I see that at each iteration the result changes,
but if I use printf out of loop (see code 2) , I see that only the
last result is shown at each iteration. This mean that at each
iteration the new value given by function erases the old result.
How to avoid this?
My code is like this
/************ code 1 *********************/
char* function (int in1, int in2)
{
static char array [5];
/*do some computation*/
return(array);
}
int main ()
{
int i = 0;
char* big_array [(5+1)*10];
for (i = 0; i<6*9; i=i+6)
{
big_array [i+6] = function( i , 2);
printf("%s\n",big_array [i+6]);
}
return 0;
}
/************** End code 1 *****************/
/************ code 2 *********************/
char* function (int in1, int in2)
{
static char array [5];
/*do some computation*/
return(array);
}
int main ()
{
int i = 0;
int j = 0;
char* big_array [(5+1)*10];
for (i = 0; i<6*9; i=i+6)
{
big_array [i+6] = function( i , 2);
}
for (j=0;j<6*9; j=j+6)
{
printf("%s\n",big_array [j+6]);
}
return 0;
}
/************** End code 2 *****************/