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

The Archive Base Latest Questions

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

I was at an interview for a C position in which they presented me

  • 0

I was at an interview for a C position in which they presented me with an idiom that I haven’t previously encountered. This is a trick that simplifies implementation of various algorithms involving linked lists and I’m wondering if anybody else has encountered this.

Say we have a linked list record defined so:

typedef struct _record {     char* value;     struct _record* next; } record; 

We need a function that inserts a new record so that the entire list remains sorted with respect to the value’s in the records. The following implementation is simpler than anything I would have used, albeit less readable.

void insert_sorted(record** r, const char* value) {     record* newrec = NULL;     while(*r && strcmp(value, (*r)->value) > 0)         r = &((*r)->next); /* move r to point to the next field of the record */     newrec = malloc(sizeof(record));     newrec->value = strdup(value);     newrec->next = *r;     *r = newrec; } 

When the function is called, r points to the head pointer of the list. During the while loop, r is updated to point to the next field of the record that comes just before the point where we want to put the new record in. The last line of the function either updates the head pointer of the list (if the insertion happens at the beginning) or the next field of the previous record, which is quite cool.

A couple of questions:

  • Does this idiom have a name or is it mentioned in any literature?

  • Are there others like it in the C language?

I thought I knew C pretty well and had pointers and indirection pretty well figured out, but this one took me a while to fully understand.

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

    I’ve used similar to this to insert into a binary tree. Because when iterating the tree, you usually stop when your pointer becomes NULL (you ran off the tree).

    So to insert, you have 3 options,

    1: use a variable which tracks the previous value of your iterating pointer.

    2: stop when the pointer you would follow is NULL before you follow it, works but slightly less elegant in my opinion.

    3: or a more elegant solution is simply use a pointer to a pointer, so you can just do: *it = new_node(); and it’ll add it where the NULL used to be in your tree.

    For a linked list, while this code works nicely, I usually just use a doubly linked list which makes it trivial to insert at any location.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's a javascript snippet trying to exploit a security vulnerability… May 11, 2026 at 12:39 pm
  • added an answer You can host the Flash plugin natively using the NPAPI,… May 11, 2026 at 12:39 pm
  • added an answer The DTO's provide an abstraction layer for your domain model.… May 11, 2026 at 12:39 pm

Related Questions

I was looking at the System.Net Namespace and it has an IPAddress instance you
I was looking at the Proxy Pattern, and to me it seems an awful
I'm looking at building an API and was considering oauth for managing access to
Here I am faced with an issue that I believe(or at least hope) was
This morning, I was reading Steve Yegge's: When Polymorphism Fails , when I came
In a past interview, I was asked how would I write a mission critical
Dealing with an employee that went over my head
A friend of mine was recently asked in a job interview to tell the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.