Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Jonathan Leffler Jun 12 '13 at 23:37
    
I think, op means something to the likes of lengthOfint == ceil(log10(abs(n))). –  David Foerster Jun 12 '13 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. –  FunkyT Jun 12 '13 at 23:49

3 Answers 3

up vote 0 down vote accepted

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]);
share|improve this answer
    
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} –  FunkyT Jun 13 '13 at 0:39
    
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]. –  Ingo Blackman Jun 13 '13 at 1:02

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

share|improve this answer
    
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. –  FunkyT Jun 12 '13 at 23:39
    
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! –  David Foerster Jun 12 '13 at 23:50
    
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. –  FunkyT Jun 13 '13 at 0:04

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.