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 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
  • 2 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

Related Questions

I went to this interview for a software developer position and they gave me
I was asked this question during an interview. They're both O(nlogn) and yet most
I just got this question on a SE position interview, and I'm not quite
In an interview for a SQL DBA position a while back, I mentioned I
This was an interview question. Given Visual Studio 2008 and an icon saved as
I was browsing Scott Hanselman's Developer Interview question list , and ran across this
I was recently on an interview for a position where C/C++ is the primary
In an interview a while ago for a .NET position the interviewer asked me
Two days ago I was asked this question in an interview for Data Analyst
So I have an interview next week for a shop that makes heavy use

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.