A.T.
A.T.

Reputation: 51

Incorrect output in an array inside a struct

I have a struct with an int array inside that I'm passing to a function for the array to be initialized

array struct like so..

typedef struct Container{
    struct intArray *P;
    int length;
} Container;

typedef struct intArray{
    int *array;
    int length;
} intArray;

function to initialize the array like so...

int Initializer(intArray *myStruct, int n)
{
    myStruct->array = malloc(sizeof(int) * (lengthOfint);
                                              ^
                                        If n=55 then length would be 2
    //let's just say n=5
    myStruct->array[0] = n;

    //return 1 if successful
    return 1;
}

In another function I am calling the initializer function like so...

Container *myContainer = malloc(sizeof(Container));

myContainer->P = malloc(sizeof(intArray *) * Some_Defined_Value);

Initializer(&myContainer, 5);

printf("the data that should be at index 0 -> %d\n", myContainer->P.array[0];

I would think that the printf statement would print out 5 but it prints out garbage of varying numbers every time I compile it. I don't think I have a grasp of the array within a struct within a struct idea.

Upvotes: 0

Views: 105

Answers (3)

Ingo Blackman
Ingo Blackman

Reputation: 990

Container holds a pointer to struct intArray, which in this case is the start of an array of struct intArray. But then you initialize this pointer with

malloc(sizeof(intArray *) * Some_Defined_Value);

So malloc returns a pointer to a memory space which holds pointers to struct intArray because you used sizeof(intArray *) and not sizeof(intArray) (you allocated space for Some_Defined_Value number of pointers).

You need sizeof(intArray) * Some_Defined_Value here to allocate space for Some_Defined_Value number of struct intArray's.

Then you use

Initializer(&myContainer, 5);

which should at least give a warning because you pass a pointer to a pointer to a struct Container, but Initializer expects a pointer to a struct intArray, so this is not what you want. To initialize the first element of your array of struct intArray's use:

Initializer(&(myContainter->P[0]), 5);

Then:

printf("the data that should be at index 0 -> %d\n", myContainer->P.array[0]));

This code does not compile because it should be:

printf("the data that should be at index 0 -> %d\n", myContainer->P[0].array[0]);

myContainer->P accesses a pointer to a struct intArray. The code right above means you access the first element (element number 0) of the array of struct intArray's .

To initialize/access the second element of the array of struct intArray's use:

Initializer(&(myContainter->P[1]), 5);
printf("the data that should be at index 0 -> %d\n", myContainer->P[1].array[0]);

Upvotes: 0

Gauthier Boaglio
Gauthier Boaglio

Reputation: 10242

Also be aware that malloc returns a void * so most of the time you'll need a cast like so:

myStruct->array = (int*)malloc(sizeof(int) * (length);
//                ^^^^^^

Assuming length is the number of int you want to store in your array.

Upvotes: 0

user268396
user268396

Reputation: 11976

You pass the container to the initializer function, but if you look closely you'll see that the you are passing a pointer to a pointer to the "outer" container instead of a pointer to the desired struct intArray. You'd want something like Initializer(myContainer->P, 5);

Upvotes: 1

Related Questions