Thread: Beginner looking at code that I need to know what it does

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    4

    Question Beginner looking at code that I need to know what it does

    Hello,

    I'm new to C and I have to look into someones code who has been doing it for many years. He is unavailable to help me so I hope someone on here can. I have worked with C# for many years, but some of the syntax doesn't seem to make sense.

    For example :

    Code:
    struct dataBlk  
        {
        struct dataBlk *prevBlk;
        struct dataBlk *nextBlk;
        struct metaBlk *metaBlk;
        struct metaFld *metaFld;
        char *key;  
        char *data; 
        };
    
    struct dataBlk
        *dataBlk = 0,
        *rootBlk = 0;
    Is struct an object definition?

    So is he creating objects within an object?

    Why does he use struct dataBlk again after the initial struct definition?

    Thank you for any help

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Is struct an object definition?
    Yes.

    So is he creating objects within an object?
    No. You missed out something. He's creating pointers to objects. You could also create objects within an object but that's not what is happening in the above code.

    Why does he use struct dataBlk again after the initial struct definition?
    After defining an object, you're free to create as many instances of the object as you would with. That is what the author is doing in like 11

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    4

    Question

    Quote Originally Posted by Aslaville View Post
    Yes.



    No. You missed out something. He's creating pointers to objects. You could also create objects within an object but that's not what is happening in the above code.



    After defining an object, you're free to create as many instances of the object as you would with. That is what the author is doing in like 11

    Aslaville,

    Thank you for your help.

    I just looked and there is a struct for metaBlk and metaFld so those are pointers to those objects.

    So when this code is executed
    Code:
    struct dataBlk
      *dataBlk = 0,
      *rootBlk = 0;
    1. It is creating an object with pointers to prevBlk, nextBlk, metaBlk and metaFld?
    2. What is the definition of *rootBlk? I checked the code and there is no definiton for it.
    3. Why is *dataBlka and *rootBlk set to 0?

    Sorry if these are obvious questions. I just want to try and understand the concepts from the basic level so I build on that.

    Thank you again

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jerry8989
    1. It is creating an object with pointers to prevBlk, nextBlk, metaBlk and metaFld?
    No, this:
    Code:
    struct dataBlk
      *dataBlk = 0,
      *rootBlk = 0;
    is shorthand for:
    Code:
    struct dataBlk *dataBlk = 0;
    struct dataBlk *rootBlk = 0;
    Quote Originally Posted by jerry8989
    2. What is the definition of *rootBlk? I checked the code and there is no definiton for it.
    rootBlk is a pointer to a struct dataBlk object, i.e., *rootBlk is a struct dataBlk object.

    Quote Originally Posted by jerry8989
    3. Why is *dataBlka and *rootBlk set to 0?
    To initialise them to be null pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    4
    Thank you Laserlight.

    Can you explain this code some more:

    Code:
    struct dataBlk  
        {
        struct dataBlk *prevBlk;
        struct dataBlk *nextBlk;
        struct metaBlk *metaBlk;
        struct metaFld *metaFld;
        char *key;  
        char *data; 
    
        };
    Once rootBlk is initialized are the structs within the definitions pointers to other objects?
    How are these used?

    *prevBlk;
    *nextBlk;
    *metaBlk;
    *metaFld;
    char *key;
    char *data;

    rootBlk.prevBlk = another dataBlk object?

    Thank you

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jerry8989
    Once rootBlk is initialized are the structs within the definitions pointers to other objects?
    rootBlk was initialised to be a null pointer. Therefore, it does not point to any object, so that's that.

    Now, suppose that rootBlk was later modified to point to a struct dataBlk object. Then yes, the object that it points to will have member pointers such as prevBlk.

    Quote Originally Posted by jerry8989
    How are these used?

    *prevBlk;
    *nextBlk;
    *metaBlk;
    *metaFld;
    char *key;
    char *data;

    rootBlk.prevBlk = another dataBlk object?
    Not quite. rootBlk is a pointer so you must dereference it, e.g.,
    Code:
    (*rootBlk).prevBlk = &x;
    where x is a struct dataBlk object. Equivalently, and more commonly, we would use the syntactic sugar:
    Code:
    rootBlk->prevBlk = &x;
    I strongly suggest that you take some time to work through some introductory text on the C programming language. You may have worked with C# for many years, but C is after all a different programming language.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2015
    Posts
    4
    Thank you so much for making it clearer.
    I'm looking up intro tutorials and going through them now. I do appreciate you help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner - code problem
    By quicksilver1234 in forum C Programming
    Replies: 3
    Last Post: 03-07-2014, 07:47 PM
  2. Replies: 20
    Last Post: 11-04-2013, 12:12 AM
  3. Help with a code (beginner)
    By LK93 in forum C Programming
    Replies: 10
    Last Post: 09-23-2012, 11:29 PM
  4. Beginner code assistance
    By akust0m in forum C Programming
    Replies: 6
    Last Post: 08-07-2012, 09:29 PM
  5. help me with this code [beginner]
    By fredsilvester93 in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2012, 03:41 PM

Tags for this Thread