Reputation: 1
I tried linked list programming by structur and pointers.passing parameters in functions and accessing structure members and I got below error please help.
#include<stdio.h>
#include<stdlib.h>
enter code here
struct node {
int32_t data;
struct node *next;
};
void
SLL_insert_beg( struct node *head_lst
){
int32_t num_lst;
struct node *temp_lst,*new_lst; //intialising local variable.
new_lst = ( struct node* )malloc( sizeof(struct node) ); //Allocating dynamic memory for node new.
printf(" Enter data: ");
scanf("%d", &num_lst );
new_lst->data = num_lst; //inserting data into the datafield in node new.
if(head_lst == NULL) { //Condition to check list is empty in SLL_insert_beg() function.
new_lst->next = NULL; //Making pointer field to point null in node new.
head_lst = new_lst; //Making new node as head node.
} else {
new_lst->next = head_lst; //pointer field in new node points to the head node.
head_lst = new_lst; //Making new node as head node.
}
}
void SLL_display( struct node *head_lst
) {
struct node *temp_lst,*new_lst;
int32_t i_lst = 1 ; //intialising local variable.
if( head_lst == NULL ) { //Condition to check list is empty in SLL_display() function.
printf( " List is empty!! " );
} else {
temp_lst = head_lst; //Making head node as temp node.
printf( "\nThe linked list is:\n " );
printf( " Elements: " );
/*Below loop used to print all elements
in single linked list*/
while( temp_lst != NULL ) {
printf( "%d-> ", temp_lst->data );
temp_lst = temp_lst->next; //Making temp next node as temp node.
}
printf( "NULL\n " );
printf( " Position: " );
temp_lst = head_lst; //Making head node as temp node.
/*Below loop used to print no.of positions
in single linked list*/
while( temp_lst != NULL ) {
printf( "%d " , i_lst );
i_lst = i_lst + 1; //Incrementing variable 'i_lst' once.
temp_lst = temp_lst->next; //Making temp next node as temp node.
}
}
}
void main(void) { //sll_program(void) {
int32_t data;
int32_t SL_ch; //Intialised a variable used to choose the option in the below menu.
struct node *head_lst=NULL;
struct node *temp_lst,*new_lst;
while( 1 ) {
printf(" \n\nSingly Linked List(SLL)");
printf(" \n1.Insert beg \
\n2.diplay \
\n3.exit " );
printf("\nEnter your choice(1-4):");
scanf("%d",&SL_ch);
switch(SL_ch) {
case 1: //If choice is 1 calls insert at beginning function.
SLL_insert_beg( &head_lst
);
break;
case 2:
SLL_display( &head_lst
); //If choice is 2 calls the display function.
break;
case 3:
return;
default:
printf( "Please give the right choice!!" );
}
}
}
AFTER COMPILATION THIS IS THE ERROR for my code
main.c: In function 'main':
main.c:91:63: warning: passing argument 1 of 'SLL_insert_beg' from incompatible pointer type [enabled by default]
main.c:12:6: note: expected 'struct node *' but argument is of type 'struct node **'
SLL_insert_beg( struct node *head_lst main.c:98:62: warning: passing argument 1 of 'SLL_display' from incompatible pointer type [enabled by default]
); //If choice is 2 calls the display function.main.c:34:6: note: expected 'struct node *' but argument is of type 'struct node **'
void SLL_display( struct node *head_lst
Upvotes: 0
Views: 118
Reputation: 1121
The issue is that the declaration is SLL_insert_beg(struct node *head_lst);
and the invocation is SLL_insert_beg(&head_lst);
- all this while head_lst
is declared as a pointer to struct node
(struct node *head_lst=NULL
). This causes &head_lst
to be a pointer to a pointer to struct node
.
In other words, as the compiler warned: expected 'struct node *' but argument is of type 'struct node **'
Either change the function's declaration, or change the invocation.
Upvotes: 1