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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:08:06+00:00 2026-05-24T01:08:06+00:00

I am goofing around with pointers and structures. I want to achieve the following:

  • 0

I am goofing around with pointers and structures. I want to achieve the following:
(1) define a linked list with a structure (numberRecord)
(2) write a function that fills a linked list with some sample records by going thourgh a loop (fillList)
(3) count the number of elements in the linked list
(4) print the number of elements

I am now so far that the fillList function works well, but I do not succeed in handing over the filled linked list to a pointer in the main(). In the code below, the printList function only displays the single record that was added in main() instead of displaying the list that was created in the function fillList.


#include <stdio.h>
#include <stdlib.h>

typedef struct numberRecord numberRecord;

//linked list
struct numberRecord {
             int number;
        struct numberRecord *next;
};

//count #records in linked list
int countList(struct numberRecord *record) {

         struct numberRecord *index = record;
    int i = 0;

    if (record == NULL)
        return i;

    while (index->next != NULL) {
        ++i;
        index = index->next;
    }

    return i + 1;
}

//print linked list
void printList (struct numberRecord *record) {

    struct numberRecord *index = record;

    if (index == NULL)
        printf("List is empty \n");

    while (index != NULL) {

        printf("%i \n", index->number);
        index = index->next;
    }

}

//fill the linked list with some sample records
void fillList(numberRecord *record) {

    numberRecord *first, *prev, *new, *buffer;

//as soon as you add more records you get an memory error, static construction
    new = (numberRecord *)malloc(100 * sizeof(numberRecord));
    new->number = 0;
    new->next = NULL;

    first = new;
    prev = new;
    buffer = new;

    int i;

    for (i = 1; i < 11; i++) {

        new++;

        new->number = i;
        new->next = NULL;

        prev->next = new;
        prev = prev->next;
    }

    record = first;
}


int main(void) {

    numberRecord *list;
    list = malloc(sizeof(numberRecord));
    list->number = 1;
    list->next = NULL;

    fillList(list);
    printf("ListCount: %i \n", countList(list));
    printList(list);
    return 0;
}

SOLUTION
Do read the posts below, they indicated this solution and contain some very insightful remarks about pointers. Below the adapted code that works:

#include <stdio.h>
#include <stdlib.h>

typedef struct numberRecord numberRecord;

//linked list
struct numberRecord {
             int number;
        struct numberRecord *next;
};

//count #records in linked list
int countList(struct numberRecord *record) {

         struct numberRecord *index = record;
    int i = 0;

    if (record == NULL)
        return i;

    while (index->next != NULL) {
        ++i;
        index = index->next;
    }

    return i + 1;
}

//print linked list
void printList (struct numberRecord *record) {

    struct numberRecord *index = record;

    if (index == NULL)
        printf("List is empty \n");

    while (index != NULL) {

        printf("%i \n", index->number);
        index = index->next;
    }

}

//fill the linked list with some sample records
 numberRecord *fillList() {

    numberRecord *firstRec, *prevRec, *newRec;

    int i;

    for (i = 1; i < 11; i++) {

        newRec = malloc(sizeof(numberRecord));
        newRec->number = i;
        newRec->next = NULL;

        //initialize firstRec and prevRec with newRec, firstRec remains head
        if (i == 1) {
            firstRec = newRec;
            prevRec = newRec;
        }
        prevRec->next = newRec;
        prevRec = prevRec->next;
    }

    return firstRec;
}


int main(void) {

    numberRecord *list;
    list = fillList();

    printf("ListCount: %i \n", countList(list));
    printList(list);
    return 0;
}
  • 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-24T01:08:07+00:00Added an answer on May 24, 2026 at 1:08 am

    This statement in fillList

    record = first;
    

    has no effect on the list variable in main. Pointers are passed by value (like everything else) in C. If you want to update the list variable in main, you’ll either have to pass a pointer to it (&list) and modify fillList accordingly, or return a numberRecord* from fillList. (I’d actually go with that second option.)

    Here’s a (bad) illustration:

    When main calls fillList, at the starting point of that function, the pointers are like this:

    main        memory       fillList
    list ----> 0x01234 <----  record
    

    A bit later in fillList, you allocate some storage for new (that’s actually a bad name, it conflicts with an operator in C++, will get people confused)

    main        memory       fillList
    list ----> 0x01234 <----  record
               0x03123 <----  new
    

    At the last line of fillList you’re left with:

    main        memory       fillList
    list ----> 0x01234   ,--  record
               0x03123 <----  new
    

    record and list are not the same variable. They start out with the same value, but changing record will not change list. The fact that they are both pointers doesn’t make them any different from say ints in this respect.

    You can change the thing pointed to by list in fillList, but you can’t change what list points to (with your version of the code).

    The easiest way for you to get around that is to change fillList like this:

    numberRecord *fillList() {
      ....
      return new;
    }
    

    And in main, don’t allocate list directly, just call fillList() to initialize it.

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

Sidebar

Related Questions

There are a number of places in the perl documentation that warn against using
Given I have an array as follows: $array = array('a', 'b', 0, 'c', null,
I used a modified version of the code here Determining what classes are defined
I have a sample query as shown below: SELECT * FROM [#temp1] UNION SELECT
I've made this method here: def get_api_xml_from_url(url, params = {}, api_key = SharedTest.user_api_key) response

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.