Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I made some edits after your previous comments and after analyzed your code more carefully. I wrongly interpreted one remark about the head and tail items and though you were designing a circular list</p> <ol> <li><p>I took the time to copy / paste / compile your code. While it is almost working I must say that I would have designed in another way by </p> <ul> <li>moving the <code>prev</code>/ <code>next</code> pointers into the <code>struct team</code> </li> <li>and replacing the <code>team</code> member of <code>nodeTeam</code> by a <code>head</code> pointer to the first <code>team</code>. </li> </ul> <p>That would have the following benefits :</p> <ul> <li>prevent a useless waste of space for the <code>numberOfTeams</code> that is duplicated for each <code>nodeTeams</code> but only meaningful to the first</li> <li>avoid a confusion between the conceptual <code>head</code> and the actual first team</li> </ul> <p>By adding the value of the pointers in the team list with </p> <p><code>printf("| %-21s | %-22s | %p - p=%p n=%p\n",listofTeams-&gt;team.name, listofTeams-&gt;team.teamPlace, listofTeams, listofTeams-&gt;prev, listofTeams-&gt;next);</code></p> <p>I noticed a possible bug in your linking :</p> <blockquote> <p>| A team | A team place | 0x101d00980 - p=0x101d00920 n=0x101d009e0</p> <p>| B team | B team place | 0x101d009e0 - p=0x101d00980 n=0x101d009b0</p> <p>| C team | C team place | 0x101d009b0 - p=0x101d00980 n=0x101d00950</p> <p>| D team | D team place | 0x101d00950 - p=0x101d009b0 n=0x0</p> </blockquote> <p>You can see that next pointers are ok, but the prev pointers show <strong>suspicious duplicates</strong> (0x101d00920 is indeed the 'head').</p> <p>If you trace the execution of your code and check what it done in <code>addNodeTeamsSorted()</code> You could notice that everything is ok <strong>until step 3</strong> (addition of team C, after existing A &amp; D) :</p> <ul> <li>due to the weird double modification of both head and tail to find the place to insert the new item, head an tail are crossing: tail actually points to 'A' and head to 'D' (dont forget that while <code>head</code> is <code>Nodeteam *</code> and its modification won't be propagated outside of the function, <code>tail</code> is a <code>Nodeteam **</code>, thus when it is changed into the caller and for the next call it will be wrong</li> <li><strong>in the step 4</strong> (addition of 'B') in the <code>else if</code> part of the test, you change properly the prev/next of <code>no</code>, the next of (*tail) <strong>but not</strong> prev of <code>no-&gt;next</code> so you have <ul> <li>'B' -> next = 'C' : OK</li> <li>'B' -> prev = 'A' : OK</li> <li>*tail (='A') -> next = 'B' : OK</li> <li>'C' -> prev still = 'A' : <strong>WRONG</strong></li> </ul></li> </ul></li> <li><p>This is not like that that the compiler is thinking. He only processes compilational units, one at a time. What is not declared in the .c and the different included .h will remain unknown. If you want to share the structs declarations between 2 modules, and to prevent errors in code maintenance, cut the typedefs and put them in a common header file (e.g. <code>equipa.h</code>) that will be included in both .c</p></li> <li><p>You have to use <code>char*</code> instead of <code>char[]</code> because in your <code>main()</code> in file1.c you are making direct assignments from literal strings and the compiler won't let you assign a literal string to a char array. If you wan to use <code>char[]</code> instead change your </p> <p><code>eq.nome = "D team";</code></p> <p>by string copying like</p> <p><code>strcpy(eq.nome, "D team");</code></p> <p>of course, I am only dealing with the concept, actually you should take care that the string that will be copied are not longer than the buffer by using <code>strncpy()</code> and <code>sizeof(eq.nome)</code></p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload