Passing struct data between files

This is a discussion on Passing struct data between files within the C Programming forums, part of the General Programming Boards category; Trying to learn how to use structs, i want the user to enter the phone number and the program to ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    Passing struct data between files

    Trying to learn how to use structs, i want the user to enter the phone number and the program to print that number back.

    main.c
    Code:
    #include "phone.h"
    #include <stdio.h>
    
    int main()
    {
    phone d;
    printf("Enter your phone number: ");
    input_phone(&d);
    
    print_phone(d);
    return 0;
    phone.h
    Code:
    #ifndef _PhoneH_
    #define _PhoneH_
    
    typedef struct {
    int areacode;
    int actualnumber;
    } phone;
    
    void input_phone(phone *dp);
    void print_phone(phone d);
    
    #endif
    phone.c
    Code:
    #include "phone.h"
    #include <stdio.h>
    
    void input_phone(phone *dp)
    {
    phone phonein;
    scanf("%d-%d",&phonein.areacode,&phonein.actualnumber);
    }
    
    void print_phone(phone d)
    {
    phone phonein;
    printf("Your phone numer is %d-%d", phonein.areacode, phoneinactualnumber);
    }
    If i do a print within input_phone of either areacode or actualnumber it works fine, but the print inside print_phone just displays a ramdon number[FONT=Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace][/FONT]
    Last edited by SuperMiguel; 03-28-2012 at 04:10 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    Ok, so what's your question? Does this code work the way you want it to, and you just need an explanation of why? Does it not work the way you expect it to? If so, what does it do that is wrong, and what should it do? Does it even compile? If not, what compile command did you use and what errors did you get?

    Note, the syntax highlighter on our forum should help you locate at least one error, near the bottom of phone.c.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by anduril462 View Post
    Ok, so what's your question? Does this code work the way you want it to, and you just need an explanation of why? Does it not work the way you expect it to? If so, what does it do that is wrong, and what should it do? Does it even compile? If not, what compile command did you use and what errors did you get?

    Note, the syntax highlighter on our forum should help you locate at least one error, near the bottom of phone.c.
    The code does compile, but If i do a print within input_phone of either areacode or actualnumber it works fine (display the right values), but the print inside print_phone just displays a ramdon number



  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    Well, you have a local variable called phonein inside input_phone. That's where you store the result of your scanf. You never copy those results into dp, so that they are in d in main, and get passed to print_phone.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    how would i do that??

    dp[0]=phonein.areacode
    ??
    Last edited by SuperMiguel; 03-28-2012 at 04:42 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    dp is not an array (nor is it the address of an array in main), so I would not use array syntax. Just use the -> operator. It works just like the . operator for struct members, but it is used with pointers to structs:
    Code:
    dp->areacode = phonein.areacode;
    Even better, you can just read directly into dp:
    Code:
    scanf("%d-%d", &dp->areacode, &dp->actualnumber);

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Thank you very much for the info.
    Then to print i just have to:

    printf("Your phone number is: %d-%d", d.areacode, d.actualnumber);

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    Yes. Though it is common in C to always pass a pointer to a struct, even when you don't really need to. If you just pass the struct, it has to copy the whole struct onto the stack as the parameter to the function. This can be quite inefficient if you have a large struct. If you pass it by pointer however, it only needs to copy the pointer (usually 4 or 8 bytes), no matter the size of the struct. Thus, you might do:
    Code:
    void print_phone(const phone *dp)
    {
        printf("Your phone number is: %d-%d", dp->areacode, dp->actualnumber);
    }
    The 'const' is a good way to keep your print function from accidentally changing the contents of the struct pointed to by dp.

    EDIT: And get rid of the phonein variable from both of your functions, it's not needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Create struct tm from user specified date
    By cunzlow in forum C Programming
    Replies: 4
    Last Post: 09-30-2009, 12:39 PM
  3. manipulate date on files
    By cnchybrid in forum C Programming
    Replies: 7
    Last Post: 04-19-2007, 12:54 AM
  4. passing struct pointers to make tables/files
    By Jarrette in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2003, 06:23 PM
  5. Displaying time and date of files
    By wozza in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21