0

Here is my code:

#include <stdio.h>

#define DEFAULT_CAPACITY 5

typedef struct Vector
{
    int items[DEFAULT_CAPACITY];
    int size;
} *VectorP;

// I am not allowed to change this struct definition.

int main()
{   
    VectorP *p;

    p = (VectorP *) malloc(DEFAULT_CAPACITY * sizeof(VectorP));

    if (p == NULL)
    {
        fprintf(stderr, "Memory allocation failed!\n");
        exit(1);
    } 

    //The problem is that I can't access instance of the vector this way ->    

    p->size = 0;
}

Searching online I found out that it is something to do with VectorP already being a pointer, I cannot change this because my professor wants it that way. How do I solve this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
pdhimal1
  • 229
  • 4
  • 9
  • 19
  • 1
    `VectorP` is just an alias for `struct Vector *`. So `VectorP *p` is a pointer to a pointer to a `struct Vector`. Just use `VectorP` if you want a pointer to the struct. – clcto Sep 04 '14 at 22:27
  • 3
    First of all, hiding pointers behind a typedef is a horrible-horrible idea. If I were you, I'd just use `struct Vector` and `struct Vector *`. Also, `sizeof(VectorP)` should be `sizeof(struct Vector)` so that it allocates memory for the vectors, not only for the pointers (incorrectly). Or even better, use `sizeof *p` for safety (in case your type ever changes). Furthermore, [don't cast the return value of `malloc()`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – The Paramagnetic Croissant Sep 04 '14 at 22:32
  • @BLUEPIXY He probably only wants to allocate one Vector, not an array of vectors – clcto Sep 04 '14 at 22:38
  • 1
    `my professor wants it that way`. Then go find another one (seriously). – michaelmeyer Sep 04 '14 at 22:49
  • 1
    typedeffing pointers considered harmful. Please find another "professor" – wildplasser Sep 04 '14 at 23:45

3 Answers3

2

These lines are wrong:

VectorP *p;
p = (VectorP *) malloc(DEFAULT_CAPACITY * sizeof(VectorP));

You need to use this instead:

VectorP p;
p = (VectorP) malloc(DEFAULT_CAPACITY * sizeof(struct Vector));

Or, if you are only interested in allocating 1 Vector object and not an array of multiple Vector objects:

VectorP p;
p = (VectorP) malloc(sizeof(struct Vector));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

It seems you mean the following

VectorP p;

p = ( VectorP ) malloc( sizeof( *p ) );

p->size = 0;

If you want to allocate an array of structures then the allocation will look like

VectorP p;

p = ( VectorP ) malloc( DEFAULT_CAPACITY * sizeof( *p ) );

p->size = 0;

or

p = ( VectorP ) malloc( DEFAULT_CAPACITY * sizeof( *p ) );

p[0].size = 0;

Or if you want indeed to allocate an array of pointers to the structure then the code will looki like

VectorP *p;

p = ( VectorP * ) malloc(DEFAULT_CAPACITY * sizeof( VectorP ) );

if (p == NULL)
{
    fprintf(stderr, "Memory allocation failed!\n");
    exit(1);
} 

for ( int i = 0; i < DEFAULT_CAPACITY; i++ )
{
    p[i] = ( VectorP ) malloc( sizeof( *p[i] ) );
}

p[0]->size = 0;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The allocation should be:

VectorP p;

p = malloc( sizeof *p );

This allocates enough space for one of the thing that p points to, regardless of whatever that is.

Then you can access the items via p->size, p->items[0], p->items[1], etc.

In C you should not cast malloc , and by using this pattern you can avoid the error of naming the wrong data type in the sizeof expression.

Your struct already contains an array of DEFAULT_CAPACITY items, so I am guessing that you just want one of these. If actually allocated DEFAULT_CAPACITY copies of your struct then you would have a total of 25 (non-contiguous) items.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365