开发者

C Programming - Passing a pointer to array

How do I pass a pointer value to an array of the struct;

For example, on a开发者_开发知识库 txt I have this:

John Doe;xxxx@hotmail.com;214425532;

My code:

typedef struct Person{
    char name[100];
    char email[100];
    int phone;
}PERSON;

int main(){
   PERSON persons[100];
    FILE *fp;
    char *ap_name;
    char *ap_email;
    char *ap_phone;
    char line[100];
    fp=("text.txt","r");
    if(fp==NULL){
        exit(1);
    }
    else{
        fgets(line,100,fp);
        ap_name=strtok(line,";");
        ap_email=strtok(NULL,";");
        ap_phone=strtok(NULL,";");
    } 
    return 0;
}

My question is how can I pass the value of ap_name, ap_email, ap_phone to the struct? And, do I need to use all of these pointers?


Use strncpy to copy a string to its corresponding struct element.

You might want to make your phone element a string rather than an int (phone numbers typically contain non-numeric characters). If it really has to be an int then use atoi or strtol to convert the ap_phone string to an int, and then just assign this value to phone.


Name and email are relatively easy; just use strcpy (or strncpy);

strncpy(persons[i].name, ap_name, sizeof persons[i].name - 1);

This will copy the contents of the string pointed to by ap_name into the name field of the struct. At most sizeof persons[i].name - 1 (100 - 1, or 99) characters will be copied into persons[i].name, and if the length of the string pointed to by ap_name is less than that, then 99 - strlen(ap_name) nul characters (ASCII 0) are appended. Same thing for email:

strncpy(persons[i].email, ap_email, sizeof persons[i].email - 1);

Note that this assumes that the length of ap_name and ap_email will always be less than the destination buffers; as written, your code pretty much guarantees this, but extra sanity checking may not be a bad idea.

As for the phone number, a regular int may not be (and most likely won't be) wide enough to hold a 10-digit number, assuming you're storing area code or extensions (the minimum range guaranteed by the language standard is [-32767,32767]). Not to mention that phone numbers are generally represented with non-numeric characters, such a (999)-999-9999. You may want to store this information as a string as well.

EDIT

Another alternative for the phone number is to use a wider numeric type (preferably unsigned):

struct PERSON {
   ...
   unsigned long phone;
   ...
};

and then convert the string using strtoul():

persons[i].phone = strtoul(ap_phone, NULL, 10);

The strtoul() library function will convert a string representation of a number into the equivalent numerical value.


I'm not sure whether this is what you're asking, but:

When you have a structure, each member is just accessed with the . operator.

persons[0].name
persons[3].email
persons[10].phone

Are all valid operators to get the 0th person's name, the 3rd person's email or the 10th person's phone number. Each of those is a separate variable that can be treated as anything else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜