structure question: passing to functions

This is a discussion on structure question: passing to functions within the C Programming forums, part of the General Programming Boards category; Hi, I was just wondering whether it is possible to pass single elements of a structure to a function as ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    structure question: passing to functions

    Hi, I was just wondering whether it is possible to pass single elements of a structure to a function as arguments. For example, structure consists of a few int, but I am only interested in passing, let's say, two of them. Is it possible at all?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Yes.

    Code:
    struct foo
    {
        int iVal1;
        int iVal2;
    };
    
    void func(int i1, int i2)
    {
        // Do something with i1 and i2
    }
    
    int main(void)
    {
        struct foo bar;
        
        bar.iVal1 = 5;
        bar.iVal2 = 10;
    
        func(bar.iVal1,bar.iVal2);
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Oh, so there's nothing special to write in function declaration/definition as type, just int or float or whatever I use from the structure... I just need to modify the function call to use the structure elements. All right, thank you very much!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    But it's useless. Why not just pass the entire address of the structure?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why? So you can duplicate all of your functions that work with basic types so that they take arguments of every structure type you make? Talk about reinventing the wheel.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  2. Beginner question: passing values between functions
    By merike in forum C Programming
    Replies: 5
    Last Post: 11-18-2006, 07:39 PM
  3. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Passing variables through functions and structures?
    By dizz in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2002, 09:18 PM

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