SlideShare a Scribd company logo
3
Most read
11
Most read
12
Most read
POINTERS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
Introduction:
In C language, a pointer is a variable that points to or
references a memory location in which data is stored.
Each memory cell in the computer has an address which can
be used to access its location.
A pointer variable points to a memory location. By making
use of pointer, we can access and change the contents of the
memory location.
Pointers contain memory addresses as their values.
What are Pointers?
A pointer is a variable whose value is the
address of another variable, i.e., direct
address of the memory location
Like any variable or constant, you must
declare a pointer before using it to store any
variable address.
The purpose of pointer is to save memory
space and achieve faster execution time.
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
How to Use Pointers?
There are a few important operations, which we will do
with the help of pointers very frequently.
(a) We define a pointer variable,
(b) Assign the address of a variable to a pointer and
(c) Finally access the value at the address available in
the pointer variable. This is done by using unary
operator * that returns the value of the variable located
at the address specified by its operand.
Pointer declaration:
A pointer variable contains the memory location
of another variable. We begin the declaration of a
pointer by specifying the type of data stored in the
location identified by the pointer.
The asterisk tells the compiler that you are
creating a pointer variable. Finally you give the
name of the pointer variable.
The general form of a pointer variable declaration is
type * variablename
Here, type is the pointer's base type; it must be a valid
C data type and variablename is the name of the
pointer variable.
The asterisk * used to declare a pointer is the same
asterisk used for multiplication. However, in this
statement the asterisk is being used to designate a
variable as a pointer.
Example:
int *ptr;
float *string;
Tells that the variable ptr is a pointer variable
ptr needs a memory location
ptr points to a variable of integer type
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers,
whether integer, float, character, or otherwise, is the
same, a long hexadecimal number that represents a
memory address.
#include <stdio.h>
void main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %xn", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %xn", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %dn", *ip );
} Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
NULL Pointers
It is always a good practice to assign a NULL value to a
pointer variable in case you do not have an exact address to
be assigned.
This is done at the time of variable declaration.
A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined
in several standard libraries.
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %xn", ptr);
return 0;
}
The value of ptr is 0
But by convention, if a pointer contains the null (zero) value, it is
Initialize a pointer using Address operator:
After declaring a pointer, we initialize it like standard variables with a
variable address.
To get the address of a variable, we use the ampersand (&)operator,
placed before the name of a variable whose address we need.
Once we declare a pointer variable, we point the variable to another
variable.
We can do this by assigning the address of the variable to the pointer
as in the following example:
◦ptr=&num;
The operator & immediately preceding a variable returns the address of
the variable associated with it.
ptr=&num;
The above declaration places the memory
address of num variable into the pointer
variable ptr.
If num is stored in memory 1260 address then
the pointer variable ptr will contain the
memory address value 1260.
Pointer Variable
int *y = &v;
Instead of storing a value, a pointer y will store the
address of a variable.
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%xn",p);
//accessing the address
printf("Value stored in a variable p is:%dn",*p);
//accessing the value
return 0;
}
/* A program to illustrate pointer declaration */
main()
{
int *ptr;
int sum;
sum=45;
ptr=&sum;
printf (“n sum variable value is %dn”, sum);
printf (“n The 'ptr' pointer variable value is %d”, ptr);
}
What is a scaling factor in C?
Scale factor is the quantity of increment in the value of a
pointer on adding the Data Type.
when we increment a pointer, its value is incremented by the
length of the data type that it points to . This length is called
the scale factor.

More Related Content

PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPT
16717 functions in C++
 
PDF
Unit ii chapter 1 operator and expressions in c
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Constants Variables Datatypes by Mrs. Sowmya Jyothi
16717 functions in C++
 
Unit ii chapter 1 operator and expressions in c
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf

What's hot (20)

PPT
Pointers C programming
PPT
Strings Functions in C Programming
PPTX
Structure in C language
PPTX
concept of Array, 1D & 2D array
PPTX
PPTX
Functions in c language
PPT
Strings
PPTX
Unit 4. Operators and Expression
PPTX
Pointer in c
PPTX
Union in C programming
PPT
Decision making and branching
PPTX
Operator.ppt
PPTX
Pointers in c - Mohammad Salman
PDF
Unit ii chapter 2 Decision making and Branching in C
PDF
Constructor and Destructor
PPTX
C Structures and Unions
PPT
RECURSION IN C
PPT
2. Characteristics of Algorithm.ppt
PPTX
Handling of character strings C programming
PPTX
Variables in C++, data types in c++
Pointers C programming
Strings Functions in C Programming
Structure in C language
concept of Array, 1D & 2D array
Functions in c language
Strings
Unit 4. Operators and Expression
Pointer in c
Union in C programming
Decision making and branching
Operator.ppt
Pointers in c - Mohammad Salman
Unit ii chapter 2 Decision making and Branching in C
Constructor and Destructor
C Structures and Unions
RECURSION IN C
2. Characteristics of Algorithm.ppt
Handling of character strings C programming
Variables in C++, data types in c++
Ad

Similar to POINTERS IN C MRS.SOWMYA JYOTHI.pdf (20)

PPTX
Pointers in C
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPT
Pointer introduction day1
PPTX
4 Pointers.pptx
PPTX
Pointer in c
PPTX
Pointer in c
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPT
pointers (1).ppt
PPTX
Presentation on pointer.
PPTX
Unit 8. Pointers
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PDF
PSPC--UNIT-5.pdf
PPTX
Ponters
PPTX
Pointers in c
PPTX
Pointer in C and its significance, Relationship between pointers and memory a...
PDF
VIT351 Software Development VI Unit3
PPTX
Pointers in c v5 12102017 1
PPTX
Pointers in c language
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Pointers in C
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
Pointer introduction day1
4 Pointers.pptx
Pointer in c
Pointer in c
Pointers and single &multi dimentionalarrays.pptx
pointers (1).ppt
Presentation on pointer.
Unit 8. Pointers
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PSPC--UNIT-5.pdf
Ponters
Pointers in c
Pointer in C and its significance, Relationship between pointers and memory a...
VIT351 Software Development VI Unit3
Pointers in c v5 12102017 1
Pointers in c language
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PDF
Business Ethics Teaching Materials for college
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
Business Ethics Teaching Materials for college
Renaissance Architecture: A Journey from Faith to Humanism
Week 4 Term 3 Study Techniques revisited.pptx
Basic Mud Logging Guide for educational purpose
STATICS OF THE RIGID BODIES Hibbelers.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India

POINTERS IN C MRS.SOWMYA JYOTHI.pdf

  • 1. POINTERS REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
  • 2. Introduction: In C language, a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address which can be used to access its location. A pointer variable points to a memory location. By making use of pointer, we can access and change the contents of the memory location. Pointers contain memory addresses as their values.
  • 3. What are Pointers? A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location Like any variable or constant, you must declare a pointer before using it to store any variable address. The purpose of pointer is to save memory space and achieve faster execution time.
  • 5. How to Use Pointers? There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) Assign the address of a variable to a pointer and (c) Finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
  • 6. Pointer declaration: A pointer variable contains the memory location of another variable. We begin the declaration of a pointer by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the pointer variable.
  • 7. The general form of a pointer variable declaration is type * variablename Here, type is the pointer's base type; it must be a valid C data type and variablename is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
  • 8. Example: int *ptr; float *string; Tells that the variable ptr is a pointer variable ptr needs a memory location ptr points to a variable of integer type
  • 9. int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address.
  • 10. #include <stdio.h> void main () { int var = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &var; /* store address of var in pointer variable*/ printf("Address of var variable: %xn", &var ); /* address stored in pointer variable */ printf("Address stored in ip variable: %xn", ip ); /* access the value using the pointer */ printf("Value of *ip variable: %dn", *ip ); } Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20
  • 11. NULL Pointers It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard libraries.
  • 12. #include <stdio.h> int main () { int *ptr = NULL; printf("The value of ptr is : %xn", ptr); return 0; } The value of ptr is 0 But by convention, if a pointer contains the null (zero) value, it is
  • 13. Initialize a pointer using Address operator: After declaring a pointer, we initialize it like standard variables with a variable address. To get the address of a variable, we use the ampersand (&)operator, placed before the name of a variable whose address we need. Once we declare a pointer variable, we point the variable to another variable. We can do this by assigning the address of the variable to the pointer as in the following example: ◦ptr=&num; The operator & immediately preceding a variable returns the address of the variable associated with it.
  • 14. ptr=&num; The above declaration places the memory address of num variable into the pointer variable ptr. If num is stored in memory 1260 address then the pointer variable ptr will contain the memory address value 1260.
  • 15. Pointer Variable int *y = &v; Instead of storing a value, a pointer y will store the address of a variable.
  • 16. #include <stdio.h> int main() { int a=10; //variable declaration int *p; //pointer variable declaration p=&a; //store address of variable a in pointer p printf("Address stored in a variable p is:%xn",p); //accessing the address printf("Value stored in a variable p is:%dn",*p); //accessing the value return 0; }
  • 17. /* A program to illustrate pointer declaration */ main() { int *ptr; int sum; sum=45; ptr=&sum; printf (“n sum variable value is %dn”, sum); printf (“n The 'ptr' pointer variable value is %d”, ptr); }
  • 18. What is a scaling factor in C? Scale factor is the quantity of increment in the value of a pointer on adding the Data Type. when we increment a pointer, its value is incremented by the length of the data type that it points to . This length is called the scale factor.