“z”键,然后将生成“z”并与“\n”进行比较,因为它们不相同,scanf()将停止。我知道这听起来可能令人困惑,但请尝试了解它。如果您想丢弃'\n'字符,有很多方法。您可以在(getchar()!='\n')的同时给我们while这将从输入缓冲区中删除任

“z”键,然后将生成“z”并与“\n”进行比较,因为它们不相同,scanf()将停止。我知道这听起来可能令人困惑,但请尝试了解它。如果您想丢弃'\n'字符,有很多方法。您可以在(getchar()!='\n')的同时给我们while这将从输入缓冲区中删除任,c,pointers,segmentation-fault,header-files,extern,C,Pointers,Segmentation Fault,Header Files,Extern,“z”键,然后将生成“z”并与“\n”进行比较,因为它们不相同,scanf()将停止。我知道这听起来可能令人困惑,但请尝试了解它。如果您想丢弃'\n'字符,有很多方法。您可以在(getchar()!='\n')的同时给我们while这将从输入缓冲区中删除任何剩余的未读字符。 struct node{ char * name; struct node* next; }; extern struct node* head; #include<stdi


“z”键,然后将生成“z”并与“\n”进行比较,因为它们不相同,scanf()将停止。我知道这听起来可能令人困惑,但请尝试了解它。如果您想丢弃
'\n'
字符,有很多方法。您可以在(getchar()!='\n')的同时给我们
while这将从输入缓冲区中删除任何剩余的未读字符。
 struct node{

        char * name;
        struct node* next;
 };

 extern struct node* head;
#include<stdio.h>
#include<stdlib.h>
#include "header1.h"    //include the header file

struct node* head = NULL;   //Define the external variable head to be null initially

struct node* create();
void main(void){


    int option;

    do{

       printf("\nplease select the option : \n\t\t1)add element \n\t\t2)Print List\n");

    scanf("\n%d",&option);

    switch(option){
    case 1:
            head = create();
            break;
    case 2:
            print_list();
            break;

    }

    }while(option != 3);

}
#include<stdio.h>
#include<stdlib.h>                                                                                                              
#include "header1.h"
 //this function creates the linked list if the list is null or inserts the
 //element at the end of the list.


struct node* create() {

    if(head == NULL){

  //create a new list and return head.
      printf("\nhead is null\n");
      struct node* newnode = (struct node*)malloc(sizeof(struct node));
      char * new_name;
      printf("\nplease enter the new name\n  ");
      scanf("%s\n", new_name);
      newnode -> name = new_name;
      newnode -> next = NULL;
      head = newnode;
      return head; 



    }

    else if(head != NULL){

      printf("\nhead is not null\n ");
      struct node* newnode = (struct node*)malloc(sizeof(struct node));
      char * new_name;
      printf("\n Please Enter the new name \n");
      scanf("%s\n", new_name);
      newnode -> name = new_name;
      newnode -> next = NULL;

      struct node* ptr = NULL;

      ptr = head;
      while((ptr -> next) != NULL){

            ptr = ptr -> next;


      } 

            ptr -> next = newnode;
            return head;

    }



}
char * new_name; // declaring the pointer
printf("\n Please Enter the new name \n");
scanf("%s\n", new_name);//reading the value from stdin to where pointer is pointing