SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Pointers In C?
POINTER is a variable that stores address of another variable. A
Pointer in C is used to allocate memory dynamically i.e. at run
time. The pointer variable might be belonging to any of the
data type such as int, float, char, double, short etc.
The purpose of pointer is to save memory space and achieve
faster execution time.
Pointer Syntax
data_type *var_name;
Example : int *p; char *p;
Where, * is used to denote that “p” is pointer variable and not a normal
variable.
KEY POINTS TO REMEMBER ABOUT POINTERS IN C:
1. Normal variable stores the value whereas pointer variable stores the
address of the variable.
2. The content of the C pointer always be a whole number i.e. address.
3. Always C pointer is initialized to null, i.e. int *p = null.
4. The value of null pointer is 0.
Let's see some valid pointer declarations
int *x; /* pointer to an integer */
int *xyz, dafar; /* xyz is a pointer to type integer and dafar is an integer variable */
double *ptr2; /* pointer to a double */
float *ptr3; /* pointer to a float */
char *ch1 ; /* pointer to a character */
float *ptr, nalayak;
/*ptr is a pointer to type float and nalayak is an ordinary float variable */
Assigning addresses to Pointers
Let's take an example.
Int *pc, c;
c = 5;
pc = &c;
Here, 5 is assigned to the c variable.
And, the address of c is assigned to the pc
pointer.
5
Variable c
Value
Address 2008
2008
Pointer Variable
pc
Value
Address 5060
NOTE:
By the way, * is called the dereference operator
(when working with pointers).
It operates on a pointer and gives the value stored
in that pointer.
A Simple program for pointer:
#include <stdio.h>
void main()
{
int a=10; // variable declaration
int *p; // pointer variable declaration
p = &a;// store address of variable ‘a’ in pointer ‘p’
printf(“Address stored in a variable p is: %xn”, p);
// accessing the address
printf(“Value stored in a variable p is : %dn”, *p);
// accessing the value
getch();
}
Output:
Address stored in a variable p is: 60ff08
Value stored in a variable p is: 10
Types of a pointer
1. Null pointer
2. void pointer
3. wild pointer
Null pointer
We can create a null pointer by assigning null value during the pointer
declaration. This method is useful when you do not have any address assigned
to the pointer. A null pointer always contains value 0.
#include <stdio.h>
void main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:%x”,p);
}
Output:
The value inside variable p is : 0
void pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any
standard data type. A void pointer is created by using the keyword void. It can be used to store
an address of any variable.
#include <stdio.h>
void main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d",sizeof(p));
getch();
}
Output:
The size of pointer is : 4 or 8 (bytes)
(depending on system you have)
Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of
pointers are not efficient because they may point to some unknown memory location which
may cause problems in our program and it may lead to crashing of the program. One should
always be careful while working with wild pointers.
#include <stdio.h>
void main()
{
int *p; //wild pointer
printf("n%d",*p);
getch();
}
Output:
Output would be unexpected…………….
Let's take an example.
int *pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Output: 1
Changing Value Pointed by Pointers
Let’s take an example.
int *pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Output: 1
printf("%d", c); // Output: 1
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %pn", &c);
printf("Value of c: %dnn", c); // 22
pc = &c;
printf("Address of pointer pc: %pn", pc);
printf("Content of pointer pc: %dnn", *pc); // 22
c = 11;
printf("Address of pointer pc: %pn", pc);
printf("Content of pointer pc: %dnn", *pc); // 11
*pc = 2;
printf("Address of c: %pn", &c);
printf("Value of c: %dnn", c); // 2
getch();
}
Output
Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2
Difference between %u, %x, %X and %p
1. %u prints the value as an unsigned decimal
2. %x prints the value as a unsigned hex value - using lower case a, b,
c, e, d, f
3. %X prints the value as unsigned hex value using upper case A, B, C,
D, E, F.
4. %p is the proper format string for pointer printing.
Advantages of Pointers
• Pointers are useful for accessing memory locations.
• Pointers provide an efficient way for accessing the elements of an
array structure.
• Pointers are used for dynamic memory allocation as well as
deallocation.
• Pointers are used to form complex data structures such as linked list,
graph, tree, etc.
Disadvantages of Pointers
• Pointers are a little complex to understand.
• If an incorrect value is provided to a pointer, it may cause memory
corruption.
• Pointers are also responsible for memory leakage.
• Programmers find it very difficult to work with the pointers; therefore
it is programmer's responsibility to manipulate a pointer carefully.
Pointers with Array
we access the array elements using its index, but this method can be
eliminated by using pointers. Pointers make it easy to access each array
element.
#include <stdio.h>
void main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p; //pointer declaration
/*the ptr points to the first element of the array*/
p=a; /*We can also type simply ptr==&a[0] */
printf("Printing the array elements using pointern");
for(int i=0;i<5;i++) //loop for traversing array elements
{
printf("n%x",*p); //printing array elements
p++;
//incrementing to the next element, you can also write p=p+1
}
getch();
} The Output is:
1
2
3
4
5
Let’s take another example of “Pointers with Array”.
#include <stdio.h>
void main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
// ptr is assigned the address of the third element
ptr = &x[2];
printf("*ptr = %d n", *ptr); // 3
printf("*(ptr+1) = %d n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
getch();
}
When you run the program,
the output will be:
*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2
Relationship Between Arrays and Pointers
#include <stdio.h>
int main() {
int x[4];
int i;
for(i = 0; i < 4; ++i) {
printf("&x[%d] = %pn", i, &x[i]);
}
printf("Address of array x: %p", x);
getch();
}
output:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
From the above example, it is clear that &x[0] is equivalent to x. And, 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).
...
Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
Pointer and Strings
#include <stdio.h>
#include <string.h>
void main()
{
char str[]="Hello Dafars!";
char *p;
int i;
p=str;
printf("First character is:%cn",*p);
p =p+1;
printf("Next character is:%cn",*p);
printf("Printing all the characters in a
stringn");
p=str; //reset the pointer
for(i=0; i< strlen(str); i++)
{
printf("%cn",*p);
p++;
}
getch();
}
Functions with Array Parameters
In C, we cannot pass an array by value to a function. Whereas, an array name is a pointer
(address), so we just pass an array name to a function which means to pass a pointer to the
array.
int add_array (int *p, int size);
int main()
{
int arr[5] = {100, 220, 37, 16, 98};
printf(“Sum is %dn", add_array(arr, 5));
getch();
}
int add_array (int *p, int size)
{
int total = 0;
int k;
for (k = 0; k < size; k++)
{
total += p[k];
/* it is equivalent to total +=*p ;p++; */
}
return (total);
}
Output:
Sum is 471
Passing Pointers to Functions
#include <stdio.h>
void addOne(int *ptr);
void main()
{
int* p, i = 10;d
p = &i;
addOne(p);
printf("%d", *p); // 11
getch();
}
void addOne(int *ptr)
{
(*ptr)++; // adding 1 to *ptr
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointers
PointersPointers
Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C functions
C functionsC functions
C functions
 
Strings in C
Strings in CStrings in C
Strings in C
 
Strings
StringsStrings
Strings
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
Structure & union
Structure & unionStructure & union
Structure & union
 
structure and union
structure and unionstructure and union
structure and union
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Ähnlich wie Pointers in c - Mohammad Salman

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
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
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
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3YOGESH SINGH
 
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
 

Ähnlich wie Pointers in c - Mohammad Salman (20)

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
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
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
 

Kürzlich hochgeladen

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Kürzlich hochgeladen (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 

Pointers in c - Mohammad Salman

  • 1. Pointers In C? POINTER is a variable that stores address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc. The purpose of pointer is to save memory space and achieve faster execution time.
  • 2. Pointer Syntax data_type *var_name; Example : int *p; char *p; Where, * is used to denote that “p” is pointer variable and not a normal variable. KEY POINTS TO REMEMBER ABOUT POINTERS IN C: 1. Normal variable stores the value whereas pointer variable stores the address of the variable. 2. The content of the C pointer always be a whole number i.e. address. 3. Always C pointer is initialized to null, i.e. int *p = null. 4. The value of null pointer is 0.
  • 3. Let's see some valid pointer declarations int *x; /* pointer to an integer */ int *xyz, dafar; /* xyz is a pointer to type integer and dafar is an integer variable */ double *ptr2; /* pointer to a double */ float *ptr3; /* pointer to a float */ char *ch1 ; /* pointer to a character */ float *ptr, nalayak; /*ptr is a pointer to type float and nalayak is an ordinary float variable */
  • 4. Assigning addresses to Pointers Let's take an example. Int *pc, c; c = 5; pc = &c; Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer. 5 Variable c Value Address 2008 2008 Pointer Variable pc Value Address 5060 NOTE: By the way, * is called the dereference operator (when working with pointers). It operates on a pointer and gives the value stored in that pointer.
  • 5. A Simple program for pointer: #include <stdio.h> void main() { int a=10; // variable declaration int *p; // pointer variable declaration p = &a;// store address of variable ‘a’ in pointer ‘p’ printf(“Address stored in a variable p is: %xn”, p); // accessing the address printf(“Value stored in a variable p is : %dn”, *p); // accessing the value getch(); } Output: Address stored in a variable p is: 60ff08 Value stored in a variable p is: 10
  • 6. Types of a pointer 1. Null pointer 2. void pointer 3. wild pointer
  • 7. Null pointer We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0. #include <stdio.h> void main() { int *p = NULL; //null pointer printf(“The value inside variable p is:%x”,p); } Output: The value inside variable p is : 0
  • 8. void pointer In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable. #include <stdio.h> void main() { void *p = NULL; //void pointer printf("The size of pointer is:%d",sizeof(p)); getch(); } Output: The size of pointer is : 4 or 8 (bytes) (depending on system you have)
  • 9. Wild pointer A pointer is said to be a wild pointer if it is not being initialized to anything. These types of pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers. #include <stdio.h> void main() { int *p; //wild pointer printf("n%d",*p); getch(); } Output: Output would be unexpected…………….
  • 10. Let's take an example. int *pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Output: 1 Changing Value Pointed by Pointers Let’s take an example. int *pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Output: 1 printf("%d", c); // Output: 1
  • 11. #include <stdio.h> int main() { int* pc, c; c = 22; printf("Address of c: %pn", &c); printf("Value of c: %dnn", c); // 22 pc = &c; printf("Address of pointer pc: %pn", pc); printf("Content of pointer pc: %dnn", *pc); // 22 c = 11; printf("Address of pointer pc: %pn", pc); printf("Content of pointer pc: %dnn", *pc); // 11 *pc = 2; printf("Address of c: %pn", &c); printf("Value of c: %dnn", c); // 2 getch(); } Output Address of c: 2686784 Value of c: 22 Address of pointer pc: 2686784 Content of pointer pc: 22 Address of pointer pc: 2686784 Content of pointer pc: 11 Address of c: 2686784 Value of c: 2
  • 12. Difference between %u, %x, %X and %p 1. %u prints the value as an unsigned decimal 2. %x prints the value as a unsigned hex value - using lower case a, b, c, e, d, f 3. %X prints the value as unsigned hex value using upper case A, B, C, D, E, F. 4. %p is the proper format string for pointer printing.
  • 13. Advantages of Pointers • Pointers are useful for accessing memory locations. • Pointers provide an efficient way for accessing the elements of an array structure. • Pointers are used for dynamic memory allocation as well as deallocation. • Pointers are used to form complex data structures such as linked list, graph, tree, etc.
  • 14. Disadvantages of Pointers • Pointers are a little complex to understand. • If an incorrect value is provided to a pointer, it may cause memory corruption. • Pointers are also responsible for memory leakage. • Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully.
  • 15. Pointers with Array we access the array elements using its index, but this method can be eliminated by using pointers. Pointers make it easy to access each array element. #include <stdio.h> void main() { int a[5]={1,2,3,4,5}; //array initialization int *p; //pointer declaration /*the ptr points to the first element of the array*/ p=a; /*We can also type simply ptr==&a[0] */ printf("Printing the array elements using pointern"); for(int i=0;i<5;i++) //loop for traversing array elements { printf("n%x",*p); //printing array elements p++; //incrementing to the next element, you can also write p=p+1 } getch(); } The Output is: 1 2 3 4 5
  • 16. Let’s take another example of “Pointers with Array”. #include <stdio.h> void main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; // ptr is assigned the address of the third element ptr = &x[2]; printf("*ptr = %d n", *ptr); // 3 printf("*(ptr+1) = %d n", *(ptr+1)); // 4 printf("*(ptr-1) = %d", *(ptr-1)); // 2 getch(); } When you run the program, the output will be: *ptr = 3 *(ptr+1) = 4 *(ptr-1) = 2
  • 17. Relationship Between Arrays and Pointers #include <stdio.h> int main() { int x[4]; int i; for(i = 0; i < 4; ++i) { printf("&x[%d] = %pn", i, &x[i]); } printf("Address of array x: %p", x); getch(); } output: &x[0] = 1450734448 &x[1] = 1450734452 &x[2] = 1450734456 &x[3] = 1450734460 Address of array x: 1450734448
  • 18. From the above example, it is clear that &x[0] is equivalent to x. And, 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). ... Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
  • 19. Pointer and Strings #include <stdio.h> #include <string.h> void main() { char str[]="Hello Dafars!"; char *p; int i; p=str; printf("First character is:%cn",*p); p =p+1; printf("Next character is:%cn",*p); printf("Printing all the characters in a stringn"); p=str; //reset the pointer for(i=0; i< strlen(str); i++) { printf("%cn",*p); p++; } getch(); }
  • 20. Functions with Array Parameters In C, we cannot pass an array by value to a function. Whereas, an array name is a pointer (address), so we just pass an array name to a function which means to pass a pointer to the array. int add_array (int *p, int size); int main() { int arr[5] = {100, 220, 37, 16, 98}; printf(“Sum is %dn", add_array(arr, 5)); getch(); } int add_array (int *p, int size) { int total = 0; int k; for (k = 0; k < size; k++) { total += p[k]; /* it is equivalent to total +=*p ;p++; */ } return (total); } Output: Sum is 471
  • 21. Passing Pointers to Functions #include <stdio.h> void addOne(int *ptr); void main() { int* p, i = 10;d p = &i; addOne(p); printf("%d", *p); // 11 getch(); } void addOne(int *ptr) { (*ptr)++; // adding 1 to *ptr }