Dynamic two dimensional Array of Characters
Hi all
I am trying to write the code for creating two dimension array of character and assign them value.
The code is:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
char **arrKey;
int i,total_no=5;
arrKey =(char **)malloc(total_no * 100);
memset(arrKey,0x40,sizeof(arrKey));
for ( i = 0; i< 5; i++ )
{
memcpy(arrKey,"100",3);
}
for ( i = 0; i< 5; i++ )
{
printf("\n %s",arrKey);
}
getchar();
return 0;
}
The output is:
Quote:
100@══════════════════════════════════════════════ ═════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
═════════════════════²²²²½½½½½½½½
100@══════════════════════════════════════════════ ═════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
═════════════════════²²²²½½½½½½½½
100@══════════════════════════════════════════════ ═════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
═════════════════════²²²²½½½½½½½½
100@══════════════════════════════════════════════ ═════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
═════════════════════²²²²½½½½½½½½
100@══════════════════════════════════════════════ ═════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
══════════════════════════════════════════════════ ══════════════════════════════
═════════════════════²²²²½½½½½½½½
I am getting the above result. I am doing memset to reset the dynamic array.
Can anybody help me in this regard so that the output is proper.
What I really want from above code is that, I want a array to be created at run time which look like this:
a[][] = {"aaaaaa","bbbbbb","ccccccc","ddddddd","UPTO LOOPS RUN"};
Please suggest me if any one has more efficient way to do that.
Thanks