DERIVED DATATYPES IN C++
From the fundamental data types other types can be derived by
using declaration operators. This article briefly introduces the derived data
types in C++. You can find a detailed explanation of all these types on our
site.
DERIVED
DATA TYPES IN C/C++
Arrays
General syntax :
data_type array_name [size];
where data_type is the type of values array will store, array_name is the name
of array and size is an integer denoting the number of elements in the array.
The elements of array can be accessed as :
array_name[index];
where, index is an integer denoting the position of that element.
Important points on Arrays :
1.
An array consists of elements of same data type.
If it is an integer array it will store only integer elements. Character array
will store only character elements and so on.
2.
Arrays are allocated contagious memory address
which makes them faster and easier to execute.
3.
Indexing starts with number 0 and continues till size-1.
4.
Trying to reference an index out of the bounds [0,
size-1] causes crashing of program.
5.
Arrays are not initialized by default.
Uninitialized arrays contain arbitrary garbage values.
6.
Arrays can be multi-dimensional.
Functions
A function is a named part of a program that can be invoked from other parts of the program as often as needed. A function is usually useful when we want to use a certain set code many times in a program. It would be a tiring and boring job to write the same set of statements over and over again. Instead, a programmer creates a function consisting of those codes so that he might easily use it whenever he needs it in his program.
General syntax for a function is :
Pointer
A pointer variable is a variable that holds a memory address.
This address is usually the location of another variable in the memory. If one
variable contains the address of another variable, the first variable is said
to point to the second variable.
If a variable is going to hold a pointer, it must be declared
as such. A pointer declaration consists of a base type, an * (asterisk), and
the variable name.
General syntax :
data_type * pointer_name ;
where data_type is any valid C++ data type.
The base type (i.e. data_type) of the pointer determines what type of values
the pointer can point to. Technically any pointer can point anywhere in the
memory but the pointer arithmetic is done according to the base type so it
becomes compulsory to declare pointers with correct base types.
Read more >> Pointers in C++
Reference
A reference is an alternative name for an object. A reference
variable provides an alias for a previously defined variable. A reference declaration
consists of a base type, an & (ampersand), a reference variable name
equated to a variable (previously defined).
General syntax :
where data_type must be same data type as that of the variable variable_name.
A reference variable does not create a new variable and so
does not take space in memory. It’s simply another name given to a variable.
You can understand it by taking the example of your actual name and all other
names that your family members or friends have given you. Whatever be the name,
it means only one individual in the real world. This is exactly the case with
reference variables.
Const
The keyword const can be added to the declaration of an
object to make that object a constant rather than a variable. Thus, the value
of that variable (called named constant) cannot be altered during
the program run.
General syntax :
Note : A constant variable cannot remain
uninitialized. It becomes mandatory to initialize the variable with a value at
the time of declaration only. For example, the following code will give error :
const int a;
a=54;
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download the pdf form of this article here.
Post a Comment