News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

C++ Tutorial » Structure » structure pointer

Started by magesh.p, Apr 27, 2009, 09:29 PM

Previous topic - Next topic

magesh.p

Define pointer for structure

#include <iostream>
using namespace std;

struct MyStructure
{
   int n;
   float f;
   float f2;
};

int main()
{
MyStructure myMyStructure;
MyStructure *ptrMyStructure;

//set the f of the structure
myMyStructure.f = 1000;

//intialize the pointer
ptrMyStructure = &myMyStructure;

//change the pointers f
ptrMyStructure->f = 2000;

//print out the structures f
cout << myMyStructure.f << "\n";

return 0;
}


- An Proud Acumen -

magesh.p

Pointers to structures

#include <iostream.h>
#include <stdlib.h>

struct Employee {
  char title [50];
  int year;
};


int main ()
{
  char buffer[50];

  Employee aEmployee;
  Employee * pEmployee;
  pEmployee = & aEmployee;

  cout << "Enter title: ";
  cin.getline (pEmployee->title,50);
  cout << "Enter year: ";
  cin.getline (buffer,50);
  pEmployee->year = atoi (buffer);

  cout << "\nYou have entered:\n";
  cout << pEmployee->title;
  cout << " (" << pEmployee->year << ")\n";

  return 0;
}


Output
QuoteEnter title: Enter year:
You have entered:
(^CTerminate batch job (Y/N)? n

- An Proud Acumen -

magesh.p

Use -> for structure pointer

#include <iostream>
using namespace std;

struct account
{
   int accountnum;
   float balance;
   float interestrate;
};

int main()
{
account myaccount;
account *ptraccount;

ptraccount->balance = 1000;
       return 0;
}


- An Proud Acumen -

magesh.p

Compare address

#include <ios>
#include <iostream>
#include <ostream>

struct MyStruct {
  int x;
  int y;
};

int main()
{
  MyStruct demo[10];
  std::cout << std::boolalpha;

  std::cout << (&demo[0]   < &demo[2])   << '\n';
  std::cout << (&demo[0]   == demo)      << '\n';
  std::cout << (&demo[10]  > &demo[9])   << '\n';
  std::cout << (&demo[0].x < &demo[0].y) << '\n';

}


- An Proud Acumen -

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview