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

  • Home
  • SEARCH
  • 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 9135155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:15+00:00 2026-06-17T08:47:15+00:00

typedef struct list { struct list * next; int val; }*list_t; list_t add(list_t l,int

  • 0
typedef struct list
{
    struct list * next;
    int val;
}*list_t;    

list_t add(list_t l,int e)
{
    list_t head;

    if(l == NULL)
    {
        l = malloc(sizeof(list_t));
        l->val = e;
        l->next = NULL;
        return l;
    }       

    head = l;

    while(l != NULL)
        l=l->next;

    l = malloc(sizeof(list_t));

    l->val = e;

    l->next = NULL;

    return head;
}

Sample driver:

int main()
{
    list_t ints=NULL;
    int i;

    for(i=0;i<156;i+=2)
        ints = add(ints,i);

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

    system("pause");
    return 0;
}

Program works, but “add” function rewinds the list so that the body of main’s loop is never achieved.It surprised me a lot, because I thought that I’d been passing list as a value! Could you explain this phenomenom?

  • 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-17T08:47:15+00:00Added an answer on June 17, 2026 at 8:47 am

    The problem is not that the add function rewinds the list, is that it’s not working at all: Nowhere in your code are you stating that the previous end of the list should link to the newly added element.

    I’ve modified it slightly:

    typedef struct list
    {
        struct list * next;
        int val;
    } list_t;    
    
    list_t *add(list_t *l,int e)
    {
        list_t *head;
    
        if(l == NULL)
        {
            l = malloc(sizeof(list_t));
            l->val = e;
            l->next = NULL;
            return l;
        }       
    
        head = l;
    
        while(l->next != NULL)
            l=l->next;
    
        l->next = malloc(sizeof(list_t));
    
        l=l->next;
    
        l->val = e;
    
        l->next = NULL;
    
        return head;
    }
    
    int main()
    {
        list_t *ints=NULL;
        int i;
    
        for(i=0;i<156;i+=2)
            ints = add(ints,i);
    
        while(ints->next != NULL)
        {   
            printf("%d\n",ints->val);
            ints=ints->next;
        }
    
        return 0;
    }
    

    The code now works as expected.

    Remember that l is a local variable in the add function. Any changes made to l will be lost if it’s not allowed to leave the scope of the function somehow (like you do when you return it, inside the first if). Changes made to the variable l points to, using either the * or the -> operators, will be effective to whoever has access to that variable.

    I recomend that you start reading on debugging techniques. They vary depending on your environment, and can go from cryptic commandline tools like gdb to full-fledged graphical object browsers and such. This way you will be able to see what happens step by step and monitor memory changes and check what’s really being stored in your variables.

    EDIT: Fixed pointer trouble as commented. Memory allocations now provide for the whole struct variable, and pointers are no longer used implicitly.

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

Sidebar

Related Questions

struct Node { int value Node* next; } typedef List Node* const Set operator
#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
typedef struct child_list {int count; char vo[100]; child_list*next;} child_list; typedef struct parent_list { char
I have a two struct, one is linked list. typedef struct Mark{ int people;
// Variables typedef struct node { int value; struct node *next; }mynode; // Globals
//file list.h #include stdafx.h namespace st { struct My_List; typedef My_List list; list* create(const
Consider the following: typedef struct { int a; int b; int c; int d;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
so usually this typedef makes code cleaner: typedef struct { int x; } X;

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.