When should I use pointers when it comes to creating an input/output function?
When should I use pointers when it comes to creating an input/output function?
When your input or output function requires them.
Quzah.
Hope is the first step on the road to disappointment.
Say I need a function that needs to get information from the user...
Do I do the following?
Where:Code:get_date(date_t *date) { scanf("%d%d%d", &(*date).day, &(*date).month, &(*date).year); } OR get_date(date_t *date) { date_t p; scanf("%d%d%d", p.day, &p.month, &p.year); }
Code:typedef struct { int day, month, year; }date_t
You can do that, but remember to allocate memory for the pointer passed to get_date(). The preferred way to reference a structure member from a pointer is ->, and in the second get_date() to copy the structure from the temporary p structure.
Code:void get_date(date_t *date) { scanf("%d%d%d", &date->day, &date->month, &date->year); //OR //scanf("%d%d%d", &(*date).day, &(*date).month, &(*date).year); } //OR void get_date(date_t *date) { date_t p; scanf("%d%d%d", &p.day, &p.month, &p.year); *date = p; // copy structure } int main() { date_t date; // allocate memory get_date(&date); printf("get_date = %d %d %d\n", date.day, date.month, date.year); return 0; }
When I have an input function, then I will have to use pointers (outputs) as arguments,
AND
When I have an output function, then I dont have to use pointers as arguements.
Is this correct?
Ive been looking through my text book, and it seems that every input function, like scanf, will have pointers (output thingys) as parameters.
Eg: scanf("%d", &a->b);
And output functions like printf will have no pointers passed as parameters.
Eg: printf("%d", a.b);
Correct me if I am wrong. Im really getting annoyed with pointers, and when it should be used.
The idea of when to use pointers with functions:
a) When you call a function and feed it variables as arguments, the function makes COPIES of the variables for its own use. So if you try to have the function change the variables, it only changes its own private copies, and the original ones you might have wanted changed don't actually get changed.
b) However, if the function is designed to receive the ADDRESS of the variables you want changed, then it has its own private copies of the ADDRESS of what its supposed to change. It can then go to the address of the original variables and change them.
When you want to output things, you're usually not interested in changing the variables, so yes, you usually don't pass addresses.
When you want to scan things in, then you usually DO want to change the variables, so that's when you have to use addresses (pointers).
Thanks for the help and clarification guys.
Now, how come every function that passes a pointer as an argument, there is a code for status. Whats this for? Also, the the number in status = to the number of scan parts? Like in the code below, Status == 3, because the function scans 3 inputs.
I would have done the following:
ORCode:my_type_struct_t scan_function(my_type_struct_t *mytypep); typedef struct { char name[100]; int age; double height; }my_type_struct_t; int main(void) { my_type_struct_t person; printf("Enter age, height & name of person, with spaces in between each answer:"); scan_function(&person); return 0; } my_type_struct_t scan_function(my_type_struct_t *mytypep) { int result; result = scanf("%d%if%s", &c->age, &c->height, c->name); return (result); }
Code:my_type_struct_t scan_function(my_type_struct_t *mytypep); typedef struct { char name[100]; int age; double height; }my_type_struct_t; int main(void) { my_type_struct_t person; printf("Enter age, height & name of person, with spaces in between each answer:"); scan_function(&person); return 0; } my_type_struct_t scan_function(my_type_struct_t *mytypep) { int status; status = scanf("%d%if%s", &c->age, &c->height, c->name); if(status == 3) { status = 1; else if(status != EOF) status = 0; return (status); }
scanf returns the number of successful conversions. Example: If you call scanf in hopes of reading 2 numbers in, and it only has one, because the input doesn't match up, it will return 1 instead of 2. If they had both worked, it would have returned 2.
So, depending on your scenario, if you're writting a wrapper for it, which it looks like you are, you may want to return X on success and Y on failure. That way you can simply do:
But what you define X and Y as is up to you. A good number of functions return 0 for success and non-zero for failure (strcmp is an example of one such function).Code:if( myfunction( myargs ) == X ) printf("Success!\n"); else printf("Failure!\n");
Thus, you see things like:
How you use your return values is up to you. For your own sanity however, you'll want to pick some form or style and stick to it fairly consistantly, as best you can.Code:if( !strcmp( foo, bar ) ) printf("A match!\n"); else printf("Not a match!\n");
Quzah.
Hope is the first step on the road to disappointment.
More questions....sorry!
Now, say I have the following structure:
And another structure which relates to the first struture above - Structures nested inside structures:Code:typedef struct { int date, month, year; }date_t;
What is the placeholder for the date_t dateOfBirth in the following function? The question marks means I dont understand what goes here.Code:typedef struct { char fname[50], lname[50]; double height, weight; date_t dateOfBirth; }person_t
From the following code:Code:scanf("%s%s%if%if%%??%??%??", personp->fname, personp->lname, &personp->height, &personp->weight, &personp->??, &personp->??, &personp->??);
Code:int main(void) { person_t lucy; printf("Enter the first name, last name, height, weight, day of birth, birth month, and birth year, with spaces between each answer:); scan_function(&lucy); } int scan_function(person_t *personp) { int status; status = scanf("%s%s%if%if%%??%??%??", personp->fname, personp->lname, &personp->height, &personp->weight, &personp->??, &personp->??, &personp->??); if(status == 3) status = 1; else if(status != EOF) status = 0; return (status); }
Reference the date structure using:
personp->dateOfBirth.date
personp->dateOfBirth.month
personp->dateOfBirth.year
Code:scanf("%s%s%if%if%%d%d%d", personp->fname, personp->lname, &personp->height, &personp->weight, &personp->dateOfBirth.date, &personp->dateOfBirth.month, &personp->dateOfBirth.year);