0
#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
4
  • a, b, and c are out of scope after the call to generate_arrays and using them by address is undefined behavior. Commented Mar 6, 2015 at 18:34
  • srand(time(NULL)); is in the wrong place - it should be before array=generate_arrays(len); which @iharob updated answer has. Commented Mar 6, 2015 at 18:39
  • @WeatherVane good catch, I didn't notice that until you said something. Thanks. Commented Mar 6, 2015 at 18:44
  • While debugging it can be helpful to use srand(0) so the sequence (and errors!) are repeatable. Commented Mar 6, 2015 at 18:46

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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));

3 Comments

Question is C, not C++.
Yes just in C, so I couldn't use that.
it doesn't mean it's not helpful, and it does not deserve a downvote.... I updated the answer in order to replace new with malloc, it's the only difference!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.