Help on understanding C structs
#include<stdio.h> typedef struct telephone { char *name; int number; }TELEPHONE; int main() { TELEPHONE index; index.name = "Jane Doe"; index.number = 12345; printf("Name: %s\n", index.name); printf("Telephone number: %d\n", index.number); return 0; } Say I have something like this, how would adding more names and their associated phone numbers work? Every new name I add would replace the old one right?
ur not taking any input for the name? how do u expect to add names then?
Say I had a way to input names, how would I make the struct accept multiple names and their associated phone number? Because the way I see it, it only accepts one single name and number right now.
struct declare structures. Except for access control, they're the same as classes in object oriented languages. Each instance of a struct (index is one TELEPHONE) holds the attributes of one (in this case) telephone. If you want to have many telephones, you'll probably want an array of structs (struct pointers).
Join our real-time social learning platform and learn together with your friends!