Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:04:54+00:00 2026-05-31T06:04:54+00:00

So my problem right now is that for some reason, I designate a pointer

  • 0

So my problem right now is that for some reason, I designate a pointer as NULL in the first function to be called, but then when I check it later, it’s not NULL anymore.

So I have a few structs, detailed here:

#include <stdio.h>
typedef struct filenode {
  struct filenode *next, *prev;
  const char *name;
} Filenode;

typedef struct foldernode {
  struct foldernode *next, *root, *subdir, *prev;
  Filenode *filenodes;
  const char *name;
} Folders;

typedef struct Filesystem {
  Folders *current;
} Filesystem;

And then I go to my actual file, where I initialize the Filesystem.

This is the initialization function:

void mkfs(Filesystem *files) {
  /* Initializes space for the filesystem itself */
  files = malloc(sizeof(*files));

  if(files == NULL) {
    printf("Memory allocation failed!\n");
    return;
  }
  /* Initializes space for the first root node */
  files->current = malloc(sizeof(files->current));

  if(files->current == NULL) {
    printf("Memory allocation failed!\n");
    return;
  }

  files->current->filenodes = NULL;
}

Now, when I go into the next function, mkdir, and I check if files->current->filenodes = NULL, and it is not NULL anymore. I’m extremely confused right now. And yes, the same *files variable is being passed in to every function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:04:55+00:00Added an answer on May 31, 2026 at 6:04 am

    From the comments, you say you can’t change the signature of mkfs(). I’m going to assume it’s called like this:

     Filesystem files;
     mkfs(&files);
    

    If that’s the case, you don’t need to create space for the structure (since it’s created on the stack just before your mkfs() call), so you can remove this line:

      files = malloc(sizeof(*files));
    

    Removing this line will fix the problem that you can’t see the changes to files after the function has returned. This was causing you trouble since C is always pass by value – the malloc was changing the value of the local copy of the files pointer, meaning that no further changes were seen outside your function.


    You will also run into trouble with this line:

    files->current = malloc(sizeof(files->current));
    

    Since files->current is a pointer to a struct foldernode, the sizeof call only tells you the size of the pointer. You probably meant:

    files->current = malloc(sizeof(struct foldernode));
    

    or:

    files->current = malloc(sizeof(Folders));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an app that's in invite-only beta right now. Problem is, I can't
I've already looked around but couldn't find the exact solution/problem I'm having right now.
I right now have a problem that is rather confusing to me: I have
Right now I'm trying to play a .wav file that is first recorded with
We are having this problem with a controller right now; the controller looks like
Right now i use iText to generate a pdf automatically. And my problem is
My problem is certainly right on my face but I can't see it... I
Hi Guys new to this site but a big fan. Right the problem. It's
problem is , that whenever the grid's row is right clicked the selected item
I'm trying to use eclipse on Win 7, but for some reason it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.