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"?
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"?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
If you need self-references, the typedef "EMPLOYEE" is not available at the point where the structure members are defined.Code:typedef struct { char name[32]; double salary; int payrollid; } EMPLOYEE;
So if we want a linked list of employees
However we can use the name "LINKED_EMPLOYEE" everywhere else, including whist accessing the "next" member.Code:typedef struct linked_employee { char name[32]; double salary; int payrollid; struct linked_employee *next; /* must use the struct tag here */ } LINKED_EMPLOYEE;
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
I'm refering to the self-reference part. Don't understand it much and good.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What's a pint and a quirk?
What's a sippet?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Nice. The question remains the same as in the first post above. The code is that:
I ask because I want to add in the last struct 10 incidents with ZERO changes in the structs and with the following form: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;
$> 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.
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:
Imagine also that you have 10 incidents with the following form:Code:typedef struct incList { int value; struct incList *next; } incident;
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
What's a data field? Also, where you refere to "records" you mean something like a voice messege or music?
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
Ahhhh.