C++ Struct and Array

Objectives :

  • formula and understanding
    sending two dimensional array as a parameter to a function.

Key Note: 1 Struct is an object , but array is not; typedef allows you to create an alias for a data type. The identifiers like typedef provides a synonym for an existing data type than creating new data type.
---------------typedef and struct------------------------

typedef struct Emp
    string name; string sal; int id;
} pEmp;

------- declaring an array of structures -------------

  • pEmp emprec[3]; 
  • pEmp emprec[3]={ {"A","45k", 10},
    {"B","55k", 11},
    {"C","65k", 12}

------------------Array element indexed in sturcture--------------------------------------

  • emprec[i] : shows an indexed struct element used like an array.
  • emp_record(&emprec[i]);  passing address of structure element with an index
  • cin>>ptr1->name;  assigning the address of a structure element with (->) pointer structure operator

Key Note: 2

  • pEmp *ptr1; pointer to a struct
  • ptr1->name pointer to a structure element
  • &ptr1->name address of a stucture element

Step: 2 Edit and save Source file


Step: 3 Runtime Views: