About structure array

2010-05-02 8:50 am
I have a structure array is as following:
struct contact {
char *name;
char *phone;
char *address;
}contactList[1024];
My question asked for me about how to add content to structure array from the function of addContact
The parameters for this fuction are as following:
addContact(char* name, char* phone, char* address)
My problem is I can add the content to structure array, but only the contact record in array[0], even I have add the pointer for jumping to next row

The function I wrote as following:

void addContact(char* name, char* phone, char* address) {
int i;
struct contactList *ptr=contactList;

for (i=0; i<4; i++) {

ptr[i].name=name;

ptr[i].phone=phone;
ptr[i].address=address;

*ptr++;
contactCount++;
}
Another problem is I can only get the value in array[0]( total 1 record can be returned), and even added the pointer here for jumping to next row

The function I wrote as following:
char* getPhoneOfName(char* name) {
int i;

struct contactList *ptr=contactList;
for (i=0; i<3; i++){
if (ptr->name==name){

return ptr->phone;
*ptr++; }
else return NULL;
}
}
in the main()
There are the data to be added to structure array using the function addContact
addContact("Bendy", "10836588", "22 Tai Cheung Street, Tai Ko");
addContact("May", "55769943", "59 Hai Tong Road, Aberdeen");
addContact("June", "10599813", "1C Fung Yee Street, Wan Chai");

printf("getPhoneOfName = %s\n", getPhoneOfName("Bendy"));
printf("getPhoneOfName = %s\n", getPhoneOfName("May"));
printf("getPhoneOfName = %s\n", getPhoneOfName("June"));

plz help!!
THX!!

Ps. the value array[0] means the value in addContact("June", "10599813", "1C Fung Yee Street, Wan Chai") can be added to structure array and can be read from the function printf("getPhoneOfName = %s\n", getPhoneOfName("June")) the phone number 10599813 will be returned;

回答 (1)

2010-05-04 6:34 pm
✔ 最佳答案
#include <stdio.h>
#define MAX 1024
struct contact {
char * name;
char * phone;
char * address;
} contact_list[MAX];
int current=0;
void add_contact( char * name, char * phone, char * address )
{
char * ptr;
ptr = (char * )malloc( strlen(name) + 1);
strcpy( ptr, name );
contact_list[current].name = ptr;
ptr = (char * )malloc( strlen(phone) + 1);
strcpy( ptr, phone );
contact_list[current].phone= ptr;
ptr = (char * )malloc( strlen(address) + 1);
strcpy( ptr, address);
contact_list[current].address= ptr;
current++;
}
char * get_contact_phone( char * name )
{
int i;
char * ptr;
i = current-1;
ptr = NULL;
while ( strcmp( name, contact_list[i].name ) && i >=0 ) {
i--;
}
if ( i >= 0 ) {
ptr = contact_list[i].phone;
}
return ptr;
}
main()
{
add_contact("Bendy", "10836588", "22 Tai Cheung Street, Tai Ko");
add_contact("May", "55769943", "59 Hai Tong Road, Aberdeen");
add_contact("June", "10599813", "1C Fung Yee Street, Wan Chai");
printf("getPhoneOfName = %s\n", get_contact_phone("Bendy"));
printf("getPhoneOfName = %s\n", get_contact_phone("May"));
printf("getPhoneOfName = %s\n", get_contact_phone("June"));
}


收錄日期: 2021-04-30 12:53:07
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100502000051KK00084

檢視 Wayback Machine 備份