stident
stident

Reputation:

enter data to structure

This code is going to a loop when I enter the name and it does not come out. What is the problem?

# define M 3
struct clas
{
    char name[16];
    int key;
};


struct node
{
    int n; 
    struct node *p[M];  
    struct clas clsf[M-1] ;
};

main()
{
    int i;

    struct clas clsf;
    for(i=0;i<2;i++)
    {
        root = malloc(sizeof (struct node));
        printf("enter name \n");
        scanf("%s\n",root->clsf[i].name);
        printf("%s\n",root->clsf[i].name);
        printf("enter key\n");
        scanf("%d", &root->clsf[i].key);
        printf("%d",root->clsf[i].key);
    }
}

Upvotes: 1

Views: 628

Answers (2)

Manish Trivedi
Manish Trivedi

Reputation: 3559

Replace only struct classifier clsf; to struct node *root; nothing more required

Upvotes: 1

peoro
peoro

Reputation: 26060

  • root is undefined, and there's an instance of an undefined struct classifier: clsf.

    Replace struct classifier clsf; with struct node *root;

  • remove the ending \n from scanf format:

    rpelace scanf("%s\n",root->clsf[i].name); with scanf("%s",root->clsf[i].name);

Upvotes: 4

Related Questions