SlideShare a Scribd company logo
1
“Pointers”
2
Pointer Basics
Pointer Definition and syntax
The pointer in C language is a variable which stores the address of
another variable. This variable can be of type int, char, array, or any
other pointer.
Syntax:
Data_type *variable_name;
• Asterisk(*)is called as Indirection Operator. It is also called as
Value at Address Operator.
• It Indicates Variable declared is of Pointer type. variable_name
must follow the rules of an identifier.
Example
int *ip; //pointer to an integer
double *dp //pointer to double
Pointer Basics
•Normal variable stores the value whereas pointer variable stores the
address of the variable.
•The content of the C pointer always be a whole number i.e. address.
•Always C pointer is initialized to null, i.e. int *p = null.
•The value of null pointer is 0.
•& symbol is used to get the address of the variable.
•* symbol is used to get the value of the variable that the pointer is
pointing to.
•If a pointer in C is assigned to NULL, it means it is pointing to
nothing.
Pointer concept with diagrams
int i=5;
int*j;
j=&i;
Pointer Basic Example
#include <stdio.h>
void main() q ptr
{
int *ptr, q;
q = 50; 5025 5045
ptr = &q;
printf(“Value of q =%dt", q);
printf(“Address of q =%un", &q);
printf(“Address of ptr =%ut", &ptr);
printf(“Value of ptr =%d", *ptr);
return 0;
}
OUTPUT:
Value of q = 50 Address of q = 5025
Address of ptr = 5045 Value of ptr = 50
50 5025
Program on Reference and De-reference operator
#include <stdio.h>
int main()
{
int *pc, c=22;
printf("Address of c:%un",&c);
printf("Value of c:%dnn",c);
pc=&c;
printf("Address of pointer pc:%un",pc);
printf("Content of pointer pc:%dn",*pc);
c=11;
printf("Address of pointer pc:%un",pc);
printf("Content of pointer pc:%dn",*pc);
*pc=2;
printf("Address of c:%un",&c);
printf("Value of c:%dn",c);
return 0;
}
7
Output
• Address of c:26867
• Value of c:22
• Address of pointer pc:26867
• Content of pointer pc:22
• Address of pointer pc:26867
• Content of pointer pc:11
• Address of c:26867
• Value of c:2
8
PointerArithmetic
Pointer Expression How it is evaluated ?
ip = ip + 1 ip => ip + 1 => 1000 + 1*4 => 1004
ip++ or ++ip ip++ => ip + 1 => 1004 + 1*4 => 1008
ip = ip + 5 ip => ip + 5 => 1008 + 5*4 => 1028
ip = ip - 2 ip => ip - 2 => 1028 - 2*4 => 1020
ip-- or --ip ip => ip - 1 => 1020 - 1*4 => 1016
Pointer Arithmetic on Integers
9
Pointer to Pointer
If a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer
Syntax:
int **ptr;
10
Pointer to Pointer Example
#include<stdio.h>
void main()
{
int a, *p1, **p2;
a=65;
p1=&a;
p2=&p1;
printf("a = %dn", a);//65
printf("address of a = %dn", &a);//5000
printf("p1 = %dn", p1);//5000
printf("address p1 = %dn", &p1);//6000
printf("*p1 = %dn", *p1);//65
printf("p2 = %dn", p2);//6000
printf("*p2 = %dn", *p2);//5000
printf("**p2 = %dn", **p2);//65
}
11
Pointer to Arrays
• When an array is declared, compiler allocates sufficient amount of
memory to contain all the elements of the array.
• Base address i.e address of the first element of the array is also
allocated by the compiler.
Example:
int x[4];
From the above example,
• &x[0] is equivalent to x.
• x[0] is equivalent to *x.
Similarly,
• &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
• &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
Example 1: Pointers and Arrays
#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]);
sum += *(x+i); // Equivalent to sum += x[i]
}
printf("Sum = %d", sum);
return 0;
}
The program computes the sum of six elements entered by the user.
Example 2: Pointers and Arrays
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2]; // ptr is assigned the address of the third element
printf("*ptr = %d n", *ptr); // 3
printf("*(ptr+1) = %d n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
return 0;
}
14
Pointers as Function Argument in C
• Pointer as a function parameter is used to hold addresses of
arguments passed during function call. This is also known as call
by reference.
• When a function is called by reference any change made to the
reference variable will effect the original variable.
• The address of num1 and num2 are passed to the swap() function
using swap(&num1, &num2);.
• Pointers n1 and n2 accept these arguments in the function
definition.
void swap(int* n1, int* n2)
{
... ..
}
15
Call by Reference Example
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 10, n = 20;
printf("m = %dn", m);
printf("n = %dnn", n);
swap(&m, &n); //passing address
printf("After Swapping:nn");
printf("m = %dn", m);
printf("n = %d", n);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Output:
m = 10
n = 20
After Swapping:
m = 20
n = 10
16
Pointer to Structure
Accessing Structure Members with Pointer
• To access members of structure using the structure variable, we used
the dot . operator.
• But when we have a pointer of structure type, we use arrow -> to
access structure members.
Example:
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
}
personPtr -> age is equivalent to
(*personPtr).age
personPtr -> weight is equivalent to
(*personPtr).weight
17
Pointer to Structure Example
#include <stdio.h>
struct my_structure
{
char name[20];
int number;
int rank;
};
int main()
{
struct my_structure variable = {“Sachin", 100, 1};
struct my_structure *ptr;
ptr = &variable;
printf("NAME: %sn", ptr->name);
printf("NUMBER: %dn", ptr->number);
printf("RANK: %d", ptr->rank);
return 0;
}
18
Self Referential Structure
Self Referential structures are those structures that have one or
more pointers which point to the same type of structure, as their
member.
In other words, structures pointing to the same type of structures are
self-referential in nature.
19
Self Referential Structure Example
#include<stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
void main()
{
struct node ob1;
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
struct node ob2;
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
ob1.link = &ob2; // Linking ob1 and ob2
/*Accessing data members of ob2 using
ob1*/
printf("%d", ob1.link->data1);
printf("n%d", ob1.link->data2);
}
Enumeration (or enum) in C
Enumeration (or enum) is a user defined data type in C. It is mainly
used to assign names to integral constants, the names make a program
easy to read and maintain.
• To define enums, the enum keyword is used.
Syntax:
enum flag {const1, const2, ..., constN};
By default, const1 is 0, const2 is 1 and so on. You can change default
values of enum elements during declaration (if necessary).
Enumeration (or enum) in C Example
#include<stdio.h>
enum week{Mon, Tue,
Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output: 2
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May,
Jun, Jul,Aug, Sep, Oct, Nov, Dec};
int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);
return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11
22
Bitfields in C
Bit Field Declaration
The declaration of a bit-field has the following form inside a structure −
struct {
type [member_name1] : width ;
type [member_name2] : width ;
};
Sr.No. Element & Description
1
type
An integer type that determines how a bit-field's value is interpreted.
The type may be int, signed int, or unsigned int.
2 member_name
The name of the bit-field.
3 width
The number of bits in the bit-field. The width must be less than or
equal to the bit width of the specified type.
23
Bitfields Example
#include <stdio.h>
struct stat{
unsigned int width;
unsigned int height;
} status1;
struct stats{
unsigned int width : 1;
unsigned int height : 1;
} status2;
int main( ) {
printf( "Memory size occupied by status1 : %dn", sizeof(status1));
printf( "Memory size occupied by status2 : %dn", sizeof(status2));
return 0;
}
Output:
Memory size occupied by status1 : 4
Memory size occupied by status2 : 1
24
“Thank You”
Ad

More Related Content

What's hot (20)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
Strings
StringsStrings
Strings
Mitali Chugh
 
structure and union
structure and unionstructure and union
structure and union
student
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
Hridoy Bepari
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C keywords and identifiers
C keywords and identifiersC keywords and identifiers
C keywords and identifiers
Akhileshwar Reddy Ankireddy
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
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
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
Darshan Patel
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Monishkanungo
 
Character set of c
Character set of cCharacter set of c
Character set of c
Chandrapriya Rediex
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 

Similar to Pointers in C Language (20)

UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
aarockiaabinsAPIICSE
 
Algoritmos e Estruturas de Dados - Pointers
Algoritmos e Estruturas de Dados - PointersAlgoritmos e Estruturas de Dados - Pointers
Algoritmos e Estruturas de Dados - Pointers
martijnkuipersandebo
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
clashontitanth11
 
Pointers
PointersPointers
Pointers
Frijo Francis
 
C programming
C programmingC programming
C programming
Karthikeyan A K
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
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
sudhakargeruganti
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
Rubal Bansal
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
dhanajimirajkar1
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Algoritmos e Estruturas de Dados - Pointers
Algoritmos e Estruturas de Dados - PointersAlgoritmos e Estruturas de Dados - Pointers
Algoritmos e Estruturas de Dados - Pointers
martijnkuipersandebo
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
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
sudhakargeruganti
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Ad

Recently uploaded (20)

Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Ad

Pointers in C Language

  • 2. 2 Pointer Basics Pointer Definition and syntax The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, or any other pointer. Syntax: Data_type *variable_name; • Asterisk(*)is called as Indirection Operator. It is also called as Value at Address Operator. • It Indicates Variable declared is of Pointer type. variable_name must follow the rules of an identifier. Example int *ip; //pointer to an integer double *dp //pointer to double
  • 3. Pointer Basics •Normal variable stores the value whereas pointer variable stores the address of the variable. •The content of the C pointer always be a whole number i.e. address. •Always C pointer is initialized to null, i.e. int *p = null. •The value of null pointer is 0. •& symbol is used to get the address of the variable. •* symbol is used to get the value of the variable that the pointer is pointing to. •If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • 4. Pointer concept with diagrams int i=5; int*j; j=&i;
  • 5. Pointer Basic Example #include <stdio.h> void main() q ptr { int *ptr, q; q = 50; 5025 5045 ptr = &q; printf(“Value of q =%dt", q); printf(“Address of q =%un", &q); printf(“Address of ptr =%ut", &ptr); printf(“Value of ptr =%d", *ptr); return 0; } OUTPUT: Value of q = 50 Address of q = 5025 Address of ptr = 5045 Value of ptr = 50 50 5025
  • 6. Program on Reference and De-reference operator #include <stdio.h> int main() { int *pc, c=22; printf("Address of c:%un",&c); printf("Value of c:%dnn",c); pc=&c; printf("Address of pointer pc:%un",pc); printf("Content of pointer pc:%dn",*pc); c=11; printf("Address of pointer pc:%un",pc); printf("Content of pointer pc:%dn",*pc); *pc=2; printf("Address of c:%un",&c); printf("Value of c:%dn",c); return 0; }
  • 7. 7 Output • Address of c:26867 • Value of c:22 • Address of pointer pc:26867 • Content of pointer pc:22 • Address of pointer pc:26867 • Content of pointer pc:11 • Address of c:26867 • Value of c:2
  • 8. 8 PointerArithmetic Pointer Expression How it is evaluated ? ip = ip + 1 ip => ip + 1 => 1000 + 1*4 => 1004 ip++ or ++ip ip++ => ip + 1 => 1004 + 1*4 => 1008 ip = ip + 5 ip => ip + 5 => 1008 + 5*4 => 1028 ip = ip - 2 ip => ip - 2 => 1028 - 2*4 => 1020 ip-- or --ip ip => ip - 1 => 1020 - 1*4 => 1016 Pointer Arithmetic on Integers
  • 9. 9 Pointer to Pointer If a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer Syntax: int **ptr;
  • 10. 10 Pointer to Pointer Example #include<stdio.h> void main() { int a, *p1, **p2; a=65; p1=&a; p2=&p1; printf("a = %dn", a);//65 printf("address of a = %dn", &a);//5000 printf("p1 = %dn", p1);//5000 printf("address p1 = %dn", &p1);//6000 printf("*p1 = %dn", *p1);//65 printf("p2 = %dn", p2);//6000 printf("*p2 = %dn", *p2);//5000 printf("**p2 = %dn", **p2);//65 }
  • 11. 11 Pointer to Arrays • When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. • Base address i.e address of the first element of the array is also allocated by the compiler. Example: int x[4]; From the above example, • &x[0] is equivalent to x. • x[0] is equivalent to *x. Similarly, • &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1). • &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
  • 12. Example 1: Pointers and Arrays #include <stdio.h> int main() { int i, x[6], sum = 0; printf("Enter 6 numbers: "); for(i = 0; i < 6; ++i) { scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]); sum += *(x+i); // Equivalent to sum += x[i] } printf("Sum = %d", sum); return 0; } The program computes the sum of six elements entered by the user.
  • 13. Example 2: Pointers and Arrays #include <stdio.h> int main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; ptr = &x[2]; // ptr is assigned the address of the third element printf("*ptr = %d n", *ptr); // 3 printf("*(ptr+1) = %d n", *(ptr+1)); // 4 printf("*(ptr-1) = %d", *(ptr-1)); // 2 return 0; }
  • 14. 14 Pointers as Function Argument in C • Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. • When a function is called by reference any change made to the reference variable will effect the original variable. • The address of num1 and num2 are passed to the swap() function using swap(&num1, &num2);. • Pointers n1 and n2 accept these arguments in the function definition. void swap(int* n1, int* n2) { ... .. }
  • 15. 15 Call by Reference Example #include <stdio.h> void swap(int *a, int *b); int main() { int m = 10, n = 20; printf("m = %dn", m); printf("n = %dnn", n); swap(&m, &n); //passing address printf("After Swapping:nn"); printf("m = %dn", m); printf("n = %d", n); return 0; } void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Output: m = 10 n = 20 After Swapping: m = 20 n = 10
  • 16. 16 Pointer to Structure Accessing Structure Members with Pointer • To access members of structure using the structure variable, we used the dot . operator. • But when we have a pointer of structure type, we use arrow -> to access structure members. Example: struct person { int age; float weight; }; int main() { struct person *personPtr, person1; } personPtr -> age is equivalent to (*personPtr).age personPtr -> weight is equivalent to (*personPtr).weight
  • 17. 17 Pointer to Structure Example #include <stdio.h> struct my_structure { char name[20]; int number; int rank; }; int main() { struct my_structure variable = {“Sachin", 100, 1}; struct my_structure *ptr; ptr = &variable; printf("NAME: %sn", ptr->name); printf("NUMBER: %dn", ptr->number); printf("RANK: %d", ptr->rank); return 0; }
  • 18. 18 Self Referential Structure Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature.
  • 19. 19 Self Referential Structure Example #include<stdio.h> struct node { int data1; char data2; struct node* link; }; void main() { struct node ob1; ob1.link = NULL; ob1.data1 = 10; ob1.data2 = 20; struct node ob2; ob2.link = NULL; ob2.data1 = 30; ob2.data2 = 40; ob1.link = &ob2; // Linking ob1 and ob2 /*Accessing data members of ob2 using ob1*/ printf("%d", ob1.link->data1); printf("n%d", ob1.link->data2); }
  • 20. Enumeration (or enum) in C Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. • To define enums, the enum keyword is used. Syntax: enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).
  • 21. Enumeration (or enum) in C Example #include<stdio.h> enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; } Output: 2 #include<stdio.h> enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,Aug, Sep, Oct, Nov, Dec}; int main() { int i; for (i=Jan; i<=Dec; i++) printf("%d ", i); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 10 11
  • 22. 22 Bitfields in C Bit Field Declaration The declaration of a bit-field has the following form inside a structure − struct { type [member_name1] : width ; type [member_name2] : width ; }; Sr.No. Element & Description 1 type An integer type that determines how a bit-field's value is interpreted. The type may be int, signed int, or unsigned int. 2 member_name The name of the bit-field. 3 width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.
  • 23. 23 Bitfields Example #include <stdio.h> struct stat{ unsigned int width; unsigned int height; } status1; struct stats{ unsigned int width : 1; unsigned int height : 1; } status2; int main( ) { printf( "Memory size occupied by status1 : %dn", sizeof(status1)); printf( "Memory size occupied by status2 : %dn", sizeof(status2)); return 0; } Output: Memory size occupied by status1 : 4 Memory size occupied by status2 : 1