views:

149

answers:

3

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

For example, on a txt I have this:

John Doe;[email protected];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?

+2  A: 

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.

Paul R
Assigning a string to an integer?
Carl Norum
You can only assign to phone after converting the string to a number.
Jonathan Leffler
sorry, strcpy don't work
Pedro
Yes, sorry, missed that initially - still editing...
Paul R
+1 for "You might want to make your phone element a string rather than an int (phone numbers typically contain non-numeric characters)."
pst
I think you meant **strncpy()** ?
Tim Post
strncpy() how can i do that?
Pedro
@Pedro: look at the man page for strncpy: `man strncpy`.
Paul R
@Tim: yes, I guess I did mean `strncpy`. ;-)
Paul R
strncpy is a extension of strcoy, the difference is that we can choose the numbers of characters.... I want that when i want to printf("Name %s\n",persons[i].name);
Pedro
@Pedro: the reason for using strncpy in this particular case is to ensure that you do not over-run the fixed size char arrays in your struct.
Paul R
i try strncpy, but doesn't work too
Pedro
@Pedro: update your question to show your latest code with strncpy calls and then we can help you to get it working
Paul R
A: 

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.

Andrei Krotkov
i know that...what i want is to pass the value of pointer to the struct
Pedro
+1  A: 

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.

John Bode