Note that there are some explanatory texts on larger screens.

plurals
  1. POChar Pointer Segmentation Fault
    text
    copied!<p>Okay so here is the gist of what's happening:</p> <p>I am passing in a character array <code>(char[x])</code> into a function, whose argument is defined to be a character pointer <code>(char *)</code>. Once inside the function I assign another character pointer (which is a part of a struct I have). I get a segmentation fault as soon as I assign the incoming argument, to the structure's character pointer; as such.</p> <pre><code>temp-&gt;name = (char*)malloc(sizeof(char)); temp-&gt;name = name; </code></pre> <p>This is how I've been utilizing the function previously:</p> <pre><code>char *name = "HEY"; Function(name); </code></pre> <p>Here's how I am utilizing it with errors:</p> <pre><code>char name[3] = "HEY"; Function(name); </code></pre> <p>with the same statement above, and it works fine. I made sure it wasn't anything else, by changing name to a constant "HEY", with the same input and everything went smoothly. </p> <p>If anybody can think of a reason why off the top of their head, I would greatly appreciate the help. Thank you! </p> <p><strong>Here's the function in full:</strong></p> <ul> <li>openList is the pointer to the beginning of a linked list of structs</li> <li>tempOpen is a temporary pointer we can utilize to scour through the list, without altering the openList position</li> <li>findOpenSetSID/findItem -> looks for a struct in the linked list via the SID/key</li> <li>answerOpen/answerItem -> 1 == first node, 2 == any other node, 0 = not found</li> </ul> <p><strong>Here is a breif summary of the structures involved.</strong> The open structure is a linked list of pointers to another structure (called set structures) A set structure is a linked list of names, sid's, and a item structure An item structure is a linked list of data and keys</p> <pre><code>Error_t WRITE(Sid_t sid, char *key, char *data){ Open_p_t tempOpen = openList; //setting a pointer to a struct int answerOpen = findOpenSetSID(sid, &amp;tempOpen); if(answerOpen &gt; 0){ Set_p_t targetNode; if(answerOpen == 1){ targetNode = tempOpen-&gt;file; } else{ targetNode= tempOpen-&gt;next-&gt;file; } Item_p_t tempItem = targetNode-&gt;items; int answerItem = findItem(key, &amp;tempItem); Item_p_t targetItem; targetItem = (Item_p_t)malloc(sizeof(Item_t)); if(answerItem &gt; 0){ if(answerItem == 1){ targetItem = targetNode-&gt;items; } else{ targetItem = targetNode-&gt;items-&gt;next; } targetItem-&gt;data = data; } else{ **targetItem-&gt;data = data;** &lt;&lt;&lt;The problem line. basically I am just adding items to my sets. But this line freaks out when the input changes from char* to char[] targetItem-&gt;key = key; targetItem-&gt;next = targetNode-&gt;items; targetNode-&gt;items = targetItem; } return 0; } return 1; } </code></pre> <p><strong>Here's the input segment:</strong></p> <p><code>char key[32], data[64]; // reads in data using fscanf(fd, "%s %s", key data) then calls WRITE(setId, key, data);</code></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