0

I made a simple double link list; the program runs fine but I am getting a few compiler warnings that I need to get rid of. The warning is: assignment from incompatible pointer type [enabled by default]. They are at the locations marked ///(WARNING). The program is abridged for simplicity:

struct stack_struct
{
int data;
struct stack *next;
struct stack *previous;
};

typedef struct stack_struct *Stack_struct;
/// -------------------------------------------------------------------------------
void push(Stack_struct *stack, int Data);
void print_stack(Stack_struct stack);
void pop(Stack_struct *stack);
/// -------------------------------------------------------------------------------
int main()
{
Stack_struct stack = NULL;

...Print instructions...
...gets answer...

if(answer == LIST)
    print_stack(stack);

else if(answer == REMOVE)
    {
    if(stack == NULL)
        ...print message...
    else
        pop(&stack);
    }
else
    push(&stack, answer, &answer_type);

return 0;
}
/// -------------------------------------------------------------------------------
void push(Stack_struct *Stack, int Data)
{
Stack_struct new_node = malloc(sizeof(struct stack_struct));

new_node->data = Data;

 if(*Stack == NULL)
    {
    new_node->next = new_node->previous = NULL;
    *Stack = new_node;
    }
 else
    {
    new_node->next = *Stack; ///(WARNING)
    (*Stack)->previous = new_node; ///(WARNING)
    new_node->previous = NULL;
    *Stack = new_node;
    }
}
/// -------------------------------------------------------------------------------
void print_stack(Stack_struct Stack)
{
Stack_struct temp_node = Stack;

while(temp_node)
    {
    printf("%d ", temp_node->data);
    temp_node = temp_node->next; ///(WARNING)
    }
}
/// -------------------------------------------------------------------------------
void pop(Stack_struct *Stack)
{
Stack_struct delete_node = *Stack;
*Stack = (*Stack)->next; ///(WARNING)
free(delete_node);
}
Samuel
  • 366
  • 1
  • 5
  • 19

1 Answers1

2

Your structure type is was shown as:

struct stack_struct
{
    int data;
    struct stack *next;
};

Note that the next is a pointer to struct stack, not struct stack_struct, so it is a pointer to a different type. You can introduce a type such as struct stack just like that; if it isn't an already known type, you can only use pointers to the type until the type is fully defined — but see Which part of the C standard allows this code to compile? for more information. However, it is clear in this context that you did not intend to refer to a different type.

Fix that and a lot of problems will go away.

I notice that the code references a previous member of the structure; this is normal for a doubly-linked list, but you got a little too enthusiastic when pruning before adding the question to SO. Consequently, your structure type should be:

struct stack_struct
{
    int data;
    struct stack_struct *next;
    struct stack_struct *previous;  // Often abbreviated to prev
};
Community
  • 1
  • 1
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • Yep that was the issue, I missed the proper assignment. I think somewhere I changed the code a bit but forgot to change that one part. (Note: Somehow the previous pointer got omitted when I abridged it. I edited the post and put it back in.) – Samuel Jan 28 '14 at 02:06
  • Stuff happens; don't fret over it. The problem is still diagnosable with what you provided. I note that you could have left the skeletal `main()` out, leaving compilable code that generates the warning. This is generally preferable to non-compilable code. A tip for next time. – Jonathan Leffler Jan 28 '14 at 02:07