SlideShare a Scribd company logo
Pointer
• Pointer is a variable, that stores the address of another
variable.
• Syntax:
data_type *pointer_name;
• Here, the data type specifies the type of data to which the
pointer points.
• The pointer name specifies the name of the pointer. It must
preceded with an * (Asterisk).
Examples:
• int *iptr; // iptr is pointer to an integer variable
• float *fptr; // fptr is pointer to an float variable
• char *cptr; // cptr is a pointer to a character variable
Initialization of a pointer variable
• The process of assigning the address of another variable to a
pointer variable is known as initialization.
• A pointer can be assigned or initialized with the address of an
variable.
• A pointer variable cannot hold a non-address value.
• A pointer can be assigned or initialized with another pointer
of the same type.
• A pointer can be assigned or initialized with another pointer
of the same type. However, it is not possible to assign a
pointer of one type to a pointer of another type without
explicit type casting.
Example:
P=&n;
Initialization of a pointer variable
Example:
• P=&n;
• Where, ‘p’ contains the address of variable ‘n’. The data item
represented by ‘n’ can be accessed by ‘*p’.
• Where * is called the indirection operator.
• We can initialize a pointer variable while declaring it, as
given below.
int num=15;
int *iptr=# // Initialization while declaration
• In the above example, variable num is first declared and then
its address is stored in pointer variable iptr.
Pointer Operators
*  Represents the value
 Also called Indirection operator / Dereferencing
operator / Value of operator.
&  It represents the address.
 Also called as, direction operator / referencing
operator / Address of operator.
SPC Unit 3
SPC Unit 3
Pointer Example
#include<stdio.h>
void main()
{
int a=10,v;
int *p;
p=&a;
v=*p;
printf("Address of a is % un", p);
printf("Value is %d", v);
}
Void Pointer or Generic Pointer
• A void pointer is a generic pointer. (i.e) Means it has no
associated data type.
• A void pointer that can point to any data type and is known as
void pointer.
• A void pointer can hold address of any type and can be
“typecasted” to access the values.
• Syntax:
void *pointer_name; /* pointer to void*/
Example:
int i=5;
void *vptr;
vptr=&i;
printf(“Value of vptr =%d”, *(int*)vptr);
NULL Pointer
• A Null pointer is a special pointer that does not point to any
memory location.
• It represents an invalid memory location.
• It does not hold the address of any variable.
• It has numeric value 0.
• When a NULL value is assigned to a pointer the pointer is
considered as NULL pointer.
Declaration:
int *nptr=NULL;
or
int *nptr=0;
Pointers to Pointers
• In C, a pointer stores the address of another variable which in
turn can store address of another variable and so on.
• Therefore we can have a pointer that stores another pointer’s
address.
• Example:
int val=500, *ptr, **ptr_to_ptr;
ptr=&val;
ptr_to_ptr=&ptr;
Here, *ptr denotes an integer pointer.
**ptr_to_ptr denotes a pointer to an integer pointer.
This is known as multiplication indirections.
Pointers to Pointers (An Example)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=50;
int *b;
int **c;
clrscr();
b=&a;
c=&b;
printf("n Value of a is %d ", a);
printf("n Value of a is %d", *(&a));
printf("n Value of a is %d", *b);
printf("n Value of a is %d", **c);
printf("n Value of b and
address of a= %u", b);
printf("n Address of a is %u", &a);
printf("n Address of b is %u", &b);
printf("n Address of a is %u", *c);
printf("n Address of b is %u", &b);
printf("n Address of b is %u", c);
printf("n Address of c is %u", &c);
getch();
}
Output:
Value of a is 50
Value of a is 50
Value of a is 50
Value of a is 50
Value of b and address of a= 65524
Address of a is 65524
Address of b is 65522
Address of a is 65524
Address of b is 65522
Address of b is 65522
Address of c is 65520
Relationship between Arrays and
Pointers
• The following relationships exist between arrays and pointers:
1. The name of an array refers to the address of the first element of the
array, i.e. an expression of array type decomposes to pointer type.
A program to depict the relationship between arrays and pointers
#include<stdio.h>
main()
{
int arr[3]={40,15,20};
printf("First element of array is at %p
n",arr);
printf("Second element of array is at %p
n",arr+1);
printf("Third element of array is at %p
n",arr+2);
}
OUTPUT:
First element of array is at 24D7;2242
Second element of array is at 24D7:2244
Third element of array is at 241J7:2248
Relationship between Arrays and
Pointers
• The name of an array refers to the address of the first element
of the array but there are two exceptions to this rule:
• a. When an array name is operand of sized operator it does
not decompose to the address of its first element.
Relationship between Arrays and Pointers
• In C language, any operation that involves array subscripting is done
by using pointers. The expression of form E1[E2] is automatically
converted into an equivalent expression of form *(E1+E2).
Array of Pointers
• Array of pointers is a collection of addresses.
• The addresses in an array of pointers could be the addresses
of isolated variables or the addresses of array elements or any
other addresses.
• Example:
int *p[3];
Pointers to an Array
• It is possible to create a pointer that points to a complete array
instead of pointing to the individual elements of an array.
Such a pointer is known as pointer to an array
• Example:
int (*p1)[3];
int (*p2)[2][2];

More Related Content

PPTX
Pointer in c
PPTX
Unit 8. Pointers
PPTX
Pointer in c program
PPTX
Pointers in C Programming
PPTX
Pointers in C Language
PPTX
Presentation on pointer.
PPTX
ppt on pointers
PPTX
Pointer in c
Unit 8. Pointers
Pointer in c program
Pointers in C Programming
Pointers in C Language
Presentation on pointer.
ppt on pointers

What's hot (19)

PPTX
Pointers in c - Mohammad Salman
PPT
Pointers C programming
PPT
PPTX
Pointer in C
PPT
Pointers in c
PPTX
Pointers in c v5 12102017 1
PPT
Lect 8(pointers) Zaheer Abbas
PPT
Introduction to pointers and memory management in C
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPT
C pointers
PPTX
C programming - Pointer and DMA
PPTX
Fundamentals of Pointers in C
PPT
Pointer in C
PDF
Types of pointer in C
PPT
Pointers (Pp Tminimizer)
PPTX
Computer Science:Pointers in C
PPTX
arrays and pointers
PDF
Pointer in c++ part1
Pointers in c - Mohammad Salman
Pointers C programming
Pointer in C
Pointers in c
Pointers in c v5 12102017 1
Lect 8(pointers) Zaheer Abbas
Introduction to pointers and memory management in C
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
C pointers
C programming - Pointer and DMA
Fundamentals of Pointers in C
Pointer in C
Types of pointer in C
Pointers (Pp Tminimizer)
Computer Science:Pointers in C
arrays and pointers
Pointer in c++ part1
Ad

Similar to SPC Unit 3 (20)

PPTX
PPS-POINTERS.pptx
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
pointers.pptx
PPTX
4 Pointers.pptx
PPTX
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
PPT
Pointer introduction day1
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPTX
Unit-I Pointer Data structure.pptx
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPTX
pointers of the programming in c most efficient.pptx
PPTX
pointers.pptx
PPTX
Pointers in C
PDF
PSPC--UNIT-5.pdf
PPTX
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PPTX
Pointers
PDF
Pointers-Computer programming
PPT
pointers (1).ppt
PPTX
Arrays to arrays and pointers with arrays.pptx
PPS-POINTERS.pptx
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
pointers.pptx
4 Pointers.pptx
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
Pointer introduction day1
Pointers and single &multi dimentionalarrays.pptx
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-I Pointer Data structure.pptx
UNIT 4 POINTERS.pptx pointers pptx for basic c language
pointers of the programming in c most efficient.pptx
pointers.pptx
Pointers in C
PSPC--UNIT-5.pdf
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Pointers
Pointers-Computer programming
pointers (1).ppt
Arrays to arrays and pointers with arrays.pptx
Ad

More from SIMONTHOMAS S (20)

PPTX
Cs8092 computer graphics and multimedia unit 5
PPTX
Cs8092 computer graphics and multimedia unit 4
PPTX
Cs8092 computer graphics and multimedia unit 3
PPT
Cs8092 computer graphics and multimedia unit 2
PPTX
Cs8092 computer graphics and multimedia unit 1
PPTX
Mg6088 spm unit-5
PPT
Mg6088 spm unit-4
PPTX
Mg6088 spm unit-3
PPTX
Mg6088 spm unit-2
PPTX
Mg6088 spm unit-1
PPTX
IT6701-Information Management Unit 5
PPTX
IT6701-Information Management Unit 4
PPTX
IT6701-Information Management Unit 3
PPTX
IT6701-Information Management Unit 2
PPTX
IT6701-Information Management Unit 1
PPTX
CS8391-Data Structures Unit 5
PPTX
CS8391-Data Structures Unit 4
PPTX
CS8391-Data Structures Unit 3
PPTX
CS8391-Data Structures Unit 2
PPTX
CS8391-Data Structures Unit 1
Cs8092 computer graphics and multimedia unit 5
Cs8092 computer graphics and multimedia unit 4
Cs8092 computer graphics and multimedia unit 3
Cs8092 computer graphics and multimedia unit 2
Cs8092 computer graphics and multimedia unit 1
Mg6088 spm unit-5
Mg6088 spm unit-4
Mg6088 spm unit-3
Mg6088 spm unit-2
Mg6088 spm unit-1
IT6701-Information Management Unit 5
IT6701-Information Management Unit 4
IT6701-Information Management Unit 3
IT6701-Information Management Unit 2
IT6701-Information Management Unit 1
CS8391-Data Structures Unit 5
CS8391-Data Structures Unit 4
CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 2
CS8391-Data Structures Unit 1

Recently uploaded (20)

PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Artificial Intelligence
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPT
Total quality management ppt for engineering students
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
86236642-Electric-Loco-Shed.pdf jfkduklg
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
introduction to high performance computing
PDF
PPT on Performance Review to get promotions
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
UNIT - 3 Total quality Management .pptx
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
Current and future trends in Computer Vision.pptx
Visual Aids for Exploratory Data Analysis.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Artificial Intelligence
UNIT 4 Total Quality Management .pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Total quality management ppt for engineering students
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
86236642-Electric-Loco-Shed.pdf jfkduklg
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
introduction to high performance computing
PPT on Performance Review to get promotions
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
UNIT - 3 Total quality Management .pptx
Exploratory_Data_Analysis_Fundamentals.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Current and future trends in Computer Vision.pptx

SPC Unit 3

  • 1. Pointer • Pointer is a variable, that stores the address of another variable. • Syntax: data_type *pointer_name; • Here, the data type specifies the type of data to which the pointer points. • The pointer name specifies the name of the pointer. It must preceded with an * (Asterisk). Examples: • int *iptr; // iptr is pointer to an integer variable • float *fptr; // fptr is pointer to an float variable • char *cptr; // cptr is a pointer to a character variable
  • 2. Initialization of a pointer variable • The process of assigning the address of another variable to a pointer variable is known as initialization. • A pointer can be assigned or initialized with the address of an variable. • A pointer variable cannot hold a non-address value. • A pointer can be assigned or initialized with another pointer of the same type. • A pointer can be assigned or initialized with another pointer of the same type. However, it is not possible to assign a pointer of one type to a pointer of another type without explicit type casting. Example: P=&n;
  • 3. Initialization of a pointer variable Example: • P=&n; • Where, ‘p’ contains the address of variable ‘n’. The data item represented by ‘n’ can be accessed by ‘*p’. • Where * is called the indirection operator. • We can initialize a pointer variable while declaring it, as given below. int num=15; int *iptr=&num; // Initialization while declaration • In the above example, variable num is first declared and then its address is stored in pointer variable iptr.
  • 4. Pointer Operators *  Represents the value  Also called Indirection operator / Dereferencing operator / Value of operator. &  It represents the address.  Also called as, direction operator / referencing operator / Address of operator.
  • 7. Pointer Example #include<stdio.h> void main() { int a=10,v; int *p; p=&a; v=*p; printf("Address of a is % un", p); printf("Value is %d", v); }
  • 8. Void Pointer or Generic Pointer • A void pointer is a generic pointer. (i.e) Means it has no associated data type. • A void pointer that can point to any data type and is known as void pointer. • A void pointer can hold address of any type and can be “typecasted” to access the values. • Syntax: void *pointer_name; /* pointer to void*/ Example: int i=5; void *vptr; vptr=&i; printf(“Value of vptr =%d”, *(int*)vptr);
  • 9. NULL Pointer • A Null pointer is a special pointer that does not point to any memory location. • It represents an invalid memory location. • It does not hold the address of any variable. • It has numeric value 0. • When a NULL value is assigned to a pointer the pointer is considered as NULL pointer. Declaration: int *nptr=NULL; or int *nptr=0;
  • 10. Pointers to Pointers • In C, a pointer stores the address of another variable which in turn can store address of another variable and so on. • Therefore we can have a pointer that stores another pointer’s address. • Example: int val=500, *ptr, **ptr_to_ptr; ptr=&val; ptr_to_ptr=&ptr; Here, *ptr denotes an integer pointer. **ptr_to_ptr denotes a pointer to an integer pointer. This is known as multiplication indirections.
  • 11. Pointers to Pointers (An Example) #include<stdio.h> #include<conio.h> void main() { int a=50; int *b; int **c; clrscr(); b=&a; c=&b; printf("n Value of a is %d ", a); printf("n Value of a is %d", *(&a)); printf("n Value of a is %d", *b); printf("n Value of a is %d", **c); printf("n Value of b and address of a= %u", b); printf("n Address of a is %u", &a); printf("n Address of b is %u", &b); printf("n Address of a is %u", *c); printf("n Address of b is %u", &b); printf("n Address of b is %u", c); printf("n Address of c is %u", &c); getch(); } Output: Value of a is 50 Value of a is 50 Value of a is 50 Value of a is 50 Value of b and address of a= 65524 Address of a is 65524 Address of b is 65522 Address of a is 65524 Address of b is 65522 Address of b is 65522 Address of c is 65520
  • 12. Relationship between Arrays and Pointers • The following relationships exist between arrays and pointers: 1. The name of an array refers to the address of the first element of the array, i.e. an expression of array type decomposes to pointer type. A program to depict the relationship between arrays and pointers #include<stdio.h> main() { int arr[3]={40,15,20}; printf("First element of array is at %p n",arr); printf("Second element of array is at %p n",arr+1); printf("Third element of array is at %p n",arr+2); } OUTPUT: First element of array is at 24D7;2242 Second element of array is at 24D7:2244 Third element of array is at 241J7:2248
  • 13. Relationship between Arrays and Pointers • The name of an array refers to the address of the first element of the array but there are two exceptions to this rule: • a. When an array name is operand of sized operator it does not decompose to the address of its first element.
  • 14. Relationship between Arrays and Pointers • In C language, any operation that involves array subscripting is done by using pointers. The expression of form E1[E2] is automatically converted into an equivalent expression of form *(E1+E2).
  • 15. Array of Pointers • Array of pointers is a collection of addresses. • The addresses in an array of pointers could be the addresses of isolated variables or the addresses of array elements or any other addresses. • Example: int *p[3];
  • 16. Pointers to an Array • It is possible to create a pointer that points to a complete array instead of pointing to the individual elements of an array. Such a pointer is known as pointer to an array • Example: int (*p1)[3]; int (*p2)[2][2];