Professional Documents
Culture Documents
Data Structures and Algorithms: What Are Pointers?
Data Structures and Algorithms: What Are Pointers?
Lab 1
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the
pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for
multiplication. However, in this statement the asterisk is being used to designate a variable as a
pointer.
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is
the same, a long hexadecimal number that represents a memory address. The only difference
between pointers of different data types is the data type of the variable or constant that the
pointer points to.
Null Pointer:
If you do not know the exact address to be assigned to a pointer Null value should be
assigned to it. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard
libraries, including iostream. Consider the following program
#include <iostream>
return 0;
}
To check for a null pointer you can use an if statement as follows −
if(ptr) // succeeds if p is not null
if(!ptr) // succeeds if p is null
Pointers vs Arrays:
Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in
many cases. A pointer that points to the beginning of an array can access that array by
using either pointer arithmetic or array-style indexing.
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
int main () {
unsigned long sec;
getSeconds( &sec );
return 0;
}
return;
}
Passing array as function argument:
#include <iostream>
using namespace std;
// function declaration:
double getAverage(int *arr, int size);
int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
return 0;
}
return avg;
}
Structures:
C/C++ arrays allow you to define variables that combine several data items of the same
kind, but structure is another user defined data type which allows you to combine data
items of different kinds.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines
a new data type, with more than one member, for your program. The format of the
struct statement is this. For example, to define a book structure
struct Books {
Data Structures and Algorithms
Lab 1
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
void printBook( struct Books book ) {
cout << "Book title : " << book.title <<endl;
cout << "Book author : " << book.author <<endl;
cout << "Book subject : " << book.subject <<endl;
cout << "Book id : " << book.book_id <<endl;
}
Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any
other variable as follows −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the & operator before the
structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use
the -> operator as follows −
struct_pointer->title;
Let us re-write above example using structure pointer, hope this will be easy for you to
understand the concept
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Data Structures and Algorithms
Lab 1
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
Problems:
1. Write a program to add, subtract and multiply two complex numbers using
structures to function.
2. Write a structure to store the name, account number and balance of customers
(more than 10) and store their information.
a) Write a function to print the names of all the customers having balance less
than $200.
b) Write a function to add $100 in the balance of all the customers having more
than $1000 in their balance and then print the incremented value of their balance.
#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob1; // Node1
// Intialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
ob1.link = &ob2;
Problem:
Declare a self referential structure tNode as follows:
Create 5 objects of tNode class at system stack. Link the nodes with one another in a long chain through
next pointer. Try it and test node data through first node object.
Self Referential Structure with Multiple Links: Self referential structures with multiple links
can have more than one self-pointers. Many complicated data structures can be easily
constructed using these structures. Such structures can easily connect to more than one nodes
at a time. The following example shows one such structure with more than one links.
#include <stdio.h>
struct node {
int data;
struct node* prev_link;
struct node* next_link;
};
int main()
{
struct node ob1; // Node1
// Intialization
ob1.prev_link = NULL;
ob1.next_link = NULL;
ob1.data = 10;
Data Structures and Algorithms
Lab 1
// Intialization
ob2.prev_link = NULL;
ob2.next_link = NULL;
ob2.data = 20;
// Intialization
ob3.prev_link = NULL;
ob3.next_link = NULL;
ob3.data = 30;
// Forward links
ob1.next_link = &ob2;
ob2.next_link = &ob3;
// Backward links
ob2.prev_link = &ob1;
ob3.prev_link = &ob2;
printf("%d\n", ob2.next_link->data);