Thread: passing structure variables into function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    39

    passing structure variables into function

    I want to get the values for the structure in one function and pass this values to the main and print those values in main. can i do this. my example code as follows. Is it correct or any changes i have to do. If i compile this and run, it is showing 0 for all the three variables. help me in this issue.



    Code:
    void h(struct);
    
    struct e
    {
         int a;
         int b;
    };
    
    struct f
    {
        int c;
        struct e d;
    }g;
    
    main()
    {
       h(struct f g);
       printf("a=%d",g.d.a);
       printf("b=%d",g.d.b);
       printf("c=%d",g.c);
    }
    
    void h(struct f g)
    {
        struct f g={10,11,12};
    }

    thanks in advance....

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/Implicit_main
    And then you need to learn about pointers and functions.
    Your code won't work at all. You must pass the struct via a pointer, the function needs to set the values (not initialize) and them main can print them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM