Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are seeing this behavior because you have a type mismatch between the definition of <code>appendAtEndOfArray</code> and the arguments you are passing into it. NuclearGhost pointed this out in the comments. As he stated, the function declaration needs to be changed to</p> <pre><code>void appendAtEndOfArray(struct node * item, struct node * arrayPointer[]) </code></pre> <p>The "Bad access" error you saw after changing the array parameter comes from the while loop. After correcting the function declaration, <code>arrayPointer[i]</code> is has type <code>struct node *</code>. Since you are now accessing the struct member through a pointer, the <code>.</code> (dot) operator must be changed to <code>-></code>:</p> <pre><code>while (arrayPointer[i]-&gt;name != '\0') { </code></pre> <p>Now you can take twalberg's advice to assign the value of <code>item</code> directly:</p> <pre><code>arrayPointer[i] = item; </code></pre> <p>There is one more problem that needs to be corrected: <code>arrayPointer[i]</code> is a pointer type, so it can have a null value. That condition needs to be checked before dereferencing the pointer, or the program will likely crash from a segmentation fault:</p> <pre><code>while (arrayPointer[i] &amp;&amp; (arrayPointer[i]-&gt;name != '\0')) { </code></pre> <hr> <p><em>Edit: Additional explanation of the "why" behind your concern that the code "strangely removes the value from the old address."</em></p> <p>In your original code, when you passed <code>sourceNode->children</code> into <code>appendAtEndOfArray</code>, the compiler issues a warning because of the type mismatch but generates the code anyway. It can do this because the value you pass in and the value the function expects are both memory addresses -- the "type" of the pointer simply determines how the compiler treats the memory the pointer refers to, so no actual data conversion has to be performed.</p> <p>On my machine, a 32-bit x86 platform, pointers are 4-bytes and your <code>struct node</code> type is 252 bytes (due to padding the char and bool types up to 4-bytes, each). When <code>appendAtEndOfArray</code> assigns <code>item</code> to the first element of <code>arrayPointer</code>, as (from the original code):</p> <pre><code>arrayPointer[i]=*item; </code></pre> <p>The system copies the 252 bytes of data from the struct to a memory location that was intended to hold a 4-byte pointer. As a result, the next 248 bytes after <code>arrayPointer[i]</code> are overwritten. Since the nodes are allocated in an array, that means that part of the next node in the <code>nodes</code> array will be overwritten.</p> <p>For example, consider the call</p> <pre><code>appendAtEndOfArray(destinationNode,sourceNode-&gt;children); </code></pre> <p>Assume the <code>sourceNode->children</code> array was empty, so <code>destinationNode</code> would be assigned to the 0th element. Since the assignment is actually writing the entire struct contents to the memory location of the 0th element, this would overwrite all 30 elements of <code>sourceNode->children</code> (120 bytes) as well as all of <code>sourceNode->parents</code> (another 120 bytes), leaving another 12 bytes of data that spills into the next element in the nodes array, which (on my machine) covers both the <code>name</code> and <code>visited</code> members.</p>
 

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