Lecture2_Array, Pointers and Structures
Lecture2_Array, Pointers and Structures
(DSAs)
Lecture 2
Outline
As derived Data types
Arrays
Array representation
Array Operations
Traverse
Insertion
Deletion
Search
Update
Hence,
Array Operations
Following are the basic operations supported by an
array;
Traverse − Prints all the array elements one by one.
Algorithm:
Consider LA is a linear array with N elements
and K is a positive integer such that K<=N.
Algorithm:
Consider LA is a linear array with N
elements and K is a positive integer such that
K<=N.
Algorithm:
Consider LA is a linear array with N elements
and K is a positive integer such that K<=N.
type *var-name;
where:-
type is the type of data accessed by the
pointer.
var_name is the name of the pointer
the strike (*) means we will access the value
that the address stored in the pointer has
Pointer
Null Pointer:-
It’s better to assign null to the pointer when
we initialize it, and the value that can be
accessed by this pointer will be zero.
int *p = null;
What we do with Pointers
We can use pointers as arguments of a
function or as a return from the function.
Point blank;
blank = { 3.0, 4.0 }; // WRONG !!
Structures
You might wonder why this perfectly
reasonable statement should be illegal; I’m not
sure, but I think the problem is that the
compiler doesn’t know what type the right
hand side should be. If you add a typecast:
Point blank; blank = (Point){ 3.0, 4.0 };
It is legal to assign one structure to another.
For example:
Point p1 = { 3.0, 4.0 };
Point p2 = p1;
cout << p2.x << ", " << p2.y << endl;
The output of this program is 3, 4.
Questions