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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:52:54+00:00 2026-06-16T06:52:54+00:00

I’m having a little trouble with linked lists. I don’t understand why this code

  • 0

I’m having a little trouble with linked lists. I don’t understand why this code will traverse and print the linked list with no issue:

struct foo {
    int data;
    struct foo * next;
};

int main(void) {
    struct foo * bar = NULL;
    ...
    print(bar);
}

void print(struct foo * bar) {
    while (bar != NULL) {
        printf("%d, bar->data);
        bar = bar->next;
    }
}

Yet when I place the list inside another structure, like an array, the list gets destroyed during traversal because pointers get reassigned at bar[i] = bar[i]->next:

struct foo {
    int data;
    struct foo * next;
};

int main(void) {
    struct foo ** bar = (struct foo**)malloc(SIZE * sizeof(struct foo*));
    for (i = 0; i < SIZE; i++)
        bar[i] = NULL;
    ...
    print(bar);
}

void print(struct foo ** bar) {
    for (i = 0; i < SIZE; i++)
        while (bar[i] != NULL) {
            printf("%d, bar->data);
            bar[i] = bar[i]->next;
        }
}

Why does this happen? I know a proper way to write the print function in this scenario would be:

void print(struct foo ** bar) {
    struct foo * helper;
    for (i = 0; i < SIZE; i++)
        for (helper = foo[i]; helper != NULL; helper = helper->next)
            printf("%d", helper[i]->data);
}

I just want to understand why. Why do pointers not get reassigned in the first scenario, but do on the second? I assume it’s something to do with passing values vs passing references, but that would mean the first function destroys the list too. Can anyone offer some insight?

  • 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-06-16T06:52:55+00:00Added an answer on June 16, 2026 at 6:52 am
    void print(struct foo* bar)
    {
        while (bar != NULL)
        {
            printf("%d, bar->data);
            bar = bar->next;
        }
    }
    

    This function takes a copy of a pointer to struct foo as a first argument. No matter what the function does with it, I am sure that the original pointer will not be modified, since I’m just working with a copy here. The value that the pointer points to on the other hand, may be modified.

    void print(struct foo** bar)
    {
        for (i = 0; i < SIZE; i++)
            while (bar[i] != NULL)
            {
                printf("%d, bar->data);
                    bar[i] = bar[i]->next;
            }
    }
    

    This function takes a copy of a pointer to pointer to struct foo as a first argument. Once again, the original pointer to pointer cannot be modified from here, the value which it points too on the other hand, which is a pointer to foo here, may be modified. Of course, the value pointed by the pointer pointed by bar, can also be modified.

    If you’ve understood this correctly, you should realize why passing struct something instead of struct something* to a function, is a bad idea performance-wise.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build

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.