SlideShare a Scribd company logo
1 of 57
Pointer Basics
Variables are allocated at addresses in computer memory
(address depends on computer/operating system)
Name of the variable is a reference to that memory address
A pointer variable contains a representation of an address of
another variable (P is a pointer variable in the following):
101
V P
Name
Abstract
Representation
Concrete
Representation
Address
4 bytes for
int value 101
4 bytes for
mem address v
v (some value) p (some value)
int V = 101;
int *P = &V;
Pointer Variable Definition
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
– more on how to read complex declarations later
Address (&) Operator
The address (&) operator can be used in front of any
variable object in C -- the result of the operation is
the location in memory of the variable
Syntax: &VariableReference
Examples:
int V;
int *P;
int A[5];
&V - memory location of integer variable V
&(A[2]) - memory location of array element 2 in array A
&P - memory location of pointer variable P
Pointer Variable Initialization/Assignment
NULL - pointer lit constant to non-existent address
– used to indicate pointer points to nothing
Can initialize/assign pointer vars to NULL or use the
address (&) op to get address of a variable
– variable in the address operator must be of the right
type for the pointer (an integer pointer points only at
integer variables)
Examples:
int V;
int *P = &V;
int A[5];
P = &(A[2]);
Indirection (*) Operator
A pointer variable contains a memory address
To refer to the contents of the variable that the
pointer points to, we use indirection operator
Syntax: *PointerVariable
Example:
int V = 101;
int *P = &V;
/* Then *P would refer to the contents of the variable V
(in this case, the integer 101) */
printf(“%d”,*P); /* Prints 101 */
Working with Pointers
In order to work with pointers, do the
following:
• Declare a pointer variable
• Initialize it.
• Access the original value through a pointer
or perform any other operation on it.
Pointer Declaration
• In C, every variable must be declared for its type.
• Since pointer variables contain addresses that belong to a separate data
type, they must be declared as pointers before we use them.
• The general format of format declaration is
– datatype *pt_name;
• This tells the compiler three things about the variable pt_name
1. The asterisk (*) tells that the variable pt_name is a pointer variable.
2. pt_name needs a memory location.
3. pt_name points to variable of type data type. Example: int *ptr; This
declaration tells that the variable points to an integer data type.
INITIALIZING POINTERS:
• Once a pointer variable has been declared, it
can be made to point a variable using an
assignment statement such as
– int i=10,*ptr; ptr=&i;
– or int *ptr=&i;
• Which causes ptr to point to i. That is, ptr
now contains the address of i.
• Note: A pointer declared of one data type
cannot point to variable of another data
type.
ACCESSING A VARIABLE THROUGH ITS
POINTER
• Once a pointer has been assigned the address of a
variable, we can access the value of the variable
using this pointer.
• This is done by using another unary operator
*(asterisk), known as the indirection operator.
Example
Example
Pointer Arithmetic
Valid operations on pointers are:
 Assignment of pointers
 Adding or subtracting a pointer and integer
 Subtracting two pointers
 Comparing two pointers
 Incrementing or decrementing the pointers
 Assigning the value 0 to the pointer.
Invalid operations on pointers are:
 Addition, Multiplication, Division of two pointers
 Multiplication, Division of pointer with a number
Example: Assignment &
Comparision of Pointers
Example: Adding or subtracting a pointer and integer
Example: Subtracting two pointers
Example: Subtracting two pointers
Example: Incrementing or decrementing the
pointers
Invalid operations on pointers
• There are various operations which can not be performed on
pointers. Since, pointer stores address hence we must ignore the
operations which may lead to an illegal address, for example,
addition, and multiplication. A list of such operations is given
below.
• Address + Address = illegal
• Address * Address = illegal
• Address % Address = illegal
• Address / Address = illegal
• Address & Address = illegal
• Address ^ Address = illegal
• Address | Address = illegal
• ~Address = illegal
Pointer to Pointer(Double pointer)
• In C, we can also define a pointer to store the
address of another pointer. Such pointer is known
as a double pointer (pointer to pointer).
• The first pointer is used to store the address of a
variable whereas the second pointer is used to store
the address of the first pointer.
Example
Array of Pointers
• The way there can be an array of ints or an array of
floats, similarly there can be an array of pointers.
• Since a pointer variable always contains an address,
an array of pointers would be nothing but a collection
of addresses.
• The general format of declaring array of pointers is
as follows
• datatype *arr_name[sz];
• Example: int *arr[5];
• This indicates arr is the array which consists of
pointers point to integer data type.
Example
Example
Pointers & Arrays
• You can also use pointers to access arrays.
How Are Pointers Related to Arrays
In C, the name of an array, is actually a pointer to the first
element of the array.
This basically means that we can work with arrays through pointers!
How? Since myNumbers is a pointer to the first element in myNumbers, you can use
the * operator to access it:
• To access the rest of the elements in myNumbers, you can increment
the pointer/array (+1, +2, etc):
• Or loop through it.
• It is also possible to change the value of array elements with pointers:
Example
Pointers and Strings
#include<stdio.h>
int main()
{
char arr[10]=“Hyderabad";
char *s=&arr;
printf("%sn",arr);
printf("%sn",s);
printf("%pn",arr);
printf("%pn",s);
return 0;
}
Hyderabad
Hyderabad
000000000062FE00
000000000062FE00
Null Pointer
• A Null Pointer is a pointer that does not point to any memory
location. It stores the base address of the segment. The null
pointer basically stores the Null value while void is the type
of the pointer.
• A null pointer is a special reserved value which is defined in
a stddef header file. Here, Null means that the pointer is
referring to the 0th memory location.
• If we do not have any address which is to be assigned to the
pointer, then it is known as a null pointer.
• When a NULL value is assigned to the pointer, then it is
considered as a Null pointer.
Applications of Null Pointer
• It is used to initialize o pointer variable when the pointer
does not point to a valid memory address.
• It is used to perform error handling with pointers before
dereferencing the pointers.
• It is passed as a function argument and to return from a
function when we do not want to pass the actual
memory address.
Generic Pointer
• The void pointer in C is a pointer which is not
associated with any data types. It points to some
data location in the storage means points to the
address of variables.
• It is also called general purpose pointer. In C,
malloc() and calloc() functions return void * or
generic pointers.
It has some limitations −
• 1) Pointer arithmetic is not possible with void
pointer due to its concrete size.
• 2) It can’t be used as dereferenced.
Example
Passing Pointer to Function
Call by Reference
• In call by reference, the address of the variable is passed
into the function call as the actual parameter.
• The value of the actual parameters can be modified by
changing the formal parameters since the address of the
actual parameters is passed.
• In call by reference, the memory allocation is similar for
both formal parameters and actual parameters. All the
operations in the function are performed on the value stored
at the address of the actual parameters, and the modified
value gets stored at the same address.
Example
Example
Example: Call by Value
Difference between Call by Value and Call by Reference
Accessing Structure using Pointer
• Pointer pointing to a structure variable is called a structure
pointer, and structures and pointers in C together can be
used to access and change the values of members of the
structure they are pointing.
• Declaring a structure pointer is similar to the declaration of
a structure variable. To declare a structure
pointer struct keyword is used followed by the structure
name and pointer name with an asterisk * symbol.
• Members of a structure can be accessed from pointers
using two ways that are.
– Using dot and asterisk operator on a pointer.
– Using arrow operator (->) on a pointer.
Example
Self referential structures
• Same structure name is referred inside the
structure or a structure refer itself is known as
self-referential structure.
• A self-referential structure is one which contains a
pointer to its own type.
• Ex.
struct student
{
int rno;
char name[50];
struct student *p;
}s1;
Example
Memory allocation for variable is two types.
– Static memory allocation.
– Dynamic memory allocation.
Static memory allocation (SMA) :- The process of
allocating memory to variables at compile time is
called SMA (or) compile time allocation.
• Ex:
– int a[10]= {10,20,30};
– float b[5]={0.5,1.1};
Advantages of Static memory allocation
– Simplicity of usage.
– Efficient execution time.
– Need not worry about memory allocation/re-
allocation/freeing of memory
– Variables remain permanently allocated.
Disadvantages of Static memory allocation
– Main disadvantage is wastage of memory.
– Memory can't be freed when it is no longer
needed.
Dynamic memory allocation (DMA) :- the process of
allocating memory to variables at the runtime is called DMA
(or) runtime allocation.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
– malloc()
– calloc()
– realloc()
– free()
malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory.
realloc() reallocates the memory occupied by malloc()
or calloc() functions.
free() frees the dynamically allocated memory.
Dynamic memory allocation (DMA) :- the process of
allocating memory to variables at the runtime is called DMA
(or) runtime allocation.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
– malloc()
– calloc()
– realloc()
– free()
malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory.
realloc() reallocates the memory occupied by malloc()
or calloc() functions.
free() frees the dynamically allocated memory.
static memory allocation & dynamic memory
allocation
static memory allocation dynamic memory allocation
memory is allocated at compile
time.
memory is allocated at run time.
memory can't be increased while
executing program.
memory can be increased while
executing program.
used in array. used in linked list.
• The malloc() function reserves a block of memory of the
specified number of bytes. And, it returns a pointer of void
which can be casted into pointers of any form.
Syntax of malloc()
– ptr = (castType*) malloc(size);
Example
– ptr = (float*) malloc(100 * sizeof(float));
• The above statement allocates 400 bytes of memory. It's
because the size of float is 4 bytes. And, the pointer ptr
holds the address of the first byte in the allocated memory.
• The expression results in a NULL pointer if the memory
cannot be allocated.
The name "calloc" stands for contiguous allocation.
The malloc() function allocates memory and leaves the
memory uninitialized, whereas the calloc() function allocates
memory and initializes all bits to zero.
Syntax of calloc()
– ptr = (castType*)calloc(n, size);
Example:
– ptr = (float*) calloc(25, sizeof(float));
• The above statement allocates contiguous space in memory
for 25 elements of type float.
free()
• Dynamically allocated memory created with either calloc()
or malloc() doesn't get freed on their own. You must
explicitly use free() to release the space.
Syntax of free()
– free(ptr);
• This statement frees the space allocated in the memory
pointed by ptr.
realloc()
• If the dynamically allocated memory is
insufficient or more than required, you can change
the size of previously allocated memory using the
realloc() function.
Syntax of realloc()
– ptr = realloc(ptr, x);
• Here, ptr is reallocated with a new size x.
Example using malloc() and free()
Example using calloc() and free()
Example using realloc()

More Related Content

Similar to pointers (1).ppt

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
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in CShivanshuVerma11
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT AcademyThe IOT Academy
 
Pointers_in_c_2024.01.10_embedded _c.pptx
Pointers_in_c_2024.01.10_embedded _c.pptxPointers_in_c_2024.01.10_embedded _c.pptx
Pointers_in_c_2024.01.10_embedded _c.pptxahmedbadr608094
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
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
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 

Similar to pointers (1).ppt (20)

Pointer
PointerPointer
Pointer
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
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
 
C pointer
C pointerC pointer
C pointer
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT Academy
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers_in_c_2024.01.10_embedded _c.pptx
Pointers_in_c_2024.01.10_embedded _c.pptxPointers_in_c_2024.01.10_embedded _c.pptx
Pointers_in_c_2024.01.10_embedded _c.pptx
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
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
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointer
PointerPointer
Pointer
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Session 5
Session 5Session 5
Session 5
 

More from Osmania University (11)

Dos_Commands.ppt
Dos_Commands.pptDos_Commands.ppt
Dos_Commands.ppt
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
Students Arsama Brochure (15 × 10 in).pdf
Students Arsama Brochure (15 × 10 in).pdfStudents Arsama Brochure (15 × 10 in).pdf
Students Arsama Brochure (15 × 10 in).pdf
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
introds_110116.pdf
introds_110116.pdfintrods_110116.pdf
introds_110116.pdf
 
1.pdf
1.pdf1.pdf
1.pdf
 
vmls-slides.pdf
vmls-slides.pdfvmls-slides.pdf
vmls-slides.pdf
 
OOAD
OOADOOAD
OOAD
 
CN Jntu PPT
CN Jntu PPTCN Jntu PPT
CN Jntu PPT
 
ISAA
ISAAISAA
ISAA
 
Unit-INP.ppt
Unit-INP.pptUnit-INP.ppt
Unit-INP.ppt
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

pointers (1).ppt

  • 1. Pointer Basics Variables are allocated at addresses in computer memory (address depends on computer/operating system) Name of the variable is a reference to that memory address A pointer variable contains a representation of an address of another variable (P is a pointer variable in the following): 101 V P Name Abstract Representation Concrete Representation Address 4 bytes for int value 101 4 bytes for mem address v v (some value) p (some value) int V = 101; int *P = &V;
  • 2. Pointer Variable Definition Basic syntax: Type *Name Examples: int *P; /* P is var that can point to an int var */ float *Q; /* Q is a float pointer */ char *R; /* R is a char pointer */ Complex example: int *AP[5]; /* AP is an array of 5 pointers to ints */ – more on how to read complex declarations later
  • 3. Address (&) Operator The address (&) operator can be used in front of any variable object in C -- the result of the operation is the location in memory of the variable Syntax: &VariableReference Examples: int V; int *P; int A[5]; &V - memory location of integer variable V &(A[2]) - memory location of array element 2 in array A &P - memory location of pointer variable P
  • 4. Pointer Variable Initialization/Assignment NULL - pointer lit constant to non-existent address – used to indicate pointer points to nothing Can initialize/assign pointer vars to NULL or use the address (&) op to get address of a variable – variable in the address operator must be of the right type for the pointer (an integer pointer points only at integer variables) Examples: int V; int *P = &V; int A[5]; P = &(A[2]);
  • 5. Indirection (*) Operator A pointer variable contains a memory address To refer to the contents of the variable that the pointer points to, we use indirection operator Syntax: *PointerVariable Example: int V = 101; int *P = &V; /* Then *P would refer to the contents of the variable V (in this case, the integer 101) */ printf(“%d”,*P); /* Prints 101 */
  • 6. Working with Pointers In order to work with pointers, do the following: • Declare a pointer variable • Initialize it. • Access the original value through a pointer or perform any other operation on it.
  • 7. Pointer Declaration • In C, every variable must be declared for its type. • Since pointer variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. • The general format of format declaration is – datatype *pt_name; • This tells the compiler three things about the variable pt_name 1. The asterisk (*) tells that the variable pt_name is a pointer variable. 2. pt_name needs a memory location. 3. pt_name points to variable of type data type. Example: int *ptr; This declaration tells that the variable points to an integer data type.
  • 8. INITIALIZING POINTERS: • Once a pointer variable has been declared, it can be made to point a variable using an assignment statement such as – int i=10,*ptr; ptr=&i; – or int *ptr=&i; • Which causes ptr to point to i. That is, ptr now contains the address of i. • Note: A pointer declared of one data type cannot point to variable of another data type.
  • 9. ACCESSING A VARIABLE THROUGH ITS POINTER • Once a pointer has been assigned the address of a variable, we can access the value of the variable using this pointer. • This is done by using another unary operator *(asterisk), known as the indirection operator.
  • 10.
  • 13. Pointer Arithmetic Valid operations on pointers are:  Assignment of pointers  Adding or subtracting a pointer and integer  Subtracting two pointers  Comparing two pointers  Incrementing or decrementing the pointers  Assigning the value 0 to the pointer. Invalid operations on pointers are:  Addition, Multiplication, Division of two pointers  Multiplication, Division of pointer with a number
  • 15. Example: Adding or subtracting a pointer and integer
  • 18. Example: Incrementing or decrementing the pointers
  • 19. Invalid operations on pointers • There are various operations which can not be performed on pointers. Since, pointer stores address hence we must ignore the operations which may lead to an illegal address, for example, addition, and multiplication. A list of such operations is given below. • Address + Address = illegal • Address * Address = illegal • Address % Address = illegal • Address / Address = illegal • Address & Address = illegal • Address ^ Address = illegal • Address | Address = illegal • ~Address = illegal
  • 20. Pointer to Pointer(Double pointer) • In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). • The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.
  • 22. Array of Pointers • The way there can be an array of ints or an array of floats, similarly there can be an array of pointers. • Since a pointer variable always contains an address, an array of pointers would be nothing but a collection of addresses. • The general format of declaring array of pointers is as follows • datatype *arr_name[sz]; • Example: int *arr[5]; • This indicates arr is the array which consists of pointers point to integer data type.
  • 25. Pointers & Arrays • You can also use pointers to access arrays.
  • 26. How Are Pointers Related to Arrays In C, the name of an array, is actually a pointer to the first element of the array. This basically means that we can work with arrays through pointers! How? Since myNumbers is a pointer to the first element in myNumbers, you can use the * operator to access it:
  • 27. • To access the rest of the elements in myNumbers, you can increment the pointer/array (+1, +2, etc): • Or loop through it.
  • 28. • It is also possible to change the value of array elements with pointers:
  • 30. Pointers and Strings #include<stdio.h> int main() { char arr[10]=“Hyderabad"; char *s=&arr; printf("%sn",arr); printf("%sn",s); printf("%pn",arr); printf("%pn",s); return 0; } Hyderabad Hyderabad 000000000062FE00 000000000062FE00
  • 31. Null Pointer • A Null Pointer is a pointer that does not point to any memory location. It stores the base address of the segment. The null pointer basically stores the Null value while void is the type of the pointer. • A null pointer is a special reserved value which is defined in a stddef header file. Here, Null means that the pointer is referring to the 0th memory location. • If we do not have any address which is to be assigned to the pointer, then it is known as a null pointer. • When a NULL value is assigned to the pointer, then it is considered as a Null pointer.
  • 32. Applications of Null Pointer • It is used to initialize o pointer variable when the pointer does not point to a valid memory address. • It is used to perform error handling with pointers before dereferencing the pointers. • It is passed as a function argument and to return from a function when we do not want to pass the actual memory address.
  • 33.
  • 34. Generic Pointer • The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. • It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers. It has some limitations − • 1) Pointer arithmetic is not possible with void pointer due to its concrete size. • 2) It can’t be used as dereferenced.
  • 36. Passing Pointer to Function Call by Reference • In call by reference, the address of the variable is passed into the function call as the actual parameter. • The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed. • In call by reference, the memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.
  • 40. Difference between Call by Value and Call by Reference
  • 41. Accessing Structure using Pointer • Pointer pointing to a structure variable is called a structure pointer, and structures and pointers in C together can be used to access and change the values of members of the structure they are pointing. • Declaring a structure pointer is similar to the declaration of a structure variable. To declare a structure pointer struct keyword is used followed by the structure name and pointer name with an asterisk * symbol. • Members of a structure can be accessed from pointers using two ways that are. – Using dot and asterisk operator on a pointer. – Using arrow operator (->) on a pointer.
  • 43. Self referential structures • Same structure name is referred inside the structure or a structure refer itself is known as self-referential structure. • A self-referential structure is one which contains a pointer to its own type. • Ex. struct student { int rno; char name[50]; struct student *p; }s1;
  • 45.
  • 46. Memory allocation for variable is two types. – Static memory allocation. – Dynamic memory allocation. Static memory allocation (SMA) :- The process of allocating memory to variables at compile time is called SMA (or) compile time allocation. • Ex: – int a[10]= {10,20,30}; – float b[5]={0.5,1.1};
  • 47. Advantages of Static memory allocation – Simplicity of usage. – Efficient execution time. – Need not worry about memory allocation/re- allocation/freeing of memory – Variables remain permanently allocated. Disadvantages of Static memory allocation – Main disadvantage is wastage of memory. – Memory can't be freed when it is no longer needed.
  • 48. Dynamic memory allocation (DMA) :- the process of allocating memory to variables at the runtime is called DMA (or) runtime allocation. The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. – malloc() – calloc() – realloc() – free() malloc() allocates single block of requested memory. calloc() allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory.
  • 49. Dynamic memory allocation (DMA) :- the process of allocating memory to variables at the runtime is called DMA (or) runtime allocation. The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. – malloc() – calloc() – realloc() – free() malloc() allocates single block of requested memory. calloc() allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory.
  • 50. static memory allocation & dynamic memory allocation static memory allocation dynamic memory allocation memory is allocated at compile time. memory is allocated at run time. memory can't be increased while executing program. memory can be increased while executing program. used in array. used in linked list.
  • 51. • The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form. Syntax of malloc() – ptr = (castType*) malloc(size); Example – ptr = (float*) malloc(100 * sizeof(float)); • The above statement allocates 400 bytes of memory. It's because the size of float is 4 bytes. And, the pointer ptr holds the address of the first byte in the allocated memory. • The expression results in a NULL pointer if the memory cannot be allocated.
  • 52. The name "calloc" stands for contiguous allocation. The malloc() function allocates memory and leaves the memory uninitialized, whereas the calloc() function allocates memory and initializes all bits to zero. Syntax of calloc() – ptr = (castType*)calloc(n, size); Example: – ptr = (float*) calloc(25, sizeof(float)); • The above statement allocates contiguous space in memory for 25 elements of type float.
  • 53. free() • Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on their own. You must explicitly use free() to release the space. Syntax of free() – free(ptr); • This statement frees the space allocated in the memory pointed by ptr.
  • 54. realloc() • If the dynamically allocated memory is insufficient or more than required, you can change the size of previously allocated memory using the realloc() function. Syntax of realloc() – ptr = realloc(ptr, x); • Here, ptr is reallocated with a new size x.