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);
}
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
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.