Thread: Structure Declaration

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Structure Declaration

    Hi There,

    can some one tell me how to declare a function with a pointer to a structure, i know how to do it with the structure outside of the main function but unclear how to do it with the structure inside.

    Code:
    void function();
    
    void main()
    {
        struct hello{
                        int goodbye;
                        int evening;
                       }
    hello.goodbye = 1;
    function();
    
    }
    
    void function()
    {
    printf("%d", hello->goodbye);
    }
    basically how do i declare the functions

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Code:
    /* First, define the structure outside functions.
    ** Just the definition, I'm not talking about variables (objects)
    ** of the structure at this time */
    struct hello {
        int goodbye;
        int evening;
    };
    
    /* now the prototype, using pointers to the structure */
    void function(struct hello *);
    
    /* The main() function */
    int main(void) {
        struct hello hello = {1}; /* initialization; all unmentioned members are initialized to 0 */
        hello.evening = 42; /* assignment ot a specific member */
        function(&hello); /* call the function */
        return 0;
    }
    
    void function(struct hello *p) {
        printf("%d\n", p->goodbye);
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, if you declare struct hello inside main, it will only be visible inside main. Any other function wishing to use that type can't, it doesn't know what such a structure looks like. Thus, as far as I can tell, your question is irrelevant.

    If you do want to declare it inside main, you need a semicolon after the struct declaration. Then, declare your variable as usual. Alternatively, you can squeeze the variable definition into the struct definition, but I think it would make your code harder to read.
    Code:
    int main(void)
    {
        struct hello {
            int goodbye;
            int goodbye;
        };
        struct hello foo, *bar;  // foo is a variable of type 'struct hello', bar is type 'pointer to struct hello'
        // or
        struct hello {
            int goodbye;
            int goodbye;
        } foo, *bar;  // same as above, but struct and variables in one statement -- I do not recommend this
        ...
        return 0;
    }
    Also, it's int main(void), and return an int at the end, usually 0. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about structure declaration
    By raczzoli in forum C Programming
    Replies: 1
    Last Post: 12-01-2011, 12:41 PM
  2. Structure declaration
    By rk211083 in forum C Programming
    Replies: 18
    Last Post: 06-23-2010, 02:52 AM
  3. int s: 3 ; declaration in structure is possible
    By nkrao123@gmail. in forum C Programming
    Replies: 3
    Last Post: 12-17-2009, 06:17 AM
  4. Can you use a structure in the declaration of itself?
    By ititrx in forum C++ Programming
    Replies: 23
    Last Post: 11-07-2006, 03:40 PM
  5. structure declaration
    By Roaring_Tiger in forum C Programming
    Replies: 1
    Last Post: 03-26-2003, 11:22 PM