passing structure variables into function

This is a discussion on passing structure variables into function within the C Programming forums, part of the General Programming Boards category; I want to get the values for the structure in one function and pass this values to the main and ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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, 11:40 AM
  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, 12: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, 06:18 AM

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