We can create our own custom data types like how we can create our own functions.
Let’s create a person
data type to bind a name and number together!
typedef struct
{
;
string name;
string number}
; person
Now we can re-implement the phonebook:
// Linear Search Through Names
[2];
person people[0].name = "Carter";
person[0].number = "+1-617-495-1000";
person[1].name = "David";
person[1].number = "+1-949-468-2750";
personfor (int i = 0; i < 2; i++) {
if ((strcomp(people[i].name, "David") == 0) { // Found
("Found %s". people[i].number);
printfreturn 0;
}
}
return 1; // Not Found
struct
because it still relies on an honor system (less encapsulation).