1
“Pointers”
2
Pointer Basics
Pointer Definition and syntax
The pointer in C language is a variable which stores the address of
another variable. This variable can be of type int, char, array, or any
other pointer.
Syntax:
Data_type *variable_name;
• Asterisk(*)is called as Indirection Operator. It is also called as
Value at Address Operator.
• It Indicates Variable declared is of Pointer type. variable_name
must follow the rules of an identifier.
Example
int *ip; //pointer to an integer
double *dp //pointer to double
Pointer Basics
•Normal variable stores the value whereas pointer variable stores the
address of the variable.
•The content of the C pointer always be a whole number i.e. address.
•Always C pointer is initialized to null, i.e. int *p = null.
•The value of null pointer is 0.
•& symbol is used to get the address of the variable.
•* symbol is used to get the value of the variable that the pointer is
pointing to.
•If a pointer in C is assigned to NULL, it means it is pointing to
nothing.
Pointer concept with diagrams
int i=5;
int*j;
j=&i;
Pointer Basic Example
#include <stdio.h>
void main() q ptr
{
int *ptr, q;
q = 50; 5025 5045
ptr = &q;
printf(“Value of q =%dt", q);
printf(“Address of q =%un", &q);
printf(“Address of ptr =%ut", &ptr);
printf(“Value of ptr =%d", *ptr);
return 0;
}
OUTPUT:
Value of q = 50 Address of q = 5025
Address of ptr = 5045 Value of ptr = 50
50 5025
Program on Reference and De-reference operator
#include <stdio.h>
int main()
{
int *pc, c=22;
printf("Address of c:%un",&c);
printf("Value of c:%dnn",c);
pc=&c;
printf("Address of pointer pc:%un",pc);
printf("Content of pointer pc:%dn",*pc);
c=11;
printf("Address of pointer pc:%un",pc);
printf("Content of pointer pc:%dn",*pc);
*pc=2;
printf("Address of c:%un",&c);
printf("Value of c:%dn",c);
return 0;
}
7
Output
• Address of c:26867
• Value of c:22
• Address of pointer pc:26867
• Content of pointer pc:22
• Address of pointer pc:26867
• Content of pointer pc:11
• Address of c:26867
• Value of c:2
8
PointerArithmetic
Pointer Expression How it is evaluated ?
ip = ip + 1 ip => ip + 1 => 1000 + 1*4 => 1004
ip++ or ++ip ip++ => ip + 1 => 1004 + 1*4 => 1008
ip = ip + 5 ip => ip + 5 => 1008 + 5*4 => 1028
ip = ip - 2 ip => ip - 2 => 1028 - 2*4 => 1020
ip-- or --ip ip => ip - 1 => 1020 - 1*4 => 1016
Pointer Arithmetic on Integers
9
Pointer to Pointer
If a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer
Syntax:
int **ptr;
10
Pointer to Pointer Example
#include<stdio.h>
void main()
{
int a, *p1, **p2;
a=65;
p1=&a;
p2=&p1;
printf("a = %dn", a);//65
printf("address of a = %dn", &a);//5000
printf("p1 = %dn", p1);//5000
printf("address p1 = %dn", &p1);//6000
printf("*p1 = %dn", *p1);//65
printf("p2 = %dn", p2);//6000
printf("*p2 = %dn", *p2);//5000
printf("**p2 = %dn", **p2);//65
}
11
Pointer to Arrays
• When an array is declared, compiler allocates sufficient amount of
memory to contain all the elements of the array.
• Base address i.e address of the first element of the array is also
allocated by the compiler.
Example:
int x[4];
From the above example,
• &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).
Example 1: Pointers and Arrays
#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]);
sum += *(x+i); // Equivalent to sum += x[i]
}
printf("Sum = %d", sum);
return 0;
}
The program computes the sum of six elements entered by the user.
Example 2: Pointers and Arrays
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2]; // ptr is assigned the address of the third element
printf("*ptr = %d n", *ptr); // 3
printf("*(ptr+1) = %d n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
return 0;
}
14
Pointers as Function Argument in C
• Pointer as a function parameter is used to hold addresses of
arguments passed during function call. This is also known as call
by reference.
• When a function is called by reference any change made to the
reference variable will effect the original variable.
• The address of num1 and num2 are passed to the swap() function
using swap(&num1, &num2);.
• Pointers n1 and n2 accept these arguments in the function
definition.
void swap(int* n1, int* n2)
{
... ..
}
15
Call by Reference Example
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 10, n = 20;
printf("m = %dn", m);
printf("n = %dnn", n);
swap(&m, &n); //passing address
printf("After Swapping:nn");
printf("m = %dn", m);
printf("n = %d", n);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Output:
m = 10
n = 20
After Swapping:
m = 20
n = 10
16
Pointer to Structure
Accessing Structure Members with Pointer
• To access members of structure using the structure variable, we used
the dot . operator.
• But when we have a pointer of structure type, we use arrow -> to
access structure members.
Example:
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
}
personPtr -> age is equivalent to
(*personPtr).age
personPtr -> weight is equivalent to
(*personPtr).weight
17
Pointer to Structure Example
#include <stdio.h>
struct my_structure
{
char name[20];
int number;
int rank;
};
int main()
{
struct my_structure variable = {“Sachin", 100, 1};
struct my_structure *ptr;
ptr = &variable;
printf("NAME: %sn", ptr->name);
printf("NUMBER: %dn", ptr->number);
printf("RANK: %d", ptr->rank);
return 0;
}
18
Self Referential Structure
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.
19
Self Referential Structure Example
#include<stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
void main()
{
struct node ob1;
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
struct node ob2;
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
ob1.link = &ob2; // Linking ob1 and ob2
/*Accessing data members of ob2 using
ob1*/
printf("%d", ob1.link->data1);
printf("n%d", ob1.link->data2);
}
Enumeration (or enum) in C
Enumeration (or enum) is a user defined data type in C. It is mainly
used to assign names to integral constants, the names make a program
easy to read and maintain.
• To define enums, the enum keyword is used.
Syntax:
enum flag {const1, const2, ..., constN};
By default, const1 is 0, const2 is 1 and so on. You can change default
values of enum elements during declaration (if necessary).
Enumeration (or enum) in C Example
#include<stdio.h>
enum week{Mon, Tue,
Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output: 2
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May,
Jun, Jul,Aug, Sep, Oct, Nov, Dec};
int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);
return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11
22
Bitfields in C
Bit Field Declaration
The declaration of a bit-field has the following form inside a structure −
struct {
type [member_name1] : width ;
type [member_name2] : width ;
};
Sr.No. Element & Description
1
type
An integer type that determines how a bit-field's value is interpreted.
The type may be int, signed int, or unsigned int.
2 member_name
The name of the bit-field.
3 width
The number of bits in the bit-field. The width must be less than or
equal to the bit width of the specified type.
23
Bitfields Example
#include <stdio.h>
struct stat{
unsigned int width;
unsigned int height;
} status1;
struct stats{
unsigned int width : 1;
unsigned int height : 1;
} status2;
int main( ) {
printf( "Memory size occupied by status1 : %dn", sizeof(status1));
printf( "Memory size occupied by status2 : %dn", sizeof(status2));
return 0;
}
Output:
Memory size occupied by status1 : 4
Memory size occupied by status2 : 1
24
“Thank You”

Pointers in C Language

  • 1.
  • 2.
    2 Pointer Basics Pointer Definitionand syntax The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, or any other pointer. Syntax: Data_type *variable_name; • Asterisk(*)is called as Indirection Operator. It is also called as Value at Address Operator. • It Indicates Variable declared is of Pointer type. variable_name must follow the rules of an identifier. Example int *ip; //pointer to an integer double *dp //pointer to double
  • 3.
    Pointer Basics •Normal variablestores the value whereas pointer variable stores the address of the variable. •The content of the C pointer always be a whole number i.e. address. •Always C pointer is initialized to null, i.e. int *p = null. •The value of null pointer is 0. •& symbol is used to get the address of the variable. •* symbol is used to get the value of the variable that the pointer is pointing to. •If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • 4.
    Pointer concept withdiagrams int i=5; int*j; j=&i;
  • 5.
    Pointer Basic Example #include<stdio.h> void main() q ptr { int *ptr, q; q = 50; 5025 5045 ptr = &q; printf(“Value of q =%dt", q); printf(“Address of q =%un", &q); printf(“Address of ptr =%ut", &ptr); printf(“Value of ptr =%d", *ptr); return 0; } OUTPUT: Value of q = 50 Address of q = 5025 Address of ptr = 5045 Value of ptr = 50 50 5025
  • 6.
    Program on Referenceand De-reference operator #include <stdio.h> int main() { int *pc, c=22; printf("Address of c:%un",&c); printf("Value of c:%dnn",c); pc=&c; printf("Address of pointer pc:%un",pc); printf("Content of pointer pc:%dn",*pc); c=11; printf("Address of pointer pc:%un",pc); printf("Content of pointer pc:%dn",*pc); *pc=2; printf("Address of c:%un",&c); printf("Value of c:%dn",c); return 0; }
  • 7.
    7 Output • Address ofc:26867 • Value of c:22 • Address of pointer pc:26867 • Content of pointer pc:22 • Address of pointer pc:26867 • Content of pointer pc:11 • Address of c:26867 • Value of c:2
  • 8.
    8 PointerArithmetic Pointer Expression Howit is evaluated ? ip = ip + 1 ip => ip + 1 => 1000 + 1*4 => 1004 ip++ or ++ip ip++ => ip + 1 => 1004 + 1*4 => 1008 ip = ip + 5 ip => ip + 5 => 1008 + 5*4 => 1028 ip = ip - 2 ip => ip - 2 => 1028 - 2*4 => 1020 ip-- or --ip ip => ip - 1 => 1020 - 1*4 => 1016 Pointer Arithmetic on Integers
  • 9.
    9 Pointer to Pointer Ifa pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer Syntax: int **ptr;
  • 10.
    10 Pointer to PointerExample #include<stdio.h> void main() { int a, *p1, **p2; a=65; p1=&a; p2=&p1; printf("a = %dn", a);//65 printf("address of a = %dn", &a);//5000 printf("p1 = %dn", p1);//5000 printf("address p1 = %dn", &p1);//6000 printf("*p1 = %dn", *p1);//65 printf("p2 = %dn", p2);//6000 printf("*p2 = %dn", *p2);//5000 printf("**p2 = %dn", **p2);//65 }
  • 11.
    11 Pointer to Arrays •When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. • Base address i.e address of the first element of the array is also allocated by the compiler. Example: int x[4]; From the above example, • &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).
  • 12.
    Example 1: Pointersand Arrays #include <stdio.h> int main() { int i, x[6], sum = 0; printf("Enter 6 numbers: "); for(i = 0; i < 6; ++i) { scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]); sum += *(x+i); // Equivalent to sum += x[i] } printf("Sum = %d", sum); return 0; } The program computes the sum of six elements entered by the user.
  • 13.
    Example 2: Pointersand Arrays #include <stdio.h> int main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; ptr = &x[2]; // ptr is assigned the address of the third element printf("*ptr = %d n", *ptr); // 3 printf("*(ptr+1) = %d n", *(ptr+1)); // 4 printf("*(ptr-1) = %d", *(ptr-1)); // 2 return 0; }
  • 14.
    14 Pointers as FunctionArgument in C • Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. • When a function is called by reference any change made to the reference variable will effect the original variable. • The address of num1 and num2 are passed to the swap() function using swap(&num1, &num2);. • Pointers n1 and n2 accept these arguments in the function definition. void swap(int* n1, int* n2) { ... .. }
  • 15.
    15 Call by ReferenceExample #include <stdio.h> void swap(int *a, int *b); int main() { int m = 10, n = 20; printf("m = %dn", m); printf("n = %dnn", n); swap(&m, &n); //passing address printf("After Swapping:nn"); printf("m = %dn", m); printf("n = %d", n); return 0; } void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Output: m = 10 n = 20 After Swapping: m = 20 n = 10
  • 16.
    16 Pointer to Structure AccessingStructure Members with Pointer • To access members of structure using the structure variable, we used the dot . operator. • But when we have a pointer of structure type, we use arrow -> to access structure members. Example: struct person { int age; float weight; }; int main() { struct person *personPtr, person1; } personPtr -> age is equivalent to (*personPtr).age personPtr -> weight is equivalent to (*personPtr).weight
  • 17.
    17 Pointer to StructureExample #include <stdio.h> struct my_structure { char name[20]; int number; int rank; }; int main() { struct my_structure variable = {“Sachin", 100, 1}; struct my_structure *ptr; ptr = &variable; printf("NAME: %sn", ptr->name); printf("NUMBER: %dn", ptr->number); printf("RANK: %d", ptr->rank); return 0; }
  • 18.
    18 Self Referential Structure SelfReferential 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.
  • 19.
    19 Self Referential StructureExample #include<stdio.h> struct node { int data1; char data2; struct node* link; }; void main() { struct node ob1; ob1.link = NULL; ob1.data1 = 10; ob1.data2 = 20; struct node ob2; ob2.link = NULL; ob2.data1 = 30; ob2.data2 = 40; ob1.link = &ob2; // Linking ob1 and ob2 /*Accessing data members of ob2 using ob1*/ printf("%d", ob1.link->data1); printf("n%d", ob1.link->data2); }
  • 20.
    Enumeration (or enum)in C Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. • To define enums, the enum keyword is used. Syntax: enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).
  • 21.
    Enumeration (or enum)in C Example #include<stdio.h> enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; } Output: 2 #include<stdio.h> enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,Aug, Sep, Oct, Nov, Dec}; int main() { int i; for (i=Jan; i<=Dec; i++) printf("%d ", i); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 10 11
  • 22.
    22 Bitfields in C BitField Declaration The declaration of a bit-field has the following form inside a structure − struct { type [member_name1] : width ; type [member_name2] : width ; }; Sr.No. Element & Description 1 type An integer type that determines how a bit-field's value is interpreted. The type may be int, signed int, or unsigned int. 2 member_name The name of the bit-field. 3 width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.
  • 23.
    23 Bitfields Example #include <stdio.h> structstat{ unsigned int width; unsigned int height; } status1; struct stats{ unsigned int width : 1; unsigned int height : 1; } status2; int main( ) { printf( "Memory size occupied by status1 : %dn", sizeof(status1)); printf( "Memory size occupied by status2 : %dn", sizeof(status2)); return 0; } Output: Memory size occupied by status1 : 4 Memory size occupied by status2 : 1
  • 24.