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 6202787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:47:59+00:00 2026-05-24T04:47:59+00:00

I made this little piece of code where I alloc and then free memory

  • 0

I made this little piece of code where I alloc and then free memory for a struct:

struct item {
    int val;
    int *vectOfInt;
    struct item *next;
};

void relItem(struct item **currItem) {
    struct item *temp;
    int *intTemp;

    temp = *currItem;
    intTemp = (*currItem)->vectOfInt;
    *currItem = (*currItem)->next;
    free(temp);
    free(intTemp);
}

int main() {
    int array[] = {0, 1, 2, 3, 4, 5};
    struct item *list = NULL;

    list = (struct item*) malloc(sizeof(struct item));
    list->val = 0;
    list->vectOfInt = array;
    list->next = NULL;

    relItem(&list);

    return 0;
}

EDIT: code for the comment:

struct item {
    int val;
    struct item *next;
};

void edit(struct item *currItem) {
    currItem->val = 2;
}

int main() {
    struct item *list = NULL;
    list = (struct item*) malloc(sizeof(struct item));
    list->val = 0;
    list->next = NULL;

    edit(list);

    //list-val == 2

    return 0;
}
  • How can I do the same thing without using double-pointer to the struct?

  • Can you explain me why and how this works (both with pointer and double-pointer)?

  • I don’t understand how a struct is represented in main memory
    (for example int a[5]; in memory a is a pointer to the first location of the buffer allocated for the a[5] array)

what is the equal representation for a struct initialized wit ha pointer
(struct item *s) ?

  • 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-24T04:47:59+00:00Added an answer on May 24, 2026 at 4:47 am

    -You need to use double pointer since you need to change the pointer to your struct and call by reference is not allowed in C.

    -It would not work with a single pointer. Because you want to change the pointer to your object. You do it by the statement *currItem = (*currItem)->next;
    In order to change it permanently, you need to use a pointer to it. Which makes you use double pointer.

    Think of it that way:

    you have an integer variable a that you want a function to change its value. You simply call this function with a pointer to the variable a
    like:

    void changeTheValue(int *x)
    {
        *x = 7;
    }
    void main()
    {
       int a = 5;
       changeTheValue(&a);
    }
    

    and in your case you want to change your pointer’s value and you simply pass its pointer to the function.(double pointer)
    simple as that. If you want to change value of something with a function, then you have to pass its pointer to the function.

    -When you make a call to malloc, you want space from it. And you state that you want a space as big as the size of your struct. (Like when you did in here list = (struct item*) malloc(sizeof(struct item));) and malloc allocates a space as big as your struct. If size of your struct is 1 byte then you have a 1 byte space, if it is 4, then you have consecutive 4 bytes.
    See, this is how your struct is kept in the memory.
    If you declare a struct variable or an array etc. then your struct is kept in memory like (dunno if it is a good metaphor) an array. The first member comes first, and then the second…

    say if you have a struct

    struct myStruct
    {
        int a;
        float b;
        char c;
    };
    

    then the memory looks like


    a


    b


    c

    ps: you are making an illegal call to free on the line free(intTemp); you are *free*ing a variable that you didn’t *malloc*ed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have made a little piece of code that will insert and manipulate
I made this little function from code snippets around the net. It does what
I made this little Cocoa app with a WebView that displays Google Maps in
Made this nice little loop for hiding and showing div's, works as a charm
I made this code with jQuery to fade images ( but not the one
I made this method + (CGFloat) round: (CGFloat)f { int a = f; CGFloat
I'm brand-spanking new to Java and I made this little translator for PigLatin. package
I made this little function: public String getDay() { String day = (String)android.text.format.DateFormat.format(E, new
I made this question in this if then else to display 2 views on
So, I have this nice little view that I've made, which basically shows two

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.