SlideShare a Scribd company logo
Pointers
Variable and data type:
 A "normal variable" is a location in memory that can
hold a value.
 It is nothing but the naming convention to refer the
address in which the value gets stored.
 Data type determines the no of bytes allocated to the
data.
Memory location:
 Each memory location is identified and referenced with an
address.
 The address in which the data gets stored.
 For int data type, two bytes get allocated. The starting byte’s
address will be returned as the address of the variable.
The & and * operators:
Both are unary operators
& - Address of operator
- It returns the address of the variable
* - Indirection (or) dereference (or) Value at address operator
main()
{
int a;
a=2;
printf(“Address of a %u”, &a);
printf(“Value at address of a %d”, *(&a));
}
Output:
Address of a 65435
Value at address of a 2
 The operator * should not be used in front of the variable.
 It has to be used in front of address of the variable to retrieve
the value.
Variable Name Value at the address Address of the variable
a 2 65435
main()
{
int a;
a=2;
printf(“Value of a %d”, *a);
}
Already ‘a’ is holding the value only.
Again applying * to ‘a’ will throw the
error.
Pointer Variable:
The variable which holds the address. A pointer is a variable that
points to another variable. A pointer "points to" that other variable
by holding a copy of its address. In the simplest term pointer is a
nearly integer variable which stores a memory address.
Declaration:
Syntax:
data type * pointervariableName;
data type – Refers to the data type of the value that is stored in the
address the pointer points to. It can be of any type (i.e) int, float,
char, struct etc.,
* - Denotes the declared variable is a pointer. It is used to differentiate
the normal variable from the pointer variable.
PointervariableName – The naming conventions applicable to the
normal variable applies here also.
Caution : * operator is also used to declare the pointer variable.
Normal Variable declaration : char a;
Pointer Variable declaration : char *a, *b;
In the above declaration,
Char – The value stored in the address the pointer holds is of char data
type
a - Pointer variable name;
 More than one pointer variable can be declared in the same line. All
of same type.
Why pointer variable is declared with data type?
Even though the pointer is just an integer, the data type is used to
determine the type of data it points to. So that it can retrieve the
value stored in the address, the pointer points to.
Note:
 The type of variable the pointer is pointing to decides pointers
properties and behavior.
 In the declaration, data type * will give the variable the special
attention that it is a pointer variable.
 To make an ordinary variable to be a pointer variable, * is added
between the data type and variable name.
Initialization of Pointer variable:
 Assigning value to the pointer variable. (i.e) address.
 & in front of a variable will retrieve the address of the variable.
main()
{
int a;
a=2;
printf(“Address of a %u”, &a);
printf(“Value at address of a %d”, *(&a));
}
Output:
Address of a 65435
Value at address of a 2
main()
{
int a;
printf(“Address of a %u”, p);
printf(“Value at address of a %d”, *p);
}
Output:
Address of a 65435
Value at address of a 2
int *p;
p=&a; int *p=&a;
a=2; *p=2;
Variable Type Value Address
a int 2 65435
&a - 65435 -
p int * 65435 65432
&p - 65432 -
*p int 2 -
*(&a) int 2 -
Variable Type Value Address
a Int 3 65435
&a - 65435 -
p Int * 65435 65432
&p - 65432 -
*p int 3 -
*(&a) int 3 -
Notes:
65435
65432
a
2
65435
p
(&a)
*p
*(&a)
2
a is assigned
value 2
As ‘p’ is
having
address of a,
*p retrieves
value of ‘a’
Address of ‘a’
is assigned to
‘p’.
3
3
 Pointer variable can be assigned to another pointer variable.
int *r, *k, h;
r=&h;
k=r;
Now r, k both point to same location(i.e) address of h.
Uninitialized Pointer:
 int *r, h;
printf(“%d”,*r); // will result in error
 Uninitialized pointers can point to any memory location.
 Using * (indirection operator) on uninitialized pointers may result in
program throwing a run time error.
 Using a pointer without initializing it to any data may result in data
corruption or even program crash.
 Make sure you initialize all pointers to a valid address before
dereferencing them.
Pointer variable not assigned with any value(Address).
Invalid Pointer References:
 An invalid pointer reference occurs when a pointer's value is
referenced even though the pointer doesn't point to a valid block.
 p=q; when q is uninitialized. The pointer p will then become
uninitialized as well, and any reference to *p is an invalid pointer
reference.
Zero Pointer Reference:
 A zero pointer reference occurs whenever a pointer pointing to zero is
used in a statement that attempts to reference a block. For example, if
p is a pointer to an integer, the following code is invalid:
int *p;
p = 0;
*p = 12;
 There is no block pointed to by p. Therefore, trying to read or write
anything from or to that block is an invalid zero pointer reference.
 Dereferencing such a pointer, however, is invalid.
 The data type of the variable and the type of pointer which
holds the address of the variable must be same.
int r;
char *h, g;
h=&r; // Wrong
h=&g; // Correct
 Do not initialize the pointer variable with normal values. Even
though it is an integer, it is used to hold the address value.
 int *r, h;
r=2; // Wrong
*r=2; // Correct
main()
{
float age;
float *adAge;
adAge=&age;
printf(“No of bytes of pointer variable
%dn”, sizeof(adAge));
printf(“No of bytes of value pointer
points to %d”, sizeof(*adAge));
printf(“No of bytes of variable %d”,
sizeof((age));
}
Output:
No of bytes of pointer variable: 2
No of bytes of value pointer points to: 4
No of bytes of variable : 4
sizeof pointer variable:
Pointer variable always holds address
value only. Hence always the size will
be 2 bytes. This is platform dependent.
The size of pointer variable doesn’t
depend on the data type of the value it
points to.
Null Pointer:
 Null pointer is a pointer which points to nothing.
 Pointer can be declared NULL if it cannot be initialized at the beginning and the
value is initialized during execution. Or assign zero to pointer variable.
 When a pointer is declared, an uninitialized pointer is set to NULL.
 It points to the base address of CPU register and since register is not
addressable, usage of a null pointer will lead to crash or at minimum a
segmentation fault. Depending on the compiler the value varies. Most of the
cases it will be zero.
main()
{
int *r, h;
r=NULL; // r=0;
printf(“Address the pointer holds %u :“, r); // Compiler dependent
printf(“Value the pointer points to %d:”, *r); // Program crashes
}
void Pointer:
 void pointer technically is a pointer which is pointing to the unknown.
 Also in dynamic memory allocation function such as malloc ( ) and alloc ( )
returns void pointer which can be easily converted to other types.(Will dealt
later)
 As the data the pointer points to is not known, dereferencing void pointer will
result in error.
main()
{
int h;
void *r;
r=&h;
printf(“size of pointer variable %d :”, sizeof(r)); // Compiler dependent
printf(“size of value pointer points to %d:”, sizeof(*r)); // Program crashes
}

More Related Content

PPTX
Pointer in c
PDF
VIT351 Software Development VI Unit3
PPT
Pointers operation day2
PPTX
PPTX
Pointer in c
PPTX
Pointers in c - Mohammad Salman
PPT
SPC Unit 3
PPTX
Unit 8. Pointers
Pointer in c
VIT351 Software Development VI Unit3
Pointers operation day2
Pointer in c
Pointers in c - Mohammad Salman
SPC Unit 3
Unit 8. Pointers

What's hot (17)

PPTX
Fundamentals of Pointers in C
PDF
Pointers
PPTX
C programming - Pointer and DMA
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Pointers in C Programming
PPTX
Pointer in c
PPTX
Computer Science:Pointers in C
PPTX
Function Pointer
PPT
Introduction to pointers and memory management in C
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Pointers In C
PPT
Pointers in C
PPTX
Pointers in c v5 12102017 1
PDF
Pointers in C
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Chapter 13.1.8
PPT
Lect 8(pointers) Zaheer Abbas
Fundamentals of Pointers in C
Pointers
C programming - Pointer and DMA
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointers in C Programming
Pointer in c
Computer Science:Pointers in C
Function Pointer
Introduction to pointers and memory management in C
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Pointers In C
Pointers in C
Pointers in c v5 12102017 1
Pointers in C
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Chapter 13.1.8
Lect 8(pointers) Zaheer Abbas
Ad

Similar to Pointer introduction day1 (20)

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

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Lecture Notes Electrical Wiring System Components
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Project quality management in manufacturing
PPTX
Internet of Things (IOT) - A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
CYBER-CRIMES AND SECURITY A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Lecture Notes Electrical Wiring System Components
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
Project quality management in manufacturing
Internet of Things (IOT) - A guide to understanding
573137875-Attendance-Management-System-original
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CH1 Production IntroductoryConcepts.pptx
R24 SURVEYING LAB MANUAL for civil enggi

Pointer introduction day1

  • 2. Variable and data type:  A "normal variable" is a location in memory that can hold a value.  It is nothing but the naming convention to refer the address in which the value gets stored.  Data type determines the no of bytes allocated to the data. Memory location:  Each memory location is identified and referenced with an address.  The address in which the data gets stored.  For int data type, two bytes get allocated. The starting byte’s address will be returned as the address of the variable.
  • 3. The & and * operators: Both are unary operators & - Address of operator - It returns the address of the variable * - Indirection (or) dereference (or) Value at address operator main() { int a; a=2; printf(“Address of a %u”, &a); printf(“Value at address of a %d”, *(&a)); } Output: Address of a 65435 Value at address of a 2
  • 4.  The operator * should not be used in front of the variable.  It has to be used in front of address of the variable to retrieve the value. Variable Name Value at the address Address of the variable a 2 65435 main() { int a; a=2; printf(“Value of a %d”, *a); } Already ‘a’ is holding the value only. Again applying * to ‘a’ will throw the error.
  • 5. Pointer Variable: The variable which holds the address. A pointer is a variable that points to another variable. A pointer "points to" that other variable by holding a copy of its address. In the simplest term pointer is a nearly integer variable which stores a memory address. Declaration: Syntax: data type * pointervariableName; data type – Refers to the data type of the value that is stored in the address the pointer points to. It can be of any type (i.e) int, float, char, struct etc., * - Denotes the declared variable is a pointer. It is used to differentiate the normal variable from the pointer variable. PointervariableName – The naming conventions applicable to the normal variable applies here also. Caution : * operator is also used to declare the pointer variable.
  • 6. Normal Variable declaration : char a; Pointer Variable declaration : char *a, *b; In the above declaration, Char – The value stored in the address the pointer holds is of char data type a - Pointer variable name;  More than one pointer variable can be declared in the same line. All of same type. Why pointer variable is declared with data type? Even though the pointer is just an integer, the data type is used to determine the type of data it points to. So that it can retrieve the value stored in the address, the pointer points to. Note:  The type of variable the pointer is pointing to decides pointers properties and behavior.  In the declaration, data type * will give the variable the special attention that it is a pointer variable.  To make an ordinary variable to be a pointer variable, * is added between the data type and variable name.
  • 7. Initialization of Pointer variable:  Assigning value to the pointer variable. (i.e) address.  & in front of a variable will retrieve the address of the variable. main() { int a; a=2; printf(“Address of a %u”, &a); printf(“Value at address of a %d”, *(&a)); } Output: Address of a 65435 Value at address of a 2 main() { int a; printf(“Address of a %u”, p); printf(“Value at address of a %d”, *p); } Output: Address of a 65435 Value at address of a 2 int *p; p=&a; int *p=&a; a=2; *p=2;
  • 8. Variable Type Value Address a int 2 65435 &a - 65435 - p int * 65435 65432 &p - 65432 - *p int 2 - *(&a) int 2 - Variable Type Value Address a Int 3 65435 &a - 65435 - p Int * 65435 65432 &p - 65432 - *p int 3 - *(&a) int 3 -
  • 9. Notes: 65435 65432 a 2 65435 p (&a) *p *(&a) 2 a is assigned value 2 As ‘p’ is having address of a, *p retrieves value of ‘a’ Address of ‘a’ is assigned to ‘p’. 3 3
  • 10.  Pointer variable can be assigned to another pointer variable. int *r, *k, h; r=&h; k=r; Now r, k both point to same location(i.e) address of h. Uninitialized Pointer:  int *r, h; printf(“%d”,*r); // will result in error  Uninitialized pointers can point to any memory location.  Using * (indirection operator) on uninitialized pointers may result in program throwing a run time error.  Using a pointer without initializing it to any data may result in data corruption or even program crash.  Make sure you initialize all pointers to a valid address before dereferencing them. Pointer variable not assigned with any value(Address).
  • 11. Invalid Pointer References:  An invalid pointer reference occurs when a pointer's value is referenced even though the pointer doesn't point to a valid block.  p=q; when q is uninitialized. The pointer p will then become uninitialized as well, and any reference to *p is an invalid pointer reference. Zero Pointer Reference:  A zero pointer reference occurs whenever a pointer pointing to zero is used in a statement that attempts to reference a block. For example, if p is a pointer to an integer, the following code is invalid: int *p; p = 0; *p = 12;  There is no block pointed to by p. Therefore, trying to read or write anything from or to that block is an invalid zero pointer reference.  Dereferencing such a pointer, however, is invalid.
  • 12.  The data type of the variable and the type of pointer which holds the address of the variable must be same. int r; char *h, g; h=&r; // Wrong h=&g; // Correct  Do not initialize the pointer variable with normal values. Even though it is an integer, it is used to hold the address value.  int *r, h; r=2; // Wrong *r=2; // Correct
  • 13. main() { float age; float *adAge; adAge=&age; printf(“No of bytes of pointer variable %dn”, sizeof(adAge)); printf(“No of bytes of value pointer points to %d”, sizeof(*adAge)); printf(“No of bytes of variable %d”, sizeof((age)); } Output: No of bytes of pointer variable: 2 No of bytes of value pointer points to: 4 No of bytes of variable : 4 sizeof pointer variable: Pointer variable always holds address value only. Hence always the size will be 2 bytes. This is platform dependent. The size of pointer variable doesn’t depend on the data type of the value it points to.
  • 14. Null Pointer:  Null pointer is a pointer which points to nothing.  Pointer can be declared NULL if it cannot be initialized at the beginning and the value is initialized during execution. Or assign zero to pointer variable.  When a pointer is declared, an uninitialized pointer is set to NULL.  It points to the base address of CPU register and since register is not addressable, usage of a null pointer will lead to crash or at minimum a segmentation fault. Depending on the compiler the value varies. Most of the cases it will be zero. main() { int *r, h; r=NULL; // r=0; printf(“Address the pointer holds %u :“, r); // Compiler dependent printf(“Value the pointer points to %d:”, *r); // Program crashes }
  • 15. void Pointer:  void pointer technically is a pointer which is pointing to the unknown.  Also in dynamic memory allocation function such as malloc ( ) and alloc ( ) returns void pointer which can be easily converted to other types.(Will dealt later)  As the data the pointer points to is not known, dereferencing void pointer will result in error. main() { int h; void *r; r=&h; printf(“size of pointer variable %d :”, sizeof(r)); // Compiler dependent printf(“size of value pointer points to %d:”, sizeof(*r)); // Program crashes }