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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:27:53+00:00 2026-05-13T00:27:53+00:00

I have a sorted struct of IPs where I need to get the number

  • 0

I have a sorted struct of IPs where I need to get the number of unique IPs, for some reason the way I’m doing it, is giving me a “0” as a result. Which in this case there should be 12 unique ips.

The struct array containing the following elements:

195.55.121.242
212.80.168.34 
65.55.106.114 
65.55.207.30  
65.55.207.95  
65.55.230.237 
66.249.68.16  
66.249.68.16  
66.249.68.16  
67.195.37.172 
67.195.37.172 
67.218.116.162
80.59.182.176 
80.59.182.176 
83.213.81.220 
83.213.81.220 
83.43.21.186  
83.43.21.186 

Code:

typedef struct {
    char *ip;
}thestruct;

qsort(mystruct, 18, sizeof(thestruct*), cmpme);

int un = 0;
for (i=0; i<18; i++) {
    if (strcmp(mystruct[i++]->ip,mystruct[i]->ip)!=0) {
        un++;
    }
}

By doing a simple gets-strcmp with only one element (ip) I get that both strings are equal. Which tells me that strcmp is treating it as a string.

I’m not quite sure what I am missing.

Any help will be appreciate it.

Thanks

  • 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-13T00:27:53+00:00Added an answer on May 13, 2026 at 12:27 am

    Look carefully at this line:

    if (strcmp(mystruct[i++]->ip,mystruct[i]->ip)!=0)
    

    You’re comparing index i to index i (which are equal, since they’re the same) and then incrementing i. (Actually, this is undefined behavior, since you’re modifying i and reading it more than once before a sequence point).

    You really want to do this:

    if (strcmp(mystruct[i + 1]->ip,mystruct[i]->ip)!=0)
    

    to compare index i+1 to index i without touching i, since i is incremented in the for loop. Also, i should only loop from 0 to 17, not 0 to 18, since you don’t want to read past the end of the array.

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

Sidebar

Related Questions

I have some files that need to be sorted by name, unfortunately I can't
How would I go about this, I have files which I have sorted the
I have a vector of custom Struct that needs to be sorted on different
I have some data that is stored in a sorted vector. This vector is
I have some C code that is something of a puzzle. For a reason
E.g., if you have a sorted array of the struct: struct Item { int
I have the following code to merge two sorted linked lists: struct node* merge(struct
I have a function that needs to return a sorted list based on some
I have sorted CFArray, and I need to find the index of a value.
From cplusplus.com reference it seems that std::set sorts elements. I need to have sorted

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.