Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Searching and displaying a record in Array of Structure in c

Searching and displaying a record in Array of Structure in c

Scheduled Pinned Locked Moved C / C++ / MFC
databasealgorithmsdata-structureshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14979108
    wrote on last edited by
    #1

    Hi all, PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help

    struct employee{
    int emp_id; //empoyee_id int
    char emp_name[30];
    }emp[MAX];

    void findAllDetails(int *e){

    printf("\n Employee Details...\n");
    printf("\n Emp No : %d", emp.emp_id);
    printf("\n Emp Name : %s", emp.emp_name);

    }
    void PrintAllDetails(int &K){

    for(i=0;i
    
    CPalliniC 1 Reply Last reply
    0
    • M Member_14979108

      Hi all, PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help

      struct employee{
      int emp_id; //empoyee_id int
      char emp_name[30];
      }emp[MAX];

      void findAllDetails(int *e){

      printf("\n Employee Details...\n");
      printf("\n Emp No : %d", emp.emp_id);
      printf("\n Emp Name : %s", emp.emp_name);

      }
      void PrintAllDetails(int &K){

      for(i=0;i
      
      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      I would have written something similar to

      #include #define EMPLOYEE_ARRAY_SIZE 10
      #define NAME_MAX_LENGTH 30
      #define NOT_FOUND -1

      struct employee
      {
      int id;
      char name[NAME_MAX_LENGTH];
      };

      int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id);
      void print_employee( const struct employee * pemp );

      int main()
      {
      struct employee emp_array[EMPLOYEE_ARRAY_SIZE] =
      {
      { 1, "foo"}, {2, "boo"}, {3, "goo"}, // .. other items here
      };

      int id = 2;

      int index = find_employee_by_id( emp_array, EMPLOYEE_ARRAY_SIZE, id );

      if ( index != NOT_FOUND )
      {
      printf("found emplyee details:\n");
      print_employee( & emp_array[index] );
      }
      else
      {
      printf("employee with id = %d not found\n", id);
      }

      return 0;
      }

      int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id)
      {
      int index;
      for (index = 0; index < emp_array_size; ++ index)
      if ( emp_array[index].id == id)
      return index;
      return -1;
      }

      void print_employee( const struct employee * pemp )
      {
      printf("id = %d\n", pemp->id);
      printf("name = %s\n", pemp->name);
      }

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups