Thread: Structures, code completed. Creating the comments

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Structures, code completed. Creating the comments

    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);
        }
    }
    Last edited by alvarito; 10-05-2011 at 03:34 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        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?
    These are not pointers. These are actual structures. 'node' contains a number, three poitners to other nodes, and two structures of type 'clas'.

    They make one root node, and allocate space for that. The problem with this example though, is that it overwrites 'root', losing the first entry. It should be checking to see if root is null, and if not, making the new node point to it, as not to lose it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    // 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
    Obviously a linked list, it may be a form of tree, as you said, with left-mid-right for smaller-equal-greater values. Or is might be a ( in lack of a better word ) hierarchical linked list that points to the previous, next and child or parent nodes.

    // ALSO AN ARRAY OF POINTERS TO STRUCTURES, BUT THIS TIME POINTING
    //TO THE STRUCTURE "CLAS" THIS IS LIKE NESTLED STRUCTURES, RIGHT?
    An array of structures, rather. I see no pointers there.

    // THIS POINTER POINTING AT THE "SUBSTRUCTURE" CREATED ABOVE...
    //I DONT SEE THE POINT, EXCUSE THE PUN
    A linked list needs a referense point, something from where to start. That's obviously it's "root".

    // WHY DYNAMIC ? HAVENT WE FIXED IT AT LENGTH 16 CHARS?
    Humm, you need a tutorial on C's struct. Seriously!
    Devoted my life to programming...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not writing all English text in upper case would be a bonus as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    @Greaper: I believe the dynamic memory allocation to structure is meant because even if you set a maximum length for its members, that gives anyway variable needs of memory in the structure itself. I have been studying C for two weeks so far, I try the hardest I can. I appreciate your help.

    @SALEM The reason I wrote it in upper case is because also for myself in my own laptop I do it like that, since in my programming environment it is written in soft gray and I can barely see it. However, here it remarks it in green, and makes it more visible, but I copied it and pasted from my own place. Somehow it helps me differentiate the comments more clearly from the code. Sorry if it visually looked annoying
    Last edited by alvarito; 10-05-2011 at 03:51 AM.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    alright, thank you. I will examine that in depth. I first started with basics and then jumped to get some more complicated examples to progress in understanding. I m going to work on this one to fully grasp it. I appreciate your help.

    best regards

    Alvaro

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    You mean, line 26 overwrites line 15 ? that is, he declares once an array of pointers to structure

    struct node *p[M]

    and then again he declares another pointer to structure

    struct node *root ?

    Also, the general question, what would be the practical use
    of this code? Like Menu Tree with branches ?

    But any programming environment does that automatically,
    I mean, .NET or others create menus by just point and clicking no need to code anything. No?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > since in my programming environment it is written in soft gray and I can barely see it.
    So your inability to choose a decent colour scheme translates into our eyesore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alvarito View Post
    Also, the general question, what would be the practical use
    of this code? Like Menu Tree with branches ?
    Linked lists are like arrays, they are collections of similar items organized in some way... They have all kinds of uses but mostly they're valueable as data arrays when you don't know in advance how many elements you might need.

    But any programming environment does that automatically,
    I mean, .NET or others create menus by just point and clicking no need to code anything. No?
    Trust me... C does not do *anything* automatically. You want something to happen... you gotta tell C how to do it in very tiny steps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code that looks for Comments
    By dgs414 in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2007, 06:36 PM
  2. Need comments on code
    By Desolation in forum C++ Programming
    Replies: 10
    Last Post: 01-02-2007, 09:17 PM
  3. Percent of comments in your code?
    By ober in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-02-2004, 12:46 AM
  4. Code comments
    By salvelinus in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-17-2002, 01:27 AM
  5. code and comments check
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-17-2002, 12:45 PM

Tags for this Thread