Chapter Six: Introduction To Arrays, String and Pointers
Chapter Six: Introduction To Arrays, String and Pointers
index
elements
(int/float)
That means, we can store 5 values of type int/float,
without having to declare 5 different variables each with a
different identifier.
Cont…
An array contains multiple objects of identical types
courses as following:
float mark[6];
Note: array size cannot be a variable whose value is set
elements.
In C++ an index always begins at zero.
Example: marks[6];
Thus, the elements belonging to marks array are
to num[2].
Cont…
If you initialize an array when you define it, you do not need to
initial values.
If the array length is explicitly stated in the definition and is
Operations on Arrays
finding the maximum member of an array
finding the minimum member of an array
searching values from the array
Multidimensional Arrays
Multidimensional arrays can be described as arrays of
arrays.
Each dimension is represented as a subscript in the array.
of type int .
The way to declare this array would be: int matrix[3][5];
Cont…
For example, the way to reference the second element
vertically and fourth horizontally in an expression would be:
matrix[1][3]
order.
For instance, int marks[2][3]={2,4,7,8,32, 51};
to the braces.
The entire initialization set must be within braces, and it
manipulate strings.
A string is a consecutive sequence (i.e., array) of
variable.
The only difference between pointer variable and regular
in memory.
Cont…
There are two pointer operators in C++:
& the address operator
* the dereference operator
The & operator always produces the memory address of
whatever it precedes.
The * operator, when used with pointers, either declares a
pointer or dereferences the pointer’s value.
The dereference operator can be literally translated to
"value pointed by" .
Cont…
Generally, objects can be accessed in two ways:
1. directly by their symbolic name, or
2. indirectly through a pointer.
The act of getting to an object via a pointer to it, is called
dereferencing the pointer. Pointer variables are defined to
point to objects of a specific type so that when the pointer is
dereferenced, a typed object is obtained.
Cont…
Note: We generally do not decide where the variable is to
be placed - fortunately that is something automatically
done by the compiler and the operating system on
runtime.
But once the operating system has assigned an address
there are some cases in which we may be interested in
knowing where the variable is stored.
This can be done by preceding the variable identifier by
an ampersand sign (&), which literally means,
"address of”. For example: ptr= &var;
Cont…
Suppose that var has been placed in the memory address
1776 and that we write the following:
var=25; The result will be the one shown in the following
diagram:
x=var;
ptr = &var;
Declaring pointers
Is reserving a memory location for a pointer variable in the
heap. Syntax:
type * pointer_name ;
To declare a pointer variable called int * p_age, do the
following:
int * p_age;
Whenever the dereference operator, *, appears in a variable
declaration, the variable being declared is always a pointer
variable.
Type is the data type pointed to by the pointer. This type is not
the type of the pointer itself, but the type of the data the pointer
points to.
Cont …
Note that the asterisk (*) used when declaring a pointer
only means that it is a pointer, and should not be
confused with the dereference operator, which is also
written with an asterisk (*). They are simply two different
things represented with the same sign.
Pointer variable Initialization
Pointers can have any name that is legal for other variables.
p_age is an integer pointer. The type of a pointer is very
important.
p_age can point only to integer values, never to floating-point
or other types.
To assign p_age the address of a variable, do the following:
int age = 26;
int * p_age; int age = 26;
p_age = &age; OR int * p_age = & age;
Cont…
Both ways are possible.
If you wanted to print the value of age, do the following:
cout<<age; //prints the value of age
Or by using pointers you can do it as follows
cout<<*p_age; //dereferences p_age;
Note: The dereference operator produces a value that tells
the pointer where to point. Without the *, (i.e
cout<<p_age), a cout statement would print an address
(the address of age). With the *, the cout prints the value
at that address.
Pointer operation
The Address of Operator &:
The & is a unary operator that returns the memory address
of its operand. For example, if var is an integer variable,
then &var is its address. This operator has the same
precedence and right-to-left associativity as the other
unary operators.
You should read the & operator as "the address of"
which means &var will be read as "the address of var".
Cont…
Indirection Operator *, it is the complement of &. It is a
unary operator that returns the value of the variable located
at the address specified by its operand.
Pointers and Arrays