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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:10:28+00:00 2026-05-17T21:10:28+00:00

Hey guys. This is a very simple question, I’m sure, but I’m getting myself

  • 0

Hey guys. This is a very simple question, I’m sure, but I’m getting myself tangled up in C references/pointers as per usual. I am trying to build a… sort-of-queue, using a sort-of-linked list. Basically, I have a struct which has contents and a pointer to the next element. I also have a pointer to the first and last elements. I then have a loop that will be building the ‘sort-of-queue’. My problem is that either my logic is failing and I’m not initialising the queue right, or my knowledge of C structs is failing (which is very probable) and I’m ending up just creating one struct and constantly referring to it.

My test code is as follows:

#include <stdio.h>

struct test {
    int contents;
    struct test *next;
};

main() {

    struct test *first = NULL;
    struct test *last = NULL;
    int i;

    for (i = 0; i < 2; i++) {
        struct test tmp;
        if (first == NULL) {
            first = &tmp;
            last = &tmp;
        } else {
            last->next = &tmp;
            last = &tmp;
        }
        tmp.x = i;
        tmp.next = NULL;
    }

    while (first != NULL) {
        printf("%d\n", first->x);
        first = first->next;
    }

    return 0;

}

Running this, I get the output that first seems to point to a test struct that has the value of ‘1’ as it’s ‘x’ variable – so not the initial one like I intended. So, am I failing at logic here, or am I failing at understanding how to declare new separate structs in a loop? Or maybe both? I’m very tired… >_<.

Thanks.

  • 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-17T21:10:28+00:00Added an answer on May 17, 2026 at 9:10 pm

    The problem you have is that you are taking the address of a temporary variable, tmp, and assigning it to a pointer which lives much longer than teh temporary, first and last. After every iteration of the loop the temporary is gone and continuing to access it via first and last results in undefined behavior.

    You need to create a value on the heap in order to build up the list like so (error checking omitted for brevity)

    struct test* tmp = malloc(sizeof(struct test));
    

    Later though you’ll need to go through and free all of the allocated nodes.

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

Sidebar

Related Questions

hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys, I taught myself PHP a few years back and am now in
Hey guys. I am currently using a very inefficient script to validate my forms.
Hey guys, XDocument is being very finicky with one of the xml feeds I
Hey guys, i have the following problem: 1 process executes a very large query
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey guys -- The website I'm working on requires a bookmarklet launchable from your
Hey, I am working on a simple Plugin for CKEditor, I am not to

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.