当前位置: 动力学知识库 > 问答 > 编程问答 >

C: how would I write a search function to look for a match in a struct array, and return (print) the entire struct it matched?

问题描述:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define RECORDS 10

The function below is what I am asking for help with.

static char searchforRecordbystate(char input[3])

{

for / while /if loop

search struct array members

if a match is found

return (print) the entire struct where a match was found

 return 0;

}

Main function - first time ever using pointers, (xcode is not complaining with it set to be as strict as possible) but all of you are welcome to complain, especially if I am making a huge oversight.

int main() {

typedef struct {

char *firstName[RECORDS];

char *lastName[RECORDS];

char *street[RECORDS];

char *city[RECORDS];

char *state[RECORDS];

int *zip[RECORDS];

char *phone[RECORDS];

int *accountId[RECORDS];

} Customer ;

typedef int records;

records i = 0;

array loop for data entry

 Customer custArray[RECORDS];

printf("================================================================\n");

for(i = 0; i < RECORDS; ++i)

{

printf("Enter data for customer %d\n", i + 1);

printf("Enter firstname, last name, phone\n");

scanf("%s %s %s", *custArray[i].firstName, *custArray[i].lastName, *custArray[i].phone);

printf("Enter Address (Street City State ZIP)");

scanf("%s %s %s*c %d", *custArray[i].street, *custArray[i].city, *custArray[i].state, *custArray[i].zip);

break;

}

char input[3];

printf("Enter in state to search for customer a customer record:\n");

scanf("%s", input);

searchforRecordbystate(input);

}

No error checking necessary, just trying to crawl into learning c at the moment. And there will not be duplicate data in the state member. Hope that makes this easier.

网友答案:

how would I write a search function to look for a match in a struct array and return (printf) the entire struct it matched?

  1. Declare the struct datatype outside of the function so it's "visible" to the whole module.
  2. Create a function that is able to pretty-print a struct:

    void CustomerPrint(const Customer *toPrint) { ... }

  3. Create a search function that iterates through the array comparing given arguments:

    Customer *CustomerFind(const char *name) { ... }

  4. Connect the two function blocks by calling CustomerFind and in case the result is not NULL call the CustomerPrint function.

Of course the interfaces are only proposal and are subject to be changed. If you've got any questions regarding the details of the proposal leave a comment, I'll explain it in great detail if you like.

Additional thoughts

While rereading my post I realized that some of my decisions I've made in above proposal need an explaination anyway:

In CustomerPrint the pointer taken is `const? because this function is not going to modify any field of the struct. Therefore we tell the compiler that we are not going to change anything.

CustomerFind is expected to have arguments for all searchable fields. (So you are encouraged to extend the signature) I'd propose to take all the "compare" values by pointer and let the caller those pointers be NULL which are not relevant for the search. (e.g. if you have name and city you can leave city NULL in order to only search for the first occurence of name.

The function itself runs through the array of records and compares the fields that are not NULL. In case it finds one, it returns the pointer to that element (return &(myRecords[n]);). If the function comes to the end of the array, it will return NULL to indicate no record matched.

There is also a concept you can introduce if you want to have "search - search next" capabilities. Let me know if you are intrested in a concept for that too.

网友答案:
typedef struct {
    char firstName[NAMEMAX];
    char lastName[NAMXMAX];
    char street[STREETMAX];
    char city[CITYMAX];
    char state[STATEMAX];
    int  zip;
    char phone[PHONEMAX];
    int  accountId;
} Customer ;

Customer Customers[RECORDS];  

static int searchforRecordbystate(char input[]) {
  for (int i = 0; i < RECORDS; i++) {
    if (strcmp(input, Customers[i].state) == 0) {
      printCustomer(Customers[i]);
      return i;
    }
  }
  return -1; // Not found
}

Writing printCustomer() is an exercise for the reader.

分享给朋友:
您可能感兴趣的文章:
随机阅读: