I created structer of 10 by 10 array for storing random number. Also made a list so each member will hold different 10 by 10. But when i print it, the numbers kind of broken, i dont know if my for loops are incorrect or there is something else. I hope i am doing the list thing correct.
struct Num
{
int Numbers[10][10];
};
struct Hold
{
struct Num *List;
}FullList;
int main(void)
{
time_t t;
int x;
srand((unsigned) time(&t));
printf("Enter the number of arrays you want to create: ");
scanf("%d", &x);
FullList.List = malloc(x * sizeof(int));
if (FullList.List == NULL) {
fprintf(stderr, "Malloc failed.\n");
exit(1);
}
int i, j, k;
for(i=0;i<x;i++)
{
for(j=0;j<10;j++)
{
for(k=0;k<10;k++)
{
FullList.List[i].Numbers[j][k] = rand() % 255;
}
}
}
for(i=0;i<x;i++)
{
for(j=0;j<10;j++)
{
for(k=0;k<10;k++)
{
printf("%d %d %d %d %d %d %d %d %d %d \n", FullList.List[i].Numbers[j][k]);
}
}
}
return 0;
}
ImageList.List = malloc(x * sizeof(int));
is wrong. It should beImageList.List = malloc(x * sizeof(struct Num));
The amount you're allocating is way too small. Also, you should check the value returned bymalloc
to make sure it isn'tNULL
. – Tom Karzes Oct 26 '20 at 15:37x
integers, but you needed space forx
instances ofstruct Num
. Eachstruct Num
contains an array of 100 integers total. So the allocated size was 100 times too small at the very least. This is something you really need to understand. – Tom Karzes Oct 26 '20 at 18:01