Last active
August 29, 2015 13:59
-
-
Save axrwkr/10589206 to your computer and use it in GitHub Desktop.
C/C++ Structure Reference and Structure De-Reference operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
on os x compile with | |
clang++ structure_pointers.cpp -o structure_pointers | |
on windows compile with | |
cl /EHsc structure_pointers.cpp | |
on linux compile with | |
g++ structure_pointers.cpp -o structure_pointers | |
*/ | |
#include <iostream> | |
struct car { | |
std::string name; | |
std::string registration_number; | |
int top_speed; | |
float cost; | |
}; | |
int main() | |
{ | |
// declare a car | |
car my_car; | |
// set the members of the car using the structure reference operator | |
my_car.name = "Model X"; | |
my_car.top_speed = 130; | |
my_car.cost = 80000; | |
my_car.registration_number = "TESLA"; | |
// access the members of the car using the structure reference operator | |
std::cout << "Access the members using the structure reference operator"; | |
std::cout << std::endl; | |
std::cout << "car name: " << my_car.name << std::endl; | |
std::cout << "car price: " << my_car.cost << std::endl; | |
// obtain a pointer to the car | |
car * pointer = &my_car; | |
// use the structure de reference operator to change the values of | |
// the car members | |
std::cout << std::endl; | |
std::cout << "Change the members via the structure de-reference operator"; | |
std::cout << std::endl; | |
pointer->name = "Model Y"; | |
pointer->cost = 85000; | |
// display the members of the | |
std::cout << std::endl; | |
std::cout << "Access the members using the structure de-reference operator"; | |
std::cout << std::endl; | |
std::cout << "car name: " << pointer->name << std::endl; | |
std::cout << "car price: " << pointer->cost << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment