Hello
I have a sample of unexplained code snippet of which I am trying to make sense by writing the comments myself. In looking at it from above, it looks like a linked structure, sort of the stuff you would build in a tree menu or something like that, yet I dont quite visualize it. I dont know if this snippet is a beginner level, but it looks to me somewhat convoluted.
Thank you
Code:
#include <stdio.h>
#include <stdlib.h>
# define M 3
struct clas //NO PROBLEM HERE
{
char name[16];
int key;
};
struct node
{
int n;
struct node *p[M]; // ARRAY OF POINTERS TO STRUCTURES BUT UNCLEAR TO ME,
//IS IT LIKE ONE OF THOSE TREE MENUS? A LINKED STRUCTURES BECAUSE THE POINTERS ARE POINTING TO ITS OWN STRUCTURE
struct clas clsf[M-1] ; // ALSO AN ARRAY OF POINTERS TO STRUCTURES, BUT THIS TIME POINTING
//TO THE STRUCTURE "CLAS" THIS IS LIKE NESTLED STRUCTURES, RIGHT? Update: yes sorry, of course, no pointers here,
};
main()
{
int i;
struct node *root; // THIS POINTER POINTING AT THE "SUBSTRUCTURE" CREATED ABOVE...
// OR ARE WE JUST GIVING IT A NAME TO ONE OF THE POINTERS
// THAT ARE PART OF THE ARRAY OF POINTERS CREATED ABOVE
for(i=0;i<2;i++)
{
root = malloc(sizeof (struct node));
printf("enter name \n");
scanf("%s",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);
}
}