2017-01-04 15 views
0

structurとpointers.passingの関数で構造体にアクセスし、構造体のメンバーにアクセスしていましたが、エラーが発生しました。機能には 'メイン':これは構造体とポインタのコンセプト

main.cの私のコードはエラーであるコンパイル後

#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:91:63:警告:の引数1を渡します12::6:注:[デフォルトで有効になって] 互換性のないポインタ型
のmain.cから 'SLL_insert_beg' '構造体のノード*' が、引数期待がタイプ '構造体ノード**' である
SLL_insert_beg(構造体ノード* head_lst main.c:98:62:警告: 'SLL_display'の引数1を渡します。 互換性のないポインタ型[デフォルトで有効]
); // choiceが2の場合、display関数が呼び出されます。

のmain.c:6:34注意:期待される '構造体のノード*' が、引数の型 である '構造体ノード**'
無効SLL_display(構造体のノード* head_lst

+0

コードを最小限に抑えてください。だから我々は助言を提供することができます。 – Massey101

+0

コンパイラが発行するエラーメッセージにはどのような不明点がありますか? – alk

答えて

1

問題がありますこれはstruct nodeへのポインタへのポインタであること&head_lstを引き起こす全てこの間head_lststruct nodestruct node *head_lst=NULL)へのポインタとして宣言されている他の単語で

- 宣言はSLL_insert_beg(struct node *head_lst);あり、呼び出しはSLL_insert_beg(&head_lst);あること。コンパイラは次のように警告します。expected 'struct node *' but argument is of type 'struct node **'

関数の宣言を変更するか、呼び出しを変更します。

+0

SLL_insert_beg()のように、head_lstの値を変更する場合は、宣言を変更する必要があります。その後、関数のコードを変更して "ポインタへのポインタ"(もちろん、structノード**、structノードを指すポインタへのポインタ)を変更する必要があります。 – linuxfan

+0

コードで簡単な説明をしてください。私は理解できません。どこでコードを変更すべきですか?ペインテンスrplyyyのおかげで – megaprog