2

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
  • Try man strcpy. And probably strtol if you need the phone number as an integer (which I personally think is a bad idea, but to each his own). Commented May 5, 2010 at 18:02
  • 2
    You can't pass anything to a struct. It's a data structure, not a procedure. You can assign to members of the struct. Commented May 5, 2010 at 18:04

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Assigning a string to an integer?
You can only assign to phone after converting the string to a number.
+1 for "You might want to make your phone element a string rather than an int (phone numbers typically contain non-numeric characters)."
I think you meant strncpy() ?
@Tim: yes, I guess I did mean strncpy. ;-)
|
1

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.

Comments

0

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.

1 Comment

i know that...what i want is to pass the value of pointer to the struct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.