1

I'm trying to assign a structure to pointer by using another pointer

typedef struct cat Category;
Category{
///some stuff here
};


Category *categoryList;
Category *ap = &categoryList;
*ap = (Category *)malloc(sizeof(Category));

I get this:

error: incompatible types when assigning to type 'Category' from type 'struct Category *'

What am I doing wrong?

eddie
  • 1,252
  • 3
  • 15
  • 20
Bob
  • 91
  • 8

4 Answers4

0

You are assigning pointer to pointer to structure Category to pointer to structure Category.. Yeah that's a lot of 'tos'. So how to fix it?

typedef struct cat Category;
Category{
///some stuff here
};

// declare variable - pointer to category
Category *categoryList;           
// store address of variable categoryList, which is pointer in variable ap,
// which needs to be of type Category ** since it is pointer to pointer!
Category **ap = &categoryList;     
// assign pointer to mallocated address to categoryList whose address is
// accessed - dereferenced via *ap
*ap = malloc(sizeof(Category));

Also please note, that you shouldn't cast return value of malloc. For explanation please refer to this answer

Community
  • 1
  • 1
Jendas
  • 3,359
  • 3
  • 27
  • 55
0

A variable declared with a pointer data type (ie, Category*) has a star counter, the number of Asterisks on the data type.

That's it, the variable x:

typedef struct category_t { 
// ...
} Category;

Category* x;

x has a star counter = 1 because you have just one star.

Then remember this:

  • Using the & operator increases the counter by one.
  • Using the * operator reduces the counter by one.

Then, the expressions:

  • &x has a star counter = 2, and
  • *x has a star counter = 0.

You always need to match the datatype, including the star counter.

On your example you have two errors:

Category *categoryList;   // 1
Category *ap = &categoryList; // 2
*ap = (Category *)malloc(sizeof(Category)); // 3

On line 2 your variable ap has a star counter = 1, yet the expression &categoryList has a star counter = 2; this is an invalid assigment.

On line 3 your variable ap has again star counter = 1, yet the expression *ap has a star counter = 0 and you're assigning the result of malloc which has a star counter = 1.

vz0
  • 32,345
  • 7
  • 44
  • 77
0
#include <stdlib.h>
#include <stdio.h>

typedef struct {
    // Some stuff here
} Category;


int main() {

    // *categoryList doesn't initialize a struct, it only creates a pointer to one. Leave the * off.
    Category categoryList;
    // Assign the address of the categoryList struct to ap (a pointer)
    Category *ap = &categoryList; 
    ap = malloc(sizeof(Category)); // Try to never cast malloc.

    return 0;
}
0

Taking issues one at a time...

Category *categoryList;

The type of the variable categoryList is Category * ( pointer to Category).

Category *ap = &categoryList;

The type of the variable ap is Category *, but the type of the expression &categoryList is Category **, or "pointer to pointer to Category". This is your first type mismatch; the assignment should be written

Category *ap = categoryList;

Finally,

*ap = (Category *)malloc(sizeof(Category));

The expression *ap has type Category; malloc returns a void *, which you are casting to Category *. You cannot assign a pointer value to a non-pointer type, which is where your compiler error is coming from. That line should be written

ap = malloc( sizeof *ap );

The cast is unnecessary1, and since the expression *ap has type Category, sizeof *ap will give the same result as sizeof (Category)2.


1. The cast is necessary in C++ and in very old versions of C predating the 1989 standard.
2. sizeof is an operator, not a function; the only time parentheses are required is when the operand is a type name.
John Bode
  • 119,563
  • 19
  • 122
  • 198