SlideShare a Scribd company logo
POINTERS in C
Neethu Narayanan
Assistant Professor
Department of Vocational Studies
St. Mary’s College, Thrissur
POINTERS- DEFINITIONS
 Pointers are variables that contain memory addresses as their
values.
 A variable name directly references a value.
 A pointer indirectly references a value. Referencing a value
through a pointer is called indirection.
 A pointer variable must be declared before it can be used.
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
POINTERS-EXAMPLE
 Examples of pointer declarations:
FILE *fptr;
int *a;
float *b;
char *c;
 The asterisk, when used as above in the declaration, tells the
compiler that the variable is to be a pointer, and the type of data
that the pointer points to, but NOT the name of the variable
pointed to.
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
POINTERS-EXAMPLE
#include <stdio.h>
int main ( )
{
FILE *fptr1 , *fptr2 ; /* Declare two file pointers */
int *aptr ; /* Declare a pointer to an int */
float *bptr ; /* Declare a pointer to a float */
int a ; /* Declare an int variable */
float b ; /* Declare a float variable */
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
POINTERS
aptr = &a ;
bptr = &b ;
fptr2 = fopen ( "my_out_file.dat" , "w" ) ;
fptr1 = fopen ( "my_in_file.dat" , "r" ) ;
if ( fptr1 != NULL )
{
fscanf ( fptr1, "%d%f" , aptr , bptr ) ;
fprintf ( fptr2, "%d %dn" , aptr , bptr ) ;
fprintf ( fptr2, "%d %fn" , *aptr , *bptr ) ;
fprintf ( fptr2, "%d %fn" , a , b ) ;
fprintf ( fptr2, "%d %dn" , &a , &b ) ;
return 0 ;
}
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
Use of & and *
 & -- "address operator" which gives or produces the memory
address of a data variable
 * -- "dereferencing operator" which provides the contents in the
memory location specified by a pointer
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
CONCEPT OF ADDRESS AND POINTERS
 Memory can be conceptualized as a linear set of data locations.
 Variables reference the contents of a locations
 Pointers have a value of the address of a given location.
 Pointers can be used to pass addresses of variables to called functions,
thus allowing the called function to alter the values stored there.
POINTERS AND FUNCTIONS
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
POINTERS AND FUNCTIONS
 Call by Value
Swap function that did not change the values stored in the main
program because only the values were passed to the function swap.
 If instead of passing the values of the variables to the called function,
we pass their addresses, so that the called function can change the
values stored in the calling routine. This is known as "call by
reference" since we are referencing the variables.
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
#include <stdio.h>
void swap ( int *a, int *b ) ;
int main ( )
{
int a = 5, b = 6;
printf("a=%d b=%dn",a,b) ;
swap (&a, &b) ;
printf("a=%d b=%dn",a,b) ;
return 0 ;
}
POINTERS AND FUNCTIONS
void swap( int *a, int *b )
{
int temp;
temp= *a; *a= *b; *b = temp ;
printf ("a=%d b=%dn", *a, *b);
}
Results:
a=5 b=6
a=6 b=5
a=6 b=5
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
 A pointer may be incremented or decremented
 An integer may be added to or subtracted from a pointer.
 Pointer variables may be subtracted from one another.
 Pointer variables can be used in comparisons, but usually only in a
comparison to NULL.
ARITHMETIC AND LOGICAL OPERATIONS on
POINTERS
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
 When an integer is added to or subtracted from a pointer, the new
pointer value is changed by the integer times the number of bytes in
the data variable the pointer is pointing to.
 For example, if the pointer valptr contains the address of a double
precision variable and that address is 234567870, then the statement:
valptr = valptr + 2;
would change valptr to 234567886
ARITHMETIC AND LOGICAL OPERATIONS ON
POINTERS
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
 Creation
& variable Returns variable’s memory address
 Dereference
• pointer Returns contents stored at address
 Indirect assignment
• pointer = val Stores value at address
 Assignment
pointer = ptr Stores pointer in another variable
POINTER OPERATIONS in C
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
USING POINTERS
int i1;
int i2;
int *ptr1;
int *ptr2;
i1 = 1;
i2 = 2;
ptr1 = &i1;
ptr2 = ptr1;
*ptr1 = 3;
i2 = *ptr2;
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
POINTERS ARITHMETIC
pointer + number pointer – number
E.g., pointer + 1 adds 1 something to a pointer
char *p;
char a;
char b;
p = &a;
p += 1;
int *p;
int a;
int b;
p = &a;
p += 1;
In each, p now points to b
(Assuming compiler doesn’t
reorder variables in memory)
Adds 1*sizeof(char) to the
memory address
Adds 1*sizeof(int) to the
memory address
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
REFERENCE
 www.d.umn.edu/~rmaclin/cs1622
 www.clear.rice.edu/comp321
Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur

More Related Content

PPTX
Pointer in c program
PPTX
Pointer in C++
PDF
Pointers
PPTX
POINTERS IN C
PDF
C Pointers
PPTX
Presentation on pointer.
PPTX
Pointers in C Programming
PPTX
Dynamic memory allocation in c++
Pointer in c program
Pointer in C++
Pointers
POINTERS IN C
C Pointers
Presentation on pointer.
Pointers in C Programming
Dynamic memory allocation in c++

What's hot (20)

PDF
Pointers in C
PPTX
Functions in c
PPTX
Pointers in c++
PPTX
Pointer arithmetic in c
PPTX
Functions in C
PPSX
Function in c
PPT
Pointers C programming
PPT
RECURSION IN C
PPTX
Pointer in C
PPTX
Function C programming
PPTX
Array Of Pointers
PDF
Introduction to Python
PPT
Variables in C Programming
PPTX
Union in C programming
PPTX
C Structures and Unions
PPTX
Array in c programming
PPTX
arrays and pointers
PDF
Character Array and String
PPT
Pointers in c
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointers in C
Functions in c
Pointers in c++
Pointer arithmetic in c
Functions in C
Function in c
Pointers C programming
RECURSION IN C
Pointer in C
Function C programming
Array Of Pointers
Introduction to Python
Variables in C Programming
Union in C programming
C Structures and Unions
Array in c programming
arrays and pointers
Character Array and String
Pointers in c
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Ad

Similar to Computer Science:Pointers in C (20)

PPTX
Pointer in c
PPTX
Chp3(pointers ref)
PPTX
pointers.pptx
PPT
pointers (1).ppt
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PPTX
INTERMEDIATE PROGRAMMING POINTERS IN C.pptx
PPT
Programming in Computer Science- Pointers in C++.ppt
PPTX
Pointers in C How to Use Pointers .pptx
PDF
Pointers-Computer programming
PPTX
pointers.pptx
PDF
PSPC--UNIT-5.pdf
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPT
Lect 8(pointers) Zaheer Abbas
PPT
Lect 9(pointers) Zaheer Abbas
PPTX
Pointers
PPTX
4 Pointers.pptx
PPTX
Fundamentals of Pointers in C
PPTX
Understanding the concept of Pointers.pptx
PPTX
Pointers and single &multi dimentionalarrays.pptx
PDF
Pointers
Pointer in c
Chp3(pointers ref)
pointers.pptx
pointers (1).ppt
FYBSC(CS)_UNIT-1_Pointers in C.pptx
INTERMEDIATE PROGRAMMING POINTERS IN C.pptx
Programming in Computer Science- Pointers in C++.ppt
Pointers in C How to Use Pointers .pptx
Pointers-Computer programming
pointers.pptx
PSPC--UNIT-5.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Lect 8(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
Pointers
4 Pointers.pptx
Fundamentals of Pointers in C
Understanding the concept of Pointers.pptx
Pointers and single &multi dimentionalarrays.pptx
Pointers
Ad

More from St Mary's College,Thrissur,Kerala (20)

PPTX
Creative writing and literature
PPTX
PPTX
Mathematics:Cryptography
PPTX
Mathematics:Arithmetical Functions
PPTX
Physical education :Yoga For Stress Relief
PPTX
Psychology:PAIN: Types, Theories and Assessment of pain
PPTX
PPTX
Mathematics:H-Complexity
PPTX
Statistics:Probability Theory
PPTX
Statistics:Fundamentals Of Statistics
PPTX
Economics:Public Revenue
PPTX
Economics:Public Debt
PPTX
Economics:Poverty-perspectives And Dimensions
PPTX
Economics:Economic Integration
PPTX
Economics:Enviornmental Pollution
PPTX
Computer Science:JavaScript
PPTX
Computer Science:Sql Set Operation
PPTX
Computer Science:Java jdbc
PPTX
Microbiology:General Principles of Food Preservation
Creative writing and literature
Mathematics:Cryptography
Mathematics:Arithmetical Functions
Physical education :Yoga For Stress Relief
Psychology:PAIN: Types, Theories and Assessment of pain
Mathematics:H-Complexity
Statistics:Probability Theory
Statistics:Fundamentals Of Statistics
Economics:Public Revenue
Economics:Public Debt
Economics:Poverty-perspectives And Dimensions
Economics:Economic Integration
Economics:Enviornmental Pollution
Computer Science:JavaScript
Computer Science:Sql Set Operation
Computer Science:Java jdbc
Microbiology:General Principles of Food Preservation

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pre independence Education in Inndia.pdf
Microbial disease of the cardiovascular and lymphatic systems
Sports Quiz easy sports quiz sports quiz
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Computing-Curriculum for Schools in Ghana
Anesthesia in Laparoscopic Surgery in India
human mycosis Human fungal infections are called human mycosis..pptx
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
GDM (1) (1).pptx small presentation for students
PPH.pptx obstetrics and gynecology in nursing
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025

Computer Science:Pointers in C

  • 1. POINTERS in C Neethu Narayanan Assistant Professor Department of Vocational Studies St. Mary’s College, Thrissur
  • 2. POINTERS- DEFINITIONS  Pointers are variables that contain memory addresses as their values.  A variable name directly references a value.  A pointer indirectly references a value. Referencing a value through a pointer is called indirection.  A pointer variable must be declared before it can be used. Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 3. POINTERS-EXAMPLE  Examples of pointer declarations: FILE *fptr; int *a; float *b; char *c;  The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to. Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 4. POINTERS-EXAMPLE #include <stdio.h> int main ( ) { FILE *fptr1 , *fptr2 ; /* Declare two file pointers */ int *aptr ; /* Declare a pointer to an int */ float *bptr ; /* Declare a pointer to a float */ int a ; /* Declare an int variable */ float b ; /* Declare a float variable */ Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 5. POINTERS aptr = &a ; bptr = &b ; fptr2 = fopen ( "my_out_file.dat" , "w" ) ; fptr1 = fopen ( "my_in_file.dat" , "r" ) ; if ( fptr1 != NULL ) { fscanf ( fptr1, "%d%f" , aptr , bptr ) ; fprintf ( fptr2, "%d %dn" , aptr , bptr ) ; fprintf ( fptr2, "%d %fn" , *aptr , *bptr ) ; fprintf ( fptr2, "%d %fn" , a , b ) ; fprintf ( fptr2, "%d %dn" , &a , &b ) ; return 0 ; } Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 6. Use of & and *  & -- "address operator" which gives or produces the memory address of a data variable  * -- "dereferencing operator" which provides the contents in the memory location specified by a pointer Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 7. CONCEPT OF ADDRESS AND POINTERS  Memory can be conceptualized as a linear set of data locations.  Variables reference the contents of a locations  Pointers have a value of the address of a given location.  Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. POINTERS AND FUNCTIONS Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 8. POINTERS AND FUNCTIONS  Call by Value Swap function that did not change the values stored in the main program because only the values were passed to the function swap.  If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 9. #include <stdio.h> void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; printf("a=%d b=%dn",a,b) ; swap (&a, &b) ; printf("a=%d b=%dn",a,b) ; return 0 ; } POINTERS AND FUNCTIONS void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf ("a=%d b=%dn", *a, *b); } Results: a=5 b=6 a=6 b=5 a=6 b=5 Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 10.  A pointer may be incremented or decremented  An integer may be added to or subtracted from a pointer.  Pointer variables may be subtracted from one another.  Pointer variables can be used in comparisons, but usually only in a comparison to NULL. ARITHMETIC AND LOGICAL OPERATIONS on POINTERS Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 11.  When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to.  For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr + 2; would change valptr to 234567886 ARITHMETIC AND LOGICAL OPERATIONS ON POINTERS Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 12.  Creation & variable Returns variable’s memory address  Dereference • pointer Returns contents stored at address  Indirect assignment • pointer = val Stores value at address  Assignment pointer = ptr Stores pointer in another variable POINTER OPERATIONS in C Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 13. USING POINTERS int i1; int i2; int *ptr1; int *ptr2; i1 = 1; i2 = 2; ptr1 = &i1; ptr2 = ptr1; *ptr1 = 3; i2 = *ptr2; Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 14. POINTERS ARITHMETIC pointer + number pointer – number E.g., pointer + 1 adds 1 something to a pointer char *p; char a; char b; p = &a; p += 1; int *p; int a; int b; p = &a; p += 1; In each, p now points to b (Assuming compiler doesn’t reorder variables in memory) Adds 1*sizeof(char) to the memory address Adds 1*sizeof(int) to the memory address Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur
  • 15. REFERENCE  www.d.umn.edu/~rmaclin/cs1622  www.clear.rice.edu/comp321 Pointers in C, Neethu Narayanan, St. Mary's College, Thrissur