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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:56:52+00:00 2026-05-14T15:56:52+00:00

I have the following array structure (linked list): struct str_pair { char ip [50]

  • 0

I have the following array structure (linked list):

    struct str_pair   
     {  
       char ip  [50] ;  
       char uri [50] ;  
       str_pair *next ;  
     } ;

str_pair *item;

I know to create a new item, I need to use

item = new str_pair;

However, I need to be able to loop through the array and delete a particular item. I have the looping part sorted. But how do I delete an item from an array of structures?

  • 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-14T15:56:52+00:00Added an answer on May 14, 2026 at 3:56 pm

    What you’ve shown is not an array of struct, but a linked list of struct containing arrays (of type char).


    An array of struct would look like this:

    str_pair array_of_structs[10];
        // or:
    str_pair* dynamically_allocated_array_of_structs = new str_pair[10];
    

    If you actually have something like this, you don’t need to delete single items from an array. Let’s say you’ve initialized your array as follows:

    str_pair* array_of_structs = new str_pair[10];
    

    Then you delete the whole array (including all of its items) using:

    delete[] array_of_structs;
    

    Again, you can’t delete single items in an array allocated with new[]; you perform a delete[] on the whole array.


    If, on the other hand, you intended to say “linked list of struct“, then you’d generally delete an item similarly to the following:

    str_pair* previous_item = ...;
    str_pair* item_to_delete = previous_item->next;
    
    if (item_to_delete != 0)
    {
        previous_item->next = item_to_delete->next;  // make the list "skip" one item
        delete item_to_delete;                       // and delete the skipped item
    }
    

    Or, in English: Find the item (A) preceding the item which you want to delete (B), then adjust A‘s “next” pointer so that B will be skipped in the list, then delete B.

    You need to be careful with special cases, i.e. when the item to be removed from the list is the first item or the last one. The above code is not sufficient when you want to delete the first item in the list, because there will be no previous_item. In this case, you’d need to change the pointer to the list’s first element to the second element.


    Your code:

    void deleteitem(char *uri)
    {
        str_pair *itemtodelete;
        curr = head;
    
        while (curr->next != NULL) {
        if ((strcmp(curr->uri, uri)) == 0) {
            itemtodelete = curr;
            curr = itemtodelete->next;
            delete itemtodelete;
    
            curr = head;
            return;
        }
    
        curr = curr->next;
        }
    }
    

    Some things are wrong here:

    • If head is null, the test curr->next != NULL will cause a segfault. (You must never dereference a null pointer!)

    • Your code for removing an item from the list is completely incorrect. Worst of all, you delete a node without changing the previous item’s next pointer. The previous item will thus reference an item that’s no longer there.

    • A detail: curr = head; before the return statement doesn’t do anything useful at all.

    Suggested code:

    Do it in two steps: One function to find the node to be deleted via its attached uri, and one function to remove the node. You could separate it even better than the code below does, but it should be a starting point:

    str_pair* finditemwithuri(char* uri)
    {
        str_pair* current = head;
        while (current)
        {
            if (strcmp(current->uri, uri) == 0) return current;
            current = current->next;
        }
        return 0;
    }
    
    void deleteitem(char* uri)
    {
        // find linked list node with that uri; abort if uri not in list
        str_pair* itemtodelete = finditemwithuri(uri);
        if (!itemtodelete) return;
    
        // special case: node to be deleted is the list's head
        if (itemtodelete == head)
        {
            head = itemtodelete->next;
            delete itemtodelete;
            return;
        }
    
        // else, iterate over list nodes
        // up to the one preceding the node to be deleted
        str_pair* current = head;
        while (current)
        {
            if (itemtodelete == current->next)
            {
                current->next = itemtodelete->next;
                delete itemtodelete;
                return;
            }
            current = current->next;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As of version 5.3 The __toString magic method can no… May 15, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer I don't know about programming for AutoCAD, but I'd suspect… May 15, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer Yes it is! Ive actually done something similiar but went… May 15, 2026 at 7:27 pm

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.