Note that there are some explanatory texts on larger screens.

plurals
  1. POAssignment of a pointer within a struct NOT WORKING, why is the value not changing?
    primarykey
    data
    text
    <p>For a school project I am supposed to implement a simplified version of the UNIX filesystem using only linked list structures. I am currently having a problem with my mkfs() function, which is supposed to simply initialize a filesystem. </p> <p>My header file that creates the structures I am using is here: </p> <pre><code>typedef struct Lines { char line[82]; struct Lines *next; } Lines; typedef struct Node { char *name; int id; struct Node *parent; struct Node *next; union { char line[82]; struct Node *children; } contents; } Node; typedef struct Filesystem { char *name; struct Node *root; struct Node *current; } Filesystem; </code></pre> <p>Here is the method in my separate file which #includes this header file:</p> <pre><code>void mkfs(Filesystem *files) { Node *root = NULL; /* Creates a pointer to the directory we will use as * the root directory for this filesystem*/ files = (Filesystem *)malloc(sizeof(*files)); /* Allocates space for the the * filesystem structure */ if(files == NULL){ /* If there is no memory available, prints error message * and does nothing else */ printf("Memory allocation failed!\n"); } else { root = (Node *)malloc(sizeof(*root)); /* Allocates space for the root * directory of the filesystem. */ if(root == NULL) { /* If there is no memory available, prints error * message and frees memory obtained thus far, but then * does nothing else */ printf("Memory allocation failed!\n"); free(files); } else { /* Allocates space for the root directory's name string */ root-&gt;name= (char *)malloc(sizeof(char)*(strlen("/")+1)); if(root-&gt;name == NULL) { /* If there is no memory available, prints error * message and frees memory obtained thus far, * but then does nothing else */ printf("Memory allocation failed!\n"); free(files); free(root); } else { root-&gt;name = "/"; /* Defines the root directory as being named by the * forward slash */ /* DO STR CPY HERE ITS CHANGING THE ADDRESS */ root-&gt;contents.children = NULL; root-&gt;next = NULL; root-&gt;parent = NULL; /* UHH CHECK ON THIS NOOO CLUE IF ITS RIGHT FUUU*/ files-&gt;root = root; /* The filesystems pointer to a directory is set * to point to the root directory we just allocated * space for and set up */ files-&gt;current = root; /* Sets the filesystems current directory to * point to the root directory as well, because * it is the only directory in existence for this * filesystem at this point. */ } } } } </code></pre> <p>The problem I am having is that when I run gdb and step through each line, the last two assignment lines ARE NOT CHANGING the contents of file->root and file->current. For example, here I print the contents of files->root, run the line files->root = root, and then print again, and you can see the address has not changed. However if I just print root, the thing I am trying to assign it to, it clearly has a different value that files->root SHOULD have been set to:</p> <pre><code>(gdb) print files-&gt;root $12 = (struct Node *) 0x400660 (gdb) step (gdb) print files-&gt;root $13 = (struct Node *) 0x400660 (gdb) print root $14 = (Node *) 0x602030 </code></pre> <p>Does anyone have any idea as to why an assignment might not work in this case? This is currently ruining my whole project, so any insight would be greatly appreciated. Thank you!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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