-2

Here is my struct,

typedef struct radixNode *radixPtr;
struct radixNode{
 int permission;
 int end;
 fileType type;
 radixPtr link[ASCII_SIZE];
};

I'm allocating memory for radixPtr,

radixPtr q;
q= createRadixNode();

And when I try to do this

q->link[index] = createRadixNode;

gcc gives warning: assignment from incompatible pointer type in c

Here is the allocate memory function

radixPtr createRadixNode () {
 radixPtr radix_node = (radixPtr)malloc(sizeof(struct radixNode));
 int i;

 if(radix_node == NULL) {
   perror("There is no memory to create radix node");
   exit(1);
 }

 radix_node->end = -1;
 for (i = 0; i < ASCII_SIZE; ++i) {
     radix_node->link[i] = NULL;
 }
 return radix_node;
}

I can't find a way to solve this. All things looks good. Thanks for any advice.

Abraam
  • 13
  • 1
  • 3
  • 2
    Did you mean `q->link[index] = createRadixNode();` instead of `q->link[index] = createRadixNode;`? And don't cast the result of `malloc` and family – Spikatrix Apr 04 '15 at 11:53
  • `q->link[index] = createRadixNode;` -->> `q->link[index] = createRadixNode();` – wildplasser Apr 04 '15 at 11:53
  • What a foolish mistake. Thank you! – Abraam Apr 04 '15 at 11:55
  • why doesn't gcc give error like createRadixNode doesn't declared ? – Abraam Apr 04 '15 at 11:56
  • @Abraam , I think that it is because the name of a function "decays" to a pointer that points to the function's address. So, `createRadixNode` is equal to `&createRadixNode`. The address of the function is used in several places like when using function pointers and multithreading(third argument of `pthread_create`) etc – Spikatrix Apr 04 '15 at 11:57
  • @CoolGuy , I agree with you – Abraam Apr 04 '15 at 12:00

1 Answers1

0

What you actually want is

q->link[index] = createRadixNode();

instead of

q->link[index] = createRadixNode;

This will fix this issue. BTW, don't cast the result of malloc(and family) in C.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83