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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:03:27+00:00 2026-06-14T07:03:27+00:00

Given a structure array (in C) I am attempting to print out the results

  • 0

Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example:

struct employee{
char gender[13]
char name[13];
int id;
};

Say I define the structure array like so:

struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

How could I go about printing the results like

1234 Matt
1235 Josh


2345 Jessica
  • 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-06-14T07:03:29+00:00Added an answer on June 14, 2026 at 7:03 am

    You’ll need to implement a sorting function that compares the structs as you require

    int compare(const void *s1, const void *s2)
    {
      struct employee *e1 = (struct employee *)s1;
      struct employee *e2 = (struct employee *)s2;
      int gendercompare = strcmp(e1->gender, e2->gender);
      if (gendercompare == 0)  /* same gender so sort by id */
        return e1->id - e2->id;
      else
        return -gendercompare;  /* the minus puts "male" first as in the question */
    }
    

    And then use qsort from the standard library.

    qsort(data, count, sizeof(struct employee), compare);
    

    Inside the compare function you may want to check for id being equal, then you can sort by name (also using strcmp()) however you like.

    Edit: Just compiled and fixed this up. Here’s a little test program

        #include <stdio.h>
        #include <stdlib.h>
    
        struct employee{
          char gender[13];
          char name[13];
          int id;
        };
    
        int compare(const void *s1, const void *s2)
        {
          struct employee *e1 = (struct employee *)s1;
          struct employee *e2 = (struct employee *)s2;
          int gendercompare = strcmp(e1->gender, e2->gender);
          if (gendercompare == 0)  /* same gender so sort by id */
            return e1->id - e2->id;
          else
            return -gendercompare;
        }
    
        main()
        {
          int i;
          struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
    
          for (i = 0; i < 3; ++i)
            printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
    
          qsort(info, 3, sizeof(struct employee), compare);
    
          for (i = 0; i < 3; ++i)
            printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
        }
    

    With output:

    $ ./a.exe
    1234    male    Matt
    2345    female  Jessica
    1235    male    Josh
    1234    male    Matt
    1235    male    Josh
    2345    female  Jessica
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have structure containing an array. Something like given below: struct Node { int
I am trying to convert an array of the RECT structure (given below) into
Given two structure in c: typedef struct _X_ { int virtual_a; int virtual_b; void
How best would I exclude results from #other_section, given a structure like this? <form>
Given that $struct is a data structure that can be either a hash or
Given this XML structure from (http://localhost:3000/route.xml): <objects type=array> <object> <trip0> <departure>20:01</departure> <start>Place a</start> <end>Place
Given a multi-dimensional array that you don't necessarily know the structure of; how would
Suppose there's a list of arguments stored somehow, in a array for example. Given
A novice array question... Given an array with this structure: $post_dates Array [1] [0...0]
Is there a function out there to make sure that any given array conforms

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.