0% found this document useful (0 votes)
62 views

Pointer

The document discusses pointers in C programming. It begins by explaining that pointers are special variables that store addresses rather than values. It provides examples of pointer syntax and how to assign addresses to pointers. It also demonstrates how to get and change the value pointed to by a pointer. The document then discusses self-referential structures, which are structures that contain pointer members pointing to the same structure type. Examples are provided to illustrate single and multiple link self-referential structures. Finally, an example is given of passing a pointer to a function to modify the value at that address.

Uploaded by

Shraddha Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Pointer

The document discusses pointers in C programming. It begins by explaining that pointers are special variables that store addresses rather than values. It provides examples of pointer syntax and how to assign addresses to pointers. It also demonstrates how to get and change the value pointed to by a pointer. The document then discusses self-referential structures, which are structures that contain pointer members pointing to the same structure type. Examples are provided to illustrate single and multiple link self-referential structures. Finally, an example is given of passing a pointer to a function to modify the value at that address.

Uploaded by

Shraddha Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Address in C

int var; scanf("%d", &var);


#include <stdio.h>
void main()
{
int var = 5; printf("var: %d\n", var);
// Notice the use of & before var
printf("address of var: %u", &var);
getch();
}
Output : var: 5
address of var: 2686778

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.

Get Value of Thing Pointed by Pointers


int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
To get the value stored in that address, we used *pc.
pc is a pointer, not *pc.
You cannot and should not do something like *pc = &c;
Changing Value Pointed by Pointers
Let's take an example.
int* pc, c;
c = 5;
pc =&c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
We have assigned the address of c to the pc pointer.
Example.
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1

Example:
int* pc, c, d;
c = 5;
d = -15;
pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15

Example: Working of Pointers


#include <stdio.h>
void main()
{

int* pc, c;
c = 22;

printf("Address of c: %p\n", &c);


printf("Value of c: %d\n\n", c); // 22

pc = &c;

printf("Address of pointer pc: %p\n", pc);


printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;

printf("Address of pointer pc: %p\n", pc);


printf("Content of pointer pc: %d\n\n", *pc); // 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 pointer pc: 2686784


Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

Common mistakes when working with pointers


pointer pc to point to the address of c.
int c, *pc;
// pc is address but c is not pc = c; // Error
// &c is address but *pc is not *pc = &c; // Error
// both &c and pc are addresses pc = &c;
// both c and *pc values *pc = c;

Example:
#include <stdio.h>
void main()
{
int c = 5;
int *p = &c;
printf("%d", *p); // 5
getch();
}

Why didn't we get an error when using int *p = &c?


It's because
int *p = &c;
is equivalent to
int *p:
p = &c;

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;

Relationship between Arrays and Pointers


An array is a block of sequential data.
Let's write a program to print addresses of array elements.

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.

&x[0] is equivalent to x.


 x[0] is equivalent to *x.
Similarly,
• &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
• &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
• ...
• Basically, &x[ i ] is equivalent to x+i and x[i] is equivalent to *(x+i).

Example 1: Pointers and Arrays


#include <stdio.h>
void main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
// Equivalent to sum += x[i]
sum += *(x+i);
}
printf("Sum = %d", sum);
getch();
}
OUTPUT:
Enter 6 numbers: 2 3 4 4 12 4
Sum = 29
Here, we have declared an array x of 6 elements.
To access elements of the array, we have used pointers.
array names are converted to pointers.
That's the reason why we can use pointers to access elements of arrays.
remember that pointers and arrays are not the same.

Example 2: Arrays and Pointers


#include <stdio.h>
void main()
{
int x[5] = {1, 2, 3, 4, 5}; int* ptr;
// ptr is assigned the address of the third element
ptr = &x[2]; *ptr=3
printf("*ptr = %d \n", *ptr); // 3
printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
getch();
}
Output:
*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2
In this example, &x[2], the address of the third element, is assigned to the ptr pointer.
Hence, 3 was displayed when we printed *ptr.
And, printing *(ptr+1) gives us the fourth element.

Similarly, printing *(ptr-1) gives us the second element.

Self Referential Structures


Self Referential structures are those structures that have one or more pointers
which point to the same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential
in nature.

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’.

Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the


referencing pointer.

An important point to consider is that the pointer should be initialized properly


before accessing, as by default it contains garbage value.
Types of Self Referential Structures
Self Referential Structure with Single Link
Self Referential Structure with Multiple Links
Self Referential Structure with Single Link: 
These structures can have only one self-pointer as their member.
The following example will show us how to connect the objects of a self-
referential structure with the single link and access the corresponding data
members.
The connection formed is shown in the following figure .

#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;

     struct node ob2; // Node2


  
    // Initialization
    ob2.link = NULL;
    ob2.data1 = 30;
    ob2.data2 = 40;
  
    // Linking ob1 and ob2
    ob1.link = &ob2;
      // Accessing data members of  ob2 using ob1
    printf("%d", ob1.link->data1);
    printf("\n%d", ob1.link->data2);
    getch();
}

Output: 30 40

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.

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

Example: Passing Pointer to a Function


#include <stdio.h>
void salaryhike(int *var, int b)
{
*var = *var + b;
}
int main()
{
int salary=0, bonus=0;
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return 0;
}

Output:

Enter the employee current salary:10000


Enter bonus:2000
Final salary: 12000
which can accept a pointer, can also accept an array as shown in the following
example –

#include <stdio.h>

/* function declaration */
double getAverage(int *arr, int size);

void main () {

/* an int array with 5 elements */


int balance[5] = {1000, 2, 3, 17, 50};
double avg;

/* pass pointer to the array as an argument */


avg = getAverage( balance, 5 ) ;

/* output the returned value */


printf("Average value is: %f\n", avg );
getch();
}

double getAverage(int *arr, int size) {

int i, sum = 0;
double avg;

for (i = 0; i < size; ++i) {


sum += arr[i];
}

avg = (double)sum / size;


return avg;
}
When the above code is compiled together and executed, it produces the following
result −
Average value is: 214.40000

You might also like