C Programming - passing a pointer to an array

How to pass the value of a pointer to a structure array;

For example, on 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 do I pass the value of ap_name, ap_email, ap_phone to a struct? And, should all these pointers be used?

+2


a source to share


3 answers


The name and email address are relatively simple; 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 ap_name

to into the structure name field. No more 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 ap_name

is less than 99 - strlen (ap_name) nul characters (ASCII 0). The same for email:

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

      

Note that this assumes that the lengths of ap_name and ap_email will always be less than the target buffers; as written, your code pretty much guarantees this, but additional sanity checking might be a bad idea.

As with a phone number, a regular int may not (and most likely will not) be wide enough to hold a 10-digit number, provided you store the area or extension code (minimum range guaranteed by locale [-32767,32767]

). Not to mention, phone numbers are usually represented by non-numeric characters like (999)-999-9999

. You can also save this information as a string.



EDIT

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

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

      

and then convert the string with strtoul()

:

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

      

The library function strtoul()

converts the string representation of a number to an equivalent numeric value.

+1


a source


Use strncpy

to copy a string to the corresponding structure element.



You might want to make your element a phone

string rather than an int (phone numbers usually contain non-numeric characters). If it really should be an int, use atoi

or strtol

to convert the string ap_phone

to int and then just assign that value to the phone.

+2


a source


I'm not sure what you are asking, but:

When you have a structure, each member is only accessible with. Operator.

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

      

All valid operators receive the 0th person name, third party email address, or the 10th person's phone number. Each of them is a separate variable that can be thought of as something else.

0


a source







All Articles