Why does this strcpy code on a 2D array generate the wrong output?
I tried running this code of two dimensional charracter arrays (Arrays of strings) Depending on the compiler and website the code showed garbage values at around 10x25-10x40 (string size).
#include<stdio.h>
#include<string.h>
int main()
{
int n=0;
char name[10][30];
char e[20];
int i;
for(i = 0; i < 30; i++)
{
strcpy(name[i],"0");
}
for(i = 0; i < 30; i++)
{
printf("\n%s",name[i]);
}
}
Solution 1:
The problem is that you go out of bounds of your array:
char name[10][30];
int i;
for( i=0;i<30;i++)
{
strcpy(name[i],"0");
}
Here you iterate 30 times over an array of only 10 elements.