SlideShare ist ein Scribd-Unternehmen logo
1 von 15
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
}

Weitere ähnliche Inhalte

Was ist angesagt?

Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in CShivanshuVerma11
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMAAchyut Devkota
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in CUri Dekel
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Pointers In C
Pointers In CPointers In C
Pointers In CSriram Raj
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1tanmaymodi4
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8patcha535
 

Was ist angesagt? (17)

Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
Pointers
PointersPointers
Pointers
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
 

Ähnlich wie Pointer introduction day1

Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
Pointer in C
Pointer in CPointer in C
Pointer in Cbipchulabmki
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfsudhakargeruganti
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c programRumman Ansari
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
Pointers
PointersPointers
PointersLp Singh
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with PointersHemantha Kulathilake
 

Ähnlich wie Pointer introduction day1 (20)

pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointer in c
Pointer in c Pointer in c
Pointer in c
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers in C language
Pointers  in  C languagePointers  in  C language
Pointers in C language
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 

KĂźrzlich hochgeladen

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 

KĂźrzlich hochgeladen (20)

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 

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 }