0

i am stucked in that code how can make memory allocation for that struct

typedef struct {
  int a, b, c, d;
} FourInts;


void fillArray(int* array, int len) {
  printf("Filling an array at address %p with %d "
         "values\n", array, len);
  for (int i = 0; i < len; ++i) {
    array[i] = (i * 3) + 2;
    // assert() verifies that the given condition is true
    // and exits the program otherwise. This is just a
    // "sanity check" to make sure that the line of code
    // above is doing what we intend.
    assert(array[i] == ( (i * 3) + 2) );
  }
  printf("Done!\n");
}


/***********from here the problem *******/
struct FourInts *heap_struct_FourInts = (FourInts*) malloc(sizeof( FourInts) * 1);

  fillArray(heap_struct_FourInts->*a), 4);


  free(heap_struct_FourInts);

the compiler gives me that error

   arrays.c:222:43: warning: initialization from incompatible pointer type [enabled by default]
   struct FourInts *heap_struct_FourInts = (FourInts*) malloc(sizeof( FourInts) * 1);
                                           ^
arrays.c:224:33: error: dereferencing pointer to incomplete type
   fillArray(heap_struct_FourInts->a, 4);
                             ^

what is the error in the code for the struct and malloc ?

3
  • 1
    I believe the type of heap_struct_FourInts should be FourInts* not struct FourInts* but I don't know why that would cause the errors that are produced. Commented Jul 10, 2014 at 20:23
  • The warning comes from the fact, that you assign a pointer to FourInts to a pointer to struct FourInts—note, that they are distinct types the latter of which is declared at line 222. Your error message shows a line different from the line in your code. A typo? (Don't type, copy and paste). Commented Jul 10, 2014 at 20:24
  • You might want to use a cast in your call to fillArray so that it would be fillArray((int *) heap_struct_FourInts, 4);, since fillArray is declared as taking (int*, int) parameters. Commented Jul 10, 2014 at 20:40

2 Answers 2

1

The following function call is incorrect:

fillArray(heap_struct_FourInts->*a), 4);

a is an int, not a pointer to an int, so you cannot dereference it. (Even if it was a pointer to an int, your syntax is incorrect).

Also, in your structure ...

typedef struct {
    int a, b, c, d;
} FourInts;

... you are not declaring an array of 4 ints, but four independent ints. If you would like a to be an array of ints with a length of 4 you need to declare it like this:

typedef struct {
    int a[4], b, c, d;
} FourInts;

Now you can call you function like this:

FourInts *heap_struct_FourInts = malloc(sizeof(*heap_struct_FourInts);
fillArray(heap_struct_FourInts->a), 4);

The following is equivalent:

fillArray((*heap_struct_FourInts).a), 4);
Sign up to request clarification or add additional context in comments.

Comments

1

To fix the first warning drop the struct from the variable type since it is not a struct, but a typedef for a struct (thus, warning for type mismatch). As for the error, use &heap_struct_FourInts->a to pass the address of the very first int in your structure.

However, the code potentially invokes undefined behaviour as the ints need not be contiguous in memory. For example, a compiler could be configured to pad to 8 byte boundaries by default, in which case there would be 4 unused bytes after each int (assuming we're on a platform that has 4 byte int). Read up on struct padding for more information. That particular padding would be a very unlikely scenario, but it is something to keep in mind.

3 Comments

Sprechen Sie Englisch? ;-)
@Fiddling Bits Ich hoffe es ... :-)
To add to Alex's point, ALWAYS address a data type when using structs via the user-defined type you created (ie: as a complete struct), or by accessing its members. Alignment and padding issues will always bite you if you do otherwise.

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.