#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct{
int* first;
int* second;
int* third;
}Arrays;
Arrays generate_arrays(int len);
int main(void)
{
int i;
int len=10;
Arrays array;
array=generate_arrays(len);
srand(time(NULL));
for(i=0;i<len;i++)
{
printf("%d %d %d\n",*array.first++,*array.second++,*array.third++);
}
return 0;
}
Arrays generate_arrays(int len)
{
int i;
Arrays array;
int a[len],b[len],c[len];
for(i=0;i<len;i++)
{
a[i]=((rand()%len)+1);
b[i]=((rand()%len)+1);
c[i]=((rand()%len)+1);
}
array.first=a;
array.second=b;
array.third=c;
printf("\n\n");
for(i=0;i<len;i++)
{
printf("%d %d %d\n",*array.first++,*array.second++,*array.third++);
}
return array;
}
In the code above I am trying to print out the struct values of just one instance of the struct Arrays, instead of using a pointer. To do that I am using arrays to store the random numbers and then assigning an array to a pointer inside the struct. The printf inside the function prints out the ints normally(top set of numbers), however when the printf in main runs it prints out garbage(bottom set of numbers). Sample output is below. So my question is why would it be doing this because the printfs are the exact same?
8 10 4
9 1 3
5 9 4
10 1 6
3 3 8
4 8 10
1 3 4
10 10 8
1 4 10
9 7 6
-65536 0 0
8064 0 0
-64641 0 1606415984
-65536 0 32767
-1 0 2058584832
0 0 32767
0 -64641 1606416144
0 -65536 32767
-65536 -1 -1857669479
8064 0 32767
Aucun commentaire:
Enregistrer un commentaire