0

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.

3
  • Please post code that could compile...myStruct->array = malloc(sizeof(int) * (lengthOfint); is missing a close parenthesis. Also, I'm puzzled how n=55 leads to lengthOfint == 2; are you measuring in bits and assuming sizeof(int) == 4 and CHAR_BIT == 8? Not unreasonable assumptions, but the use of a bit count is not so obvious. Commented Jun 12, 2013 at 23:37
  • I think, op means something to the likes of lengthOfint == ceil(log10(abs(n))). Commented Jun 12, 2013 at 23:46
  • The idea behind that is that I need to store a number into a int array so 12345 would be in an intArray[5] = {1,2,3,4,5}...So 55 would yield an array of size 2. Commented Jun 12, 2013 at 23:49

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

the compiler tells me that's an incompatible type argument for Initializer when I do that, that's the only reason I changed it to &myContainer. of course the code I have sampled here is not the complete thing but it's pretty accurate.
It's an incompatible pointer type because the types Container and intArray are indeed incompatible. Trying to cast Container** to intarray* instead doesn't make it better. These warnings exist for a reason!
So what's the work around, no matter what this is the infrastructure I'm trying to implement -> A container which holds multiple intArray structs and each intArray struct holds an int array with different amounts of data.
0

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

2 Comments

I am trying to achieve A single container which holds an array of structs. Inside of each of these structs are an int array which holds an integer represented in array form -> 12345 is an array[5]={1,2,3,4,5}
Then you should be fine with the end of what I wrote. If you want to initialize the 2nd intArray inside your Container, you need Initializer(&(myContainer->P[1])); and to access it (after initialization), myContainter->P[1].array[0].
0

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.

Comments

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.