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)

PPT
pointers (1).ppt
PPTX
Pointers in c language
PPTX
Pointers
PPTX
PPS-POINTERS.pptx
PPTX
4 Pointers.pptx
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
Ponters
PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
PDF
VIT351 Software Development VI Unit3
PPT
13092119343434343432232323121211213435554
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPTX
Arrays to arrays and pointers with arrays.pptx
PDF
C pointers and references
PPTX
Pointers and single &multi dimentionalarrays.pptx
DOCX
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
PPT
Pointers on data structure in computer science.ppt
PPT
pointer, structure ,union and intro to file handling
PDF
PSPC--UNIT-5.pdf
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
pointers (1).ppt
Pointers in c language
Pointers
PPS-POINTERS.pptx
4 Pointers.pptx
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
Ponters
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
VIT351 Software Development VI Unit3
13092119343434343432232323121211213435554
Algoritmos e Estruturas de Dados - Pointers
Arrays to arrays and pointers with arrays.pptx
C pointers and references
Pointers and single &multi dimentionalarrays.pptx
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
Pointers on data structure in computer science.ppt
pointer, structure ,union and intro to file handling
PSPC--UNIT-5.pdf
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
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)

PPTX
Geodesy 1.pptx...............................................
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
737-MAX_SRG.pdf student reference guides
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
Artificial Intelligence
DOCX
573137875-Attendance-Management-System-original
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PPT on Performance Review to get promotions
PPTX
Current and future trends in Computer Vision.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPT
introduction to datamining and warehousing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
Geodesy 1.pptx...............................................
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
737-MAX_SRG.pdf student reference guides
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Artificial Intelligence
573137875-Attendance-Management-System-original
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT on Performance Review to get promotions
Current and future trends in Computer Vision.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
additive manufacturing of ss316l using mig welding
introduction to datamining and warehousing
R24 SURVEYING LAB MANUAL for civil enggi
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Construction Project Organization Group 2.pptx
Safety Seminar civil to be ensured for safe working.

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