Pointer
Pointer
C Pointers
Pointers (pointer variables) are special variables that are used to store addresses
rather than values.
Pointer Syntax
int* p; int *p1; int * p2;
________________________________________
int* p1, p2;
we have declared a pointer p1 and a normal variable p2.
________________________________________
Assigning addresses to Pointers
int* pc, c;
c = 5; //5 is assigned to the c variable.
pc = &c; //The address of c is assigned to the pc pointer.
Example:
int* pc, c, d;
c = 5;
d = -15;
pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15
int* pc, c;
c = 22;
pc = &c;
c = 11;
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
getch();
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
Example:
#include <stdio.h>
void main()
{
int c = 5;
int *p = &c;
printf("%d", *p); // 5
getch();
}
In both cases, we are creating a pointer p (not *p) and assigning &c to it.
To avoid this confusion, we can use the statement like this:
int* p = &c;
include <stdio.h>
void main()
{
int x[4]; int i;
for(i = 0; i < 4; ++i)
{
printf("&x[%d] = %p\n", i, &x[i]);
}
printf("Address of array x: %p", x);
getch();
}
Output:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
Notice that, the address of &x[0] and x is the same.
It's because the variable name x points to the first element of the array.
Example:
struct node
{
int data1;
char data2;
struct node* link;
};
void main()
{
struct node ob;
getch();
}
In the above example ‘link’ is a pointer to a structure of type ‘node’.
#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
Void main()
{
struct node ob1; // Node1
// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
Output: 30 40
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.
The connections made in the above example can be understood using the following
figure.
#include <stdio.h>
struct node {
int data;
struct node* prev_link;
struct node* next_link;
};
Void main()
{
struct node ob1; // Node1
// Initialization
ob1.prev_link = NULL;
ob1.next_link = NULL;
ob1.data = 10;
struct node ob2; // Node2
// Initialization
ob2.prev_link = NULL;
ob2.next_link = NULL;
ob2.data = 20;
struct node ob3; // Node3
// Initialization
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;
// Accessing data of ob1, ob2 and ob3 by ob1
printf("%d\t", ob1.data);
printf("%d\t", ob1.next_link->data);
printf("%d\n", ob1.next_link->next_link->data);
// Accessing data of ob1, ob2 and ob3 by ob2
printf("%d\t", ob2.prev_link->data);
printf("%d\t", ob2.data);
printf("%d\n", ob2.next_link->data);
// Accessing data of ob1, ob2 and ob3 by ob3
printf("%d\t", ob3.prev_link->prev_link->data);
printf("%d\t", ob3.prev_link->data);
printf("%d", ob3.data);
getch();
}
Output:
10 20 30
10 20 30
10 20 30
In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of
the self referential structure ‘node’.
And they are connected using their links in such a way that any of them can easily
access each other’s data.
This is the beauty of the self referential structures.
The connections can be manipulated according to the requirements of the
programmer.
Applications:
Self referential structures are very useful in creation of other complex data
structures like:
Linked Lists
Stacks
Queues
Trees
Graphs etc
Output:
#include <stdio.h>
/* function declaration */
double getAverage(int *arr, int size);
void main () {
int i, sum = 0;
double avg;