Like Tree4Likes

Passing structures to a function?

This is a discussion on Passing structures to a function? within the C++ Programming forums, part of the General Programming Boards category; If you wouldn't mind I'd like to see an example. You really should read a tutorial on classes to get ...

  1. #16
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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;
    }

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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.

Page 2 of 2 FirstFirst 12
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, 09:02 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  4. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 07:13 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM

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