gpt4 book ai didi

c - 结构和指针概念

转载 作者:行者123 更新时间:2023-11-30 15:01:22 26 4
gpt4 key购买 nike

我尝试通过结构和指针进行链表编程。在函数中传递参数并访问结构成员,但出现以下错误,请帮助。

#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!!" );

}
}
}

编译后这是我的代码的错误

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

最佳答案

问题在于声明是 SLL_insert_beg(struct node *head_lst); 并且调用是 SLL_insert_beg(&head_lst); - 所有这一切都是 head_lst 被声明为指向 struct node 的指针(struct node *head_lst=NULL)。这导致 &head_lst 成为指向 struct node 的指针。

换句话说,正如编译器警告的那样:需要“struct node *”,但参数的类型为“struct node **”

更改函数的声明,或更改调用。

关于c - 结构和指针概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com