Thread: passing struct to function

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    passing struct to function

    hey all, is it possible to pass a struct to a function (i'd think so, but i have no idea what I'm doing). what's the syntax? i thought about loading an array and passing that to function, would that be better? thanks

    Code:
    main()
    {
    	
    	s_plyrs unchs1={"t", .9, .2, 9, .9, .333, .100, .5};
    	s_plyrs unchs2={"r", .6, .05, 4, 1, .220, .500, .5};
    	s_plyrs unchs3={"a",1,1,1,1,1,1,1};
    	s_plyrs bob={"b",1,1,1,1,1,1,1};
    	s_plyrs chuck={"c",1,1,1,1,1,1,1};
    	s_plyrs dave={"d",1,1,1,1,1,1,1};
    	s_plyrs ed={"e",1,1,1,1,1,1,1};
    	s_plyrs fred={"f",1,1,1,1,1,1,1};
    	s_plyrs guy={"g",1,1,1,1,1,1,1};
    
    //....
    
    void f_show_ps (s_plyrs  :confused: )
    {
    	clrscr();
    	cout<<"Name  "<<unchs1<<tab<<endl<<endl;//input goes here
    	cout<<"fielding"<<tab<<"batting"<<endl;
    	cout<<"speed"<<tab<<tab<<"pitching"<<endl;
    	cout<<"throwing"<<tab<<"Luck"<<endl;
    }

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    me again. the thing is i'd to manipulate the entire struct in the function. i should just learn how to use oo . . .

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is it possible to pass a struct to a function
    Yes
    Code:
    struct T
    {
      // Data members
    }
    
    void function ( T *t );
    
    int main ( void )
    {
      T t;
      function ( &t );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    As prelude showed- it's better to pass directly, rather than having the compiler make a copy of it for use in the function. But if you really don't want the structure passed to be touched, you can use const

    Code:
    struct MyStruct
    {
          int a, b, c, d, e, f, g, h;
    };
    
    void  do_something (const MyStruct &obj)
    {
         /* ... */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Acessing a struct after passing to a function
    By bomberto in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 04:29 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM