Thread: Pointers...

  1. #1
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19

    Pointers...

    When should I use pointers when it comes to creating an input/output function?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When your input or output function requires them.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Say I need a function that needs to get information from the user...

    Do I do the following?

    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);
    }
    Where:

    Code:
    typedef struct
    {
              int day, month, year;
    }date_t

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    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;
    }

  5. #5
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    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.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    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).

  7. #7
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    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:

    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 result;
              result = scanf("%d%if%s", &c->age, &c->height, c->name);
              return (result);
    }
    OR

    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);
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    Code:
    if( myfunction( myargs ) == X )
        printf("Success!\n");
    else
        printf("Failure!\n");
    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).

    Thus, you see things like:
    Code:
    if( !strcmp( foo, bar ) )
        printf("A match!\n");
    else
        printf("Not a match!\n");
    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.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    More questions....sorry!

    Now, say I have the following structure:

    Code:
    typedef struct
    {
            int date, month, year;
    }date_t;
    And another structure which relates to the first struture above - Structures nested inside structures:

    Code:
    typedef struct
    {
              char fname[50], lname[50];
              double height, weight;
              date_t dateOfBirth;
    }person_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:
    scanf("%s%s%if%if%%??%??%??", personp->fname, personp->lname, &personp->height, &personp->weight, &personp->??, &personp->??, &personp->??);
    From the following code:

    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);
    }

  10. #10
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM