Accessing struct elements C++ code example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Accessing struct element in different ways in C++

To show: How to access the structure's members in different ways




// Accessing structures' elements

#include <iostream>

using namespace std;


// struct type definition

struct Card


char *face; // pointer to char type

char *suit; // pointer to char type



void main(void)


// declare the struct type variables

struct Card p;

struct Card *SPtr;

p.face = "Ace";

p.suit = "Spades";


SPtr = &p;


cout<<"Accessing structure element:\n";

cout<<"\n\'SPtr->suit\' = "<<SPtr->suit<<endl;

cout<<"\'SPtr->face\' = "<<SPtr->face<<endl;



Output example:


Accessing structure element:

'SPtr->suit' = Spades

'SPtr->face' = Ace

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index