REFERENCES, ARRAYS AND
POINTERS
Krishna M. Singh
Department of Mechanical and Industrial Engineering
Indian Institute of Technology Roorkee
Roorkee 247667
Reference: Deitel and Deitel. C++ : How to Program.
Prentice Hall, 2008
Krishna M. Singh, Department of Mechanical & Industrial Engineerig, IIT-Roorkee
1
Fundamental Data Types
Basic Types: Boolean, character, integer and floating point types.
Additional Types: void : To signify absence of information void f() // function f() does not return a value void *pv; // pointer to object of unknown type
enumeration (enum): To represent specific set of values. Named integer constants can be defined as members of an enumeration. enum keyword {ASM, AUTO, BREAK}; keyword key;
Krishna M. Singh, Department of Mechanical & Industrial Engineerig, IIT-Roorkee
2
Derived Data Types
Address Types
References
Pointers
Powerful, but difficult to master
Simulate pass-by-reference
Close relationship with arrays and strings
Aggregate Types
Arrays and strings
Structures and Unions
Abstract Data Types (ADTs): classes
Krishna M. Singh, Department of Mechanical & Industrial Engineerig, IIT-Roorkee
3
References
Reference is another name (alias) for a variable
It is a pointer but a constant one – once declared it cannot be made alias of another variable
A reference declaration must have initialization and it can be initialized to a variable, not a literal constant. A variable can have several references (aliases) – all references hold the same address
Reference is not a separate variable like a pointer
– it does not occupy space in memory
Krishna M. Singh, Department of Mechanical & Industrial Engineerig, IIT-Roorkee
4
…. References
All operations supposedly performed on the alias (i.e., the reference) are actually performed on the original variable Example int count = 1; int &cRef = count; cRef++; Increments count through alias cRef