Thread: Theoritical question.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    Theoritical question.

    I haven't still understood good after reading a lot the point of self-reference in a struct. Does it replace the addition of a new node in the struct? If yes, does it replace the "add new node in the end"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Are you talking about a node in a linked list? If so, the pointer to a node that is a member of the node struct is not a "self-reference"; it is a pointer to the next node in the linked list.
    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

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    408
    A structure cannot contain another structure which is identical to itself. This is a logical impossibility. It can contain a pointer to another structure identical to itself. This is often useful. For example, if you have a linked list, you have a "next" pointer member, whch points to the next item in the list. At list end, it is null. If you have a binary tree, you could hace "leftchild" and "rightchild" pointers, maybe a "parent" pointer. And you can build up other structures of arbitary complexity.

    C has a quirk, which is that normally people typedef structures and make the structure itself nameless.

    eg you will often see code like
    Code:
    typedef struct
    {
       char name[32];
       double salary;
       int payrollid;
    } EMPLOYEE;
    If you need self-references, the typedef "EMPLOYEE" is not available at the point where the structure members are defined.
    So if we want a linked list of employees

    Code:
    typedef struct linked_employee
    {
       char name[32];
       double salary;
       int payrollid;
       struct linked_employee *next; /* must use the struct tag here */
    } LINKED_EMPLOYEE;
    However we can use the name "LINKED_EMPLOYEE" everywhere else, including whist accessing the "next" member.
    The use of the tag is purely a little workaround for one of C's quirks.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    I'm refering to the self-reference part. Don't understand it much and good.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Quote Originally Posted by TheGreekMan2000 View Post
    I'm refering to the self-reference part. Don't understand it much and good.
    What do you mean by the "self-reference part"? A code snippet might help.
    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

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    What's a pint and a quirk?

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    What's a sippet?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Post code. Actually, let's make this a rule: whenever you ask a question that has to do with some code concept, post relevant code to go along with it.
    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

  9. #9
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Nice. The question remains the same as in the first post above. The code is that:

    Code:
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        /*Gia string ""*/
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    }incident;
    I ask because I want to add in the last struct 10 incidents with ZERO changes in the structs and with the following form:

    $> newIncident Mediterranean;;5;2;0;1;Drowning,Asphyxiation;Off the coast of Larache, Morocco;35.189860435378,-6.173145606543; https://archive.ph/gB4Vs

    With semicolons( in between each member of the struct incident.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    From what I see, you want to parse that line of input separated by semi-colons into the nodes of a linked list.

    If you find this difficult to understand, then take a step back and imagine you had this struct instead:
    Code:
    typedef struct incList
    {
        int value;
        struct incList *next;
    } incident;
    Imagine also that you have 10 incidents with the following form:
    1;2;3;4;5;6;7;8;9;10

    Write a program to parse this line of incident numbers to create a linked list of 10 incident objects using the struct I showed you.

    Resist the urge to work on your actual program until you have done this. The idea is to acquire the skill of working with a simple linked list where each node has just one int data member before you try handling a linked list that's more complex, like the one in your actual program.
    Last edited by laserlight; 05-25-2020 at 06:04 AM.
    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

  11. #11
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    The semicolons doesn't refere to the repetitions of the program but rather for the separation of the data that must be put into the incident struct.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    408
    Quote Originally Posted by TheGreekMan2000 View Post
    The semicolons doesn't refere to the repetitions of the program but rather for the separation of the data that must be put into the incident struct.
    Normally data fields are separated by commas, and records are separated by newlines. Here, a semicolon rather than a newline is the record separator. That's only a small difficulty. Start by splitting your input on semicolons, to get the records.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  13. #13
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    What's a data field? Also, where you refere to "records" you mean something like a voice messege or music?

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    408
    Quote Originally Posted by TheGreekMan2000 View Post
    What's a data field? Also, where you refere to "records" you mean something like a voice messege or music?
    Imagine a payroll program. An employee has a name, a salary, and a payroll id. These are the three "fields". Taken together, they describe an employee, and are a "record".
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  15. #15
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Ahhhh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2014, 05:41 PM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. Theoritical question in trees structures
    By nomikos in forum C Programming
    Replies: 5
    Last Post: 12-20-2010, 11:00 AM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM
  5. A theoritical Qustion
    By ihsir in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2002, 11:47 PM

Tags for this Thread