i am reading a c++ source code, converting infix to postfix i am using turbo c++
#include <stdio.h>
typedef struct node
{
float data;
struct node *next;
} stack;
void StackInitiate(stack **head)
{
//error
if(*head=(stack *)malloc(sizeof(stack))==NULL)
exit(1);
(*head)->next=NULL;
}
// I'm getting .. cannot convert 'int' to 'node *' ...
can anybody tell me why so . and how to solve it regards.
full source code here
Because of operator precedence the expression
*head=(stack *)malloc(sizeof(stack))==NULL
is actually equivalent to
*head=((stack *)malloc(sizeof(stack))==NULL)
That is, you assign the value of the comparison to *head
.
You need to put in your own parentheses to make it correct:
(*head=(stack *)malloc(sizeof(stack)))==NULL
Or better yet use the new
operator which is what you rreally should use to allocate object dynamically in C++:
(*head=new stack)==NULL