1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <stdio.h>
/* Returns the number of addresses corresponding to the family name name.
* name must be a pointer to a string.
*/
int nb_occ(char* name);
/* When several addresses match de same family name,
* we refer to each of them seperataly by using their index.
* This function stores a unique address identified by name and index,
* in the structure pointed to by address.
* Valid values for index are from 0 to nb_occ(name)-1.
* address must be a valid pointer to a structure to be filled by this function.
* name must be a pointer to a string.
* Returns 0 in case of success, or -1 in case of error.
* In an error occurs, errno is set to the appropiate error code.
*/
int get_addr_at(char* name, int index, struct addr* address);
/* Creates an array containing all the addresses mayching with the name name.
* A pointer to the created array is stored inside addresses.
* name must be a pointer to a string.
* Returns the number of stored addresses in case of succes,
* or -1 in case of failure.
*/
int get_addr_array(char* name, struct addr** addresses);
/* Release the memory associated to the address array addresses.
* Returns 0 in case of success, -1 in case of error.
*/
int free_addr_array(struct addr** addresses);
struct addr{
char first_name[50];
char last_name[50];
char city[50];
}
int get_addr_array(char* name, struct addr** addresses){
int index, numberAdd;
index = numberAdd = nb_occ(name);
index -= 1;
*addresses = (struct addr *)malloc(nb_occ(name)*sizeof(numberAdd*sizeof(struct addr));
while(get_addr_at(name, index, addresses) == 0){
(*addresses)++;
index--;
}
if (index == 0) return numberAdd;
else return -1;
}
int free_addr_array(struct addr **addresses){
struct addr *memory = *adresses;
(*addresses)++;
free(memory);
}
main(){
struct addr *addresses
get_addr_array("Smith", addresses);
free_addr_array(adresses);
return 0;
} |
Partager