简体   繁体   中英

Printing struct pointers from one instance of the struct

#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

You are storing the addresses of local variables in the struct members.

The a b and c arrays are local to the generate_arrays() function and when that function returns the arrays are deallocated.

You need to ensure that they data will still be valid after the function returns, so I would suggest the following

#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    index;
    int    length;
    Arrays array;

    srand(time(NULL));

    length = 10;
    array  = generate_arrays(length);

    for (index = 0 ; index < length ; index++)
        printf("%d %d %d\n", array.first[index], array.second[index], array.third[index]);

    free(array.first);
    free(array.second);
    free(array.third);

    return 0;
}

Arrays generate_arrays(int length)
{
    int    index;
    Arrays array;

    array.first  = malloc(length * (sizeof(*array.first)));
    array.second = malloc(length * (sizeof(*array.first)));
    array.third  = malloc(length * (sizeof(*array.first)));

    if ((array.first == NULL) || (array.second == NULL) || (array.third == NULL))
     {
        free(array.first);
        free(array.second);
        free(array.third);

        array.first  = NULL;
        array.second = NULL;
        array.third  = NULL;

        return array;
     }

    for (index = 0 ; index < length ; index++)
    {
        array.first[index]  = ((rand() % length) + 1);
        array.second[index] = ((rand() % length) + 1);
        array.third[index]  = ((rand() % length) + 1);
    }

    return array;
}

The declaration

int a[len],b[len],c[len];

is declaring/allocating variables which are local to the function.

You should perform something like this:

int *a = malloc(len * sizeof(int)), 
    *b = malloc(len * sizeof(int)), 
    *c = malloc(len * sizeof(int));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM