Thread: Passing structures to a function?

  1. #16
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    If you wouldn't mind I'd like to see an example.
    You really should read a tutorial on classes to get an idea of cunstructors.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class SpaceShip{
      public: 
        int XCoord;
        int YCoord;
        SpaceShip(int x, int y);
    };
    
    SpaceShip::SpaceShip(int x, int y){
      XCoord = x;
      YCoord = y;
    }
    
    int main(int argc, char* argv[])
    {
      SpaceShip ThisSpaceShip(20, 30);
      cout<<"X Coordinate: "<<ThisSpaceShip.XCoord<<endl<<"Y Coordinate: "<<ThisSpaceShip.YCoord<<endl;
      return 0;
    }
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Regarding your question on std::array...
    Click here if you don't know what std::array is (requires TR1 or C++11).

    Code:
    #include <array>
    #include <iostream>
    
    void foo(std::array<int, 5> arr)
    {
    	arr[0] = 5;
    }
    
    int main()
    {
    	std::array<int, 5> arr = { 1, 2, 3, 4, 5 }; // Requires C++11 initializer lists*
    	foo(arr);
    	std::cout << arr[0] << std::endl; // Prints 1 because a copy of arr was passed to foo.
    }

    Code:
    #include <array>
    #include <iostream>
    
    void foo(std::array<int, 5>& arr)
    {
    	arr[0] = 5;
    }
    
    int main()
    {
    	std::array<int, 5> arr = { 1, 2, 3, 4, 5 }; // Requires C++11 initializer lists*
    	foo(arr);
    	std::cout << arr[0] << std::endl; // Prints 5 because a reference of arr was passed to foo.
    }
    *) Known supported compilers: gcc
    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. Passing structures to a function
    By Magi in forum C Programming
    Replies: 7
    Last Post: 12-05-2012, 03:04 PM
  2. Passing Structures as Function Arguments
    By Jyqft in forum C Programming
    Replies: 13
    Last Post: 03-26-2012, 08:02 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM