SlideShare a Scribd company logo
1 of 36
POINTERS
INTRODUCTION
ā€¢ A pointer is a derived data type in C.
ā€¢ Pointers contain memory addresses as their values.
ā€¢ Since these memory addresses are the locations in
the computer memory where program instructions
and data are stored, pointers can be used to access
and manipulate data stored in the memory.
POINTERS BENEFITS TO THE
PROGRAMMER
ā€¢ Pointers are more efficient in handling arrays and data tables.
ā€¢ Pointers can be used to return multiple values from a function via
function arguments.
ā€¢ Pointers permit references to functions and thereby facilitating passing of
function as arguments to other functions.
ā€¢ The use of pointers arrays to character stings results in saving of data
storage space in memory.
ā€¢ Pointers allow C to support dynamic memory management.
ā€¢ Pointers provide an efficient tool for manipulating dynamic data
structures such as structures, linked lists, queues, stacks and trees.
ā€¢ Pointers reduce length and complexity of programs.
ā€¢ They increase the execution speed and thus reduce the program
execution time.
UNDERSTANDING POINTER
ā€¢ The computerā€™s memory is a
sequential collection of storage cells
ā€¢ Each cell. Commonly known as a byte,
has a number called address
associated with it
ā€¢ The address are numbers
consecutively, starting from zero
ā€¢ The last address depends on the
memory size
ā€¢ A computer system having 64K
memory will have its last address as
65,535
POINTER VARIABLE
ā€¢ We may access the value 547 by using either the
name X or the address 4000.
ā€¢ Since memory addresses are simply numbers, they
can be assigned to some variables, that can be
stored in memory, like any other variable.
ā€¢ Such variables that hold memory addresses are
called pointer variables.
ā€¢ A pointer variable is, nothing but a variable that
contains an address, which is a location of another
variable in memory.
ACCESSING THE ADDRESS OF A
VARIABLE
ā€¢ The actual location of a variable in the memory is
system dependent.
ā€¢ We can determine the address of a variable with the
help of the operator & available in C.
ā€¢ The operator & immediately preceding a variable
returns the address of the variable associated with it.
p = &quantity
ā€¢ Would assign the address 5000 to the variable p
CHAIN OF POINTERS
ā€¢ It is possible to make a pointer to point to another pointer,
thus creating a chain of pointers.
ā€¢ The pointer variable p2 contains the address of the pointer
variable p1, which points to the location that contains the
desired value. This is known as multiple indirections.
ā€¢ A variable that is a pointer to a pointer must be declared
using additional indirection operator symbols in front of the
name.
ā€¢ This declaration tells the compiler that p2 is a pointer to a
pointer of int type.
CHAIN OF POINTERS
ā€¢ We can access the target value indirectly pointed to by
pointer to a pointer by applying the indirection operator
twice.
main()
{
int x, *p1, **p2;
x = 100;
p1 = &x; /* address of x */
p2 = &p1; /* address of p1 */
printf("%d", **p2);
}
POINTER EXPRESSION
ā€¢ Like other variables, pointer variables can be used in
expressions.
ā€¢ If p1 and p2 are properly declared and initialized pointers, the
following statements are valid:
y = *p1 * *p2 same as (*p1) * (*p2)
sum = sum + *p1;
z = 5* - *p2/ *p1; same as (5 * (-(*p2)))/(*p1)
*p2 = *p2 +10;
ā€¢ Note the blank space between / and *. the following is wrong
z = 5* - *p2 /*p1;
ā€¢ The symbol /* is considered as the beginning of a comment
POINTER EXPRESSION
ā€¢ C allows us to add integers to or subtract integers from pointers,
as well as to subtract one pointer from another.
p1 + 4, p2 ā€“ 2, p1 - p2 are all allowed
ā€¢ If p1 and p2 are both pointer to the same array, then p2-p1 gives
the number of elements between p1 and p2.
ā€¢ We may also use short-hand operators with the pointers.
p1++; -p2; sum += *p2;
ā€¢ Pointer can also be compared using the relational operators.
p1 > p2, p1 == p2, p1 != p2 are allowed
ā€¢ We may not use pointers in division or multiplication.
p1/p2 or p1 * p2 or p1 / 3 are not allowed
ā€¢ Two pointer cannot be added.
p1 + p2 is illegal
EXAMPLE
main()
{
int a, b, *p1, *p2, x, y, z;
a = 12;
b = 4;
p1 = &a;
p2 = &b;
x = *p1 * *p2 -6;
y = 4* - *p2 / *p1 + 10;
printf("Address of a = %dn", p1);
printf("Address of a = %dn", p2);
printf("n");
printf("a = %d, b = %dn", a, b);
printf("x = %d, y = %dn", x, y);
*p2 = *p2 + 3;
*p1 = *p2 - 5;
z = *p1 * *p2 - 6;
printf("na = %d, b = %d,", a, b);
printf(" z = %dn",z);
}
OUTPUT:
Address of a = 4020
Address of b = 4016
a = 12, b = 4
x = 42, y = 9
a = 2, b = 7, z = 8
Pointer Increments and Scale Factor
ā€¢ Arithmetic operations can be performed on pointers
ā€“ Increment/decrement pointer (++ or --)
ā€“ Add an integer to a pointer( + or += , - or -=)
ā€“ Pointers may be subtracted from each other
ā€¢ The pointers can be incremented like
p1 = p1 + 2;
p1 = p1 + 1;
ā€¢ Expression like
p1++;
ā€¢ Will cause the pointer p1 to point to the next value of its type
ā€¢ When a pointer is incremented, its value is increased by the
ā€˜lengthā€™ of the data type that it points to
ā€¢ This length called the scale factor
Pointers and Arrays
ā€¢ When an array is declared, the compiler allocates a base address
and sufficient amount of storage to contain all the elements of
the array in contiguous memory locations
ā€¢ The base address is the location of the first elements (index 0) of
the array
ā€¢ The compiler also defines the array name as the constant pointer
to the first element
int x[5] = {1, 2, 3, 4, 5};
ā€¢ Base address of x is 1000
ā€¢ Each integer requires two bytes
ā€¢ The name x is defined as a constant pointer pointing to the first
element, x[0]
ā€¢ The value of x is 1000, the location where x[0] is stored
x = &x[0] = 1000
ā€¢ Declare p as an integer pointer, then we can make the pointer p
to point to the array x
p = x;
Pointers and Arrays
ā€¢ This is equivalent to
p = &x[0];
ā€¢ We can access every value of x using p++ to move from one
element to another
ā€¢ The relationship between p and x is
p = &x[0] (=1000)
p+1 = &x[1] (=1002)
p+2 = &x[2] (=1004)
p+3 = &x[3] (=1006)
P+4 = &x[4] (=1008)
ā€¢ The address of an element is calculated using its index and the
scale factor of the data type
Address of x[3] = base address + (3 x scale factor of int)
= 1000 + (3 x 2) = 1006
ā€¢ When handling arrays, instead of using array indexing, we can
use pointers to access array elements.
ā€¢ The pointer accessing method is much faster than array
indexing.
EXAMPLE
main()
{
int *p, sum, i;
int x[5] = {5,9,6,2,7};
i = 0; p = x;
printf("Element Value Addressnn");
while(i<5)
{
printf(" x[%d] %d %dn",i, *p, p);
sum = sum + *p;
i++; p++;
}
printf("n Sum = %dn", sum);
printf("n &x[0] = %dn", &x[0]);
printf("n p = %dn", p);
}
OUTPUT:
Element Value Address
x[0] 5 166
x[1] 9 168
x[2] 6 170
x[3] 3 172
x[4] 7 174
Sum = 55
&x[0] = 166
p = 176
POINTERS TO 2D ARRAYS
ā€¢ Pointers can be used to manipulate two-dimensional arrays as
well.
ā€¢ In a one-dimensional array x, the expression
*(x+i) or *(p+i)
ā€¢ Represents the elements x[i].
ā€¢ An element in a two-dimensional array can be represented by
the pointer expression as:
*(*(a+i)+j) or *(*(p+i)+j)
POINTERS AND CHARACTER STRINGS
ā€¢ The strings are treated as character arrays and therefore they are
declared and initialized as
char str[5] = ā€œgoodā€;
ā€¢ The compiler automatically inserts the null character ā€˜0ā€™ at the
end of the string.
ā€¢ Alternate method to create stings is by using pointer variables of
type char.
char *str = ā€œgoodā€;
ā€¢ This creates a string and then stores its address in the pointer
variable str.
ā€¢ The pointer str now points to the first character of the string
ā€œgoodā€ as:
ā€¢ We can use the run-time assignment for giving values to a
string pointer.
char * string1;
string1 = ā€œgoodā€;
ā€¢ The above assignment is not a string copy, because the
variable string1 is a pointer, not a string.
ā€¢ We can print the content of the string string1 using either
printf or puts functions as
printf(ā€œ%sā€, string1);
puts(string1);
ā€¢ Although string1 is a pointer to the string, it is also the name of
the string. Therefore we do not need to use the indirection
operator * here
POINTERS AND CHARACTER STRINGS
EXAMPLE
main()
{
char *name;
int length;
char *cptr = name;
name = DELHI;
printf("%snā€œ,name);
while(*cptr != '0')
{
pritf("%c is stored at address %dn", *cptr, cptr);
cptr++;
}
length = cptr - name;
printf("nLength of the string = %dn", length);
}
OUTPUT:
DELHI
D is stored at address 54
E is stored at address 55
L is stored at address 56
H is stored at address 57
I is stored at address 58
Length of the string = 5
POINTER AS FUNCTION ARGUMENTS
ā€¢ When an array is passed to a function as an argument, only the
address of the first element of the array is passed, but no the
actual values of the array elements.
ā€¢ If x is an array, when we call sort(x), the address of x[0] is passed
to the function sort.
ā€¢ The function used this address for manipulating the array
elements.
ā€¢ We can also pass the address of a variable as an argument to a
function.
ā€¢ When we pass addresses to function, the parameters receiving
the addresses should be pointers.
ā€¢ The process of calling a function using pointers to pass the
addresses of variables is known as ā€˜call by referenceā€™.
ā€¢ The function called by ā€˜referenceā€™ can change the value of the
variable used in the call.
EXAMPLE
main()
{
int x;
x = 20;
change(&x);
printf("%dn",x);
}
change(int *P)
{
*P = *P + 10;
}
OUTPUT:
30
Pointers with Functions (example)
void swap ( int *, int * ) ;
main ( )
{
int a = 5, b = 6;
printf(ā€œBefore Swapping : a=%d b=%dn",a,b) ;
swap (&a, &b) ;
printf(ā€œAfter swapping : a=%d b=%dn",a,b) ;
}
void swap( int *a, int *b )
{
int temp;
temp= *a; *a= *b; *b = temp ;
}
Results:
Before Swapping : a=5 b=6
After Swapping : a=6 b=5
FUNTION RETURNING POINTERS
ā€¢ Function can also return a pointer to the calling function
int *larger (int *, int *);
main() {
int a = 10, b = 20;
int *p;
p = larger(&a, &b);
printf("%d", *p);
}
int *larger(int *x, int *y)
{
if(*x>*y)
return(x);
else
return(y);
}
THE END

More Related Content

What's hot

Array in c
Array in cArray in c
Array in cRavi Gelani
Ā 
Call by value
Call by valueCall by value
Call by valueDharani G
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
Ā 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
Ā 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
Ā 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
Ā 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
Ā 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
Ā 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
Ā 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arraysjanani thirupathi
Ā 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.Md. Afif Al Mamun
Ā 
Pointer in c
Pointer in cPointer in c
Pointer in cImamul Kadir
Ā 
Data types in C
Data types in CData types in C
Data types in CTarun Sharma
Ā 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
Ā 
Structure in C
Structure in CStructure in C
Structure in CKamal Acharya
Ā 

What's hot (20)

Array in c
Array in cArray in c
Array in c
Ā 
Call by value
Call by valueCall by value
Call by value
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Ā 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ā 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Ā 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
Ā 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Ā 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Ā 
File handling in c
File handling in cFile handling in c
File handling in c
Ā 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
Ā 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
Ā 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
Ā 
String functions in C
String functions in CString functions in C
String functions in C
Ā 
Function C programming
Function C programmingFunction C programming
Function C programming
Ā 
Pointer in c
Pointer in cPointer in c
Pointer in c
Ā 
Data types in C
Data types in CData types in C
Data types in C
Ā 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Ā 
Arrays in c
Arrays in cArrays in c
Arrays in c
Ā 
Structure in C
Structure in CStructure in C
Structure in C
Ā 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Ā 

Viewers also liked

Deep C Programming
Deep C ProgrammingDeep C Programming
Deep C ProgrammingWang Hao Lee
Ā 
C Pointers
C PointersC Pointers
C Pointersomukhtar
Ā 
Difference between structure and union
Difference between structure and unionDifference between structure and union
Difference between structure and unionAppili Vamsi Krishna
Ā 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
Ā 
Types of storage class specifiers in c programming
Types of storage class specifiers in c programmingTypes of storage class specifiers in c programming
Types of storage class specifiers in c programmingAppili Vamsi Krishna
Ā 
Pointers in C
Pointers in CPointers in C
Pointers in Cguestdc3f16
Ā 
a project report on MPPT algorithm for PV panel
a project report on MPPT algorithm for PV panela project report on MPPT algorithm for PV panel
a project report on MPPT algorithm for PV panelgauravchitransh
Ā 
MEP- Building services
MEP- Building servicesMEP- Building services
MEP- Building servicesAniket Khandelwal
Ā 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Appili Vamsi Krishna
Ā 
BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015Appili Vamsi Krishna
Ā 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming Appili Vamsi Krishna
Ā 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
Ā 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
Ā 
Commercial Design Considerations & Solectria Solutions
Commercial Design Considerations & Solectria Solutions Commercial Design Considerations & Solectria Solutions
Commercial Design Considerations & Solectria Solutions Claude Colp
Ā 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
Ā 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
Ā 

Viewers also liked (20)

Deep C Programming
Deep C ProgrammingDeep C Programming
Deep C Programming
Ā 
C Pointers
C PointersC Pointers
C Pointers
Ā 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
Ā 
Difference between structure and union
Difference between structure and unionDifference between structure and union
Difference between structure and union
Ā 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
Ā 
Types of storage class specifiers in c programming
Types of storage class specifiers in c programmingTypes of storage class specifiers in c programming
Types of storage class specifiers in c programming
Ā 
Pointers
PointersPointers
Pointers
Ā 
Pointers in C
Pointers in CPointers in C
Pointers in C
Ā 
a project report on MPPT algorithm for PV panel
a project report on MPPT algorithm for PV panela project report on MPPT algorithm for PV panel
a project report on MPPT algorithm for PV panel
Ā 
MEP- Building services
MEP- Building servicesMEP- Building services
MEP- Building services
Ā 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Ā 
BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015
Ā 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
Ā 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Ā 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Ā 
Commercial Design Considerations & Solectria Solutions
Commercial Design Considerations & Solectria Solutions Commercial Design Considerations & Solectria Solutions
Commercial Design Considerations & Solectria Solutions
Ā 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
Ā 
C pointers
C pointersC pointers
C pointers
Ā 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
Ā 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
Ā 

Similar to Pointers C programming

PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
Ā 
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
Ā 
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
Ā 
Pointer in C
Pointer in CPointer in C
Pointer in Cbipchulabmki
Ā 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxsangeeta borde
Ā 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
Ā 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxAmitRout25
Ā 
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
Ā 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
Ā 

Similar to Pointers C programming (20)

PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
Ā 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ā 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
Ā 
Pointers
PointersPointers
Pointers
Ā 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
Ā 
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...
Ā 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
Ā 
Pointer in C
Pointer in CPointer in C
Pointer in C
Ā 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
Ā 
Pointers
PointersPointers
Pointers
Ā 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
Ā 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
Ā 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
Ā 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
Ā 
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
Ā 
Session 5
Session 5Session 5
Session 5
Ā 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
Ā 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
Ā 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
Ā 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
Ā 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
Ā 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
Ā 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
Ā 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
Ā 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
Ā 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
Ā 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
Ā 
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...Nguyen Thanh Tu Collection
Ā 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
Ā 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
Ā 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A BeƱa
Ā 
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
Ā 
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)lakshayb543
Ā 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
Ā 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
Ā 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...SeƔn Kennedy
Ā 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
Ā 

Recently uploaded (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
Ā 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
Ā 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
Ā 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
Ā 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
Ā 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Ā 
Model Call Girl in Tilak Nagar Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at šŸ”9953056974šŸ”Model Call Girl in Tilak Nagar Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at šŸ”9953056974šŸ”
Ā 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Ā 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
Ā 
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...
Hį»ŒC Tį»T TIįŗ¾NG ANH 11 THEO CHĘÆĘ NG TRƌNH GLOBAL SUCCESS ĐƁP ƁN CHI TIįŗ¾T - Cįŗ¢ NĂ...
Ā 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
Ā 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
Ā 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
Ā 
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSĀ® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
Ā 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
Ā 
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)
Visit to a blind student's schoolšŸ§‘ā€šŸ¦ÆšŸ§‘ā€šŸ¦Æ(community medicine)
Ā 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
Ā 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Ā 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
Ā 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
Ā 

Pointers C programming

  • 2. INTRODUCTION ā€¢ A pointer is a derived data type in C. ā€¢ Pointers contain memory addresses as their values. ā€¢ Since these memory addresses are the locations in the computer memory where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory.
  • 3. POINTERS BENEFITS TO THE PROGRAMMER ā€¢ Pointers are more efficient in handling arrays and data tables. ā€¢ Pointers can be used to return multiple values from a function via function arguments. ā€¢ Pointers permit references to functions and thereby facilitating passing of function as arguments to other functions. ā€¢ The use of pointers arrays to character stings results in saving of data storage space in memory. ā€¢ Pointers allow C to support dynamic memory management. ā€¢ Pointers provide an efficient tool for manipulating dynamic data structures such as structures, linked lists, queues, stacks and trees. ā€¢ Pointers reduce length and complexity of programs. ā€¢ They increase the execution speed and thus reduce the program execution time.
  • 4. UNDERSTANDING POINTER ā€¢ The computerā€™s memory is a sequential collection of storage cells ā€¢ Each cell. Commonly known as a byte, has a number called address associated with it ā€¢ The address are numbers consecutively, starting from zero ā€¢ The last address depends on the memory size ā€¢ A computer system having 64K memory will have its last address as 65,535
  • 5.
  • 6. POINTER VARIABLE ā€¢ We may access the value 547 by using either the name X or the address 4000. ā€¢ Since memory addresses are simply numbers, they can be assigned to some variables, that can be stored in memory, like any other variable. ā€¢ Such variables that hold memory addresses are called pointer variables. ā€¢ A pointer variable is, nothing but a variable that contains an address, which is a location of another variable in memory.
  • 7. ACCESSING THE ADDRESS OF A VARIABLE ā€¢ The actual location of a variable in the memory is system dependent. ā€¢ We can determine the address of a variable with the help of the operator & available in C. ā€¢ The operator & immediately preceding a variable returns the address of the variable associated with it. p = &quantity ā€¢ Would assign the address 5000 to the variable p
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19. CHAIN OF POINTERS ā€¢ It is possible to make a pointer to point to another pointer, thus creating a chain of pointers. ā€¢ The pointer variable p2 contains the address of the pointer variable p1, which points to the location that contains the desired value. This is known as multiple indirections. ā€¢ A variable that is a pointer to a pointer must be declared using additional indirection operator symbols in front of the name. ā€¢ This declaration tells the compiler that p2 is a pointer to a pointer of int type.
  • 20. CHAIN OF POINTERS ā€¢ We can access the target value indirectly pointed to by pointer to a pointer by applying the indirection operator twice. main() { int x, *p1, **p2; x = 100; p1 = &x; /* address of x */ p2 = &p1; /* address of p1 */ printf("%d", **p2); }
  • 21. POINTER EXPRESSION ā€¢ Like other variables, pointer variables can be used in expressions. ā€¢ If p1 and p2 are properly declared and initialized pointers, the following statements are valid: y = *p1 * *p2 same as (*p1) * (*p2) sum = sum + *p1; z = 5* - *p2/ *p1; same as (5 * (-(*p2)))/(*p1) *p2 = *p2 +10; ā€¢ Note the blank space between / and *. the following is wrong z = 5* - *p2 /*p1; ā€¢ The symbol /* is considered as the beginning of a comment
  • 22. POINTER EXPRESSION ā€¢ C allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another. p1 + 4, p2 ā€“ 2, p1 - p2 are all allowed ā€¢ If p1 and p2 are both pointer to the same array, then p2-p1 gives the number of elements between p1 and p2. ā€¢ We may also use short-hand operators with the pointers. p1++; -p2; sum += *p2; ā€¢ Pointer can also be compared using the relational operators. p1 > p2, p1 == p2, p1 != p2 are allowed ā€¢ We may not use pointers in division or multiplication. p1/p2 or p1 * p2 or p1 / 3 are not allowed ā€¢ Two pointer cannot be added. p1 + p2 is illegal
  • 23. EXAMPLE main() { int a, b, *p1, *p2, x, y, z; a = 12; b = 4; p1 = &a; p2 = &b; x = *p1 * *p2 -6; y = 4* - *p2 / *p1 + 10; printf("Address of a = %dn", p1); printf("Address of a = %dn", p2); printf("n"); printf("a = %d, b = %dn", a, b); printf("x = %d, y = %dn", x, y); *p2 = *p2 + 3; *p1 = *p2 - 5; z = *p1 * *p2 - 6; printf("na = %d, b = %d,", a, b); printf(" z = %dn",z); } OUTPUT: Address of a = 4020 Address of b = 4016 a = 12, b = 4 x = 42, y = 9 a = 2, b = 7, z = 8
  • 24. Pointer Increments and Scale Factor ā€¢ Arithmetic operations can be performed on pointers ā€“ Increment/decrement pointer (++ or --) ā€“ Add an integer to a pointer( + or += , - or -=) ā€“ Pointers may be subtracted from each other ā€¢ The pointers can be incremented like p1 = p1 + 2; p1 = p1 + 1; ā€¢ Expression like p1++; ā€¢ Will cause the pointer p1 to point to the next value of its type ā€¢ When a pointer is incremented, its value is increased by the ā€˜lengthā€™ of the data type that it points to ā€¢ This length called the scale factor
  • 25. Pointers and Arrays ā€¢ When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations ā€¢ The base address is the location of the first elements (index 0) of the array ā€¢ The compiler also defines the array name as the constant pointer to the first element int x[5] = {1, 2, 3, 4, 5}; ā€¢ Base address of x is 1000 ā€¢ Each integer requires two bytes ā€¢ The name x is defined as a constant pointer pointing to the first element, x[0] ā€¢ The value of x is 1000, the location where x[0] is stored x = &x[0] = 1000 ā€¢ Declare p as an integer pointer, then we can make the pointer p to point to the array x p = x;
  • 26. Pointers and Arrays ā€¢ This is equivalent to p = &x[0]; ā€¢ We can access every value of x using p++ to move from one element to another ā€¢ The relationship between p and x is p = &x[0] (=1000) p+1 = &x[1] (=1002) p+2 = &x[2] (=1004) p+3 = &x[3] (=1006) P+4 = &x[4] (=1008) ā€¢ The address of an element is calculated using its index and the scale factor of the data type Address of x[3] = base address + (3 x scale factor of int) = 1000 + (3 x 2) = 1006 ā€¢ When handling arrays, instead of using array indexing, we can use pointers to access array elements. ā€¢ The pointer accessing method is much faster than array indexing.
  • 27. EXAMPLE main() { int *p, sum, i; int x[5] = {5,9,6,2,7}; i = 0; p = x; printf("Element Value Addressnn"); while(i<5) { printf(" x[%d] %d %dn",i, *p, p); sum = sum + *p; i++; p++; } printf("n Sum = %dn", sum); printf("n &x[0] = %dn", &x[0]); printf("n p = %dn", p); } OUTPUT: Element Value Address x[0] 5 166 x[1] 9 168 x[2] 6 170 x[3] 3 172 x[4] 7 174 Sum = 55 &x[0] = 166 p = 176
  • 28. POINTERS TO 2D ARRAYS ā€¢ Pointers can be used to manipulate two-dimensional arrays as well. ā€¢ In a one-dimensional array x, the expression *(x+i) or *(p+i) ā€¢ Represents the elements x[i]. ā€¢ An element in a two-dimensional array can be represented by the pointer expression as: *(*(a+i)+j) or *(*(p+i)+j)
  • 29. POINTERS AND CHARACTER STRINGS ā€¢ The strings are treated as character arrays and therefore they are declared and initialized as char str[5] = ā€œgoodā€; ā€¢ The compiler automatically inserts the null character ā€˜0ā€™ at the end of the string. ā€¢ Alternate method to create stings is by using pointer variables of type char. char *str = ā€œgoodā€; ā€¢ This creates a string and then stores its address in the pointer variable str. ā€¢ The pointer str now points to the first character of the string ā€œgoodā€ as:
  • 30. ā€¢ We can use the run-time assignment for giving values to a string pointer. char * string1; string1 = ā€œgoodā€; ā€¢ The above assignment is not a string copy, because the variable string1 is a pointer, not a string. ā€¢ We can print the content of the string string1 using either printf or puts functions as printf(ā€œ%sā€, string1); puts(string1); ā€¢ Although string1 is a pointer to the string, it is also the name of the string. Therefore we do not need to use the indirection operator * here POINTERS AND CHARACTER STRINGS
  • 31. EXAMPLE main() { char *name; int length; char *cptr = name; name = DELHI; printf("%snā€œ,name); while(*cptr != '0') { pritf("%c is stored at address %dn", *cptr, cptr); cptr++; } length = cptr - name; printf("nLength of the string = %dn", length); } OUTPUT: DELHI D is stored at address 54 E is stored at address 55 L is stored at address 56 H is stored at address 57 I is stored at address 58 Length of the string = 5
  • 32. POINTER AS FUNCTION ARGUMENTS ā€¢ When an array is passed to a function as an argument, only the address of the first element of the array is passed, but no the actual values of the array elements. ā€¢ If x is an array, when we call sort(x), the address of x[0] is passed to the function sort. ā€¢ The function used this address for manipulating the array elements. ā€¢ We can also pass the address of a variable as an argument to a function. ā€¢ When we pass addresses to function, the parameters receiving the addresses should be pointers. ā€¢ The process of calling a function using pointers to pass the addresses of variables is known as ā€˜call by referenceā€™. ā€¢ The function called by ā€˜referenceā€™ can change the value of the variable used in the call.
  • 33. EXAMPLE main() { int x; x = 20; change(&x); printf("%dn",x); } change(int *P) { *P = *P + 10; } OUTPUT: 30
  • 34. Pointers with Functions (example) void swap ( int *, int * ) ; main ( ) { int a = 5, b = 6; printf(ā€œBefore Swapping : a=%d b=%dn",a,b) ; swap (&a, &b) ; printf(ā€œAfter swapping : a=%d b=%dn",a,b) ; } void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; } Results: Before Swapping : a=5 b=6 After Swapping : a=6 b=5
  • 35. FUNTION RETURNING POINTERS ā€¢ Function can also return a pointer to the calling function int *larger (int *, int *); main() { int a = 10, b = 20; int *p; p = larger(&a, &b); printf("%d", *p); } int *larger(int *x, int *y) { if(*x>*y) return(x); else return(y); }

Editor's Notes

  1. Instructor/Narrator: Here is a modified swap program. Notice the swap function now requires pointer variables as input (both in the function and function prototype), and that all operations are done by dereferencing the pointers within the swap function to get the values and move them around with the aid of the local ā€œtempā€ variable. In the main() function the addresses of the a and b variable are passed to the swap() function (given by &amp;a and &amp;b). The results are now what would be expected of a swap function.