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”

More Related Content

What's hot

Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
C string
C stringC string
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
Neel Mungra
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Prabhu Govind
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Fatima Kate Tanay
 
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
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Self employed
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 

What's hot (20)

Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
C string
C stringC string
C string
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
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...
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 

Similar to Pointers in C Language

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
 
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 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 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
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
thenmozhip8
 
U3.pptx
U3.pptxU3.pptx
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
Sreedhar Chowdam
 

Similar to Pointers in C Language (20)

C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Pointers
PointersPointers
Pointers
 
C programming
C programmingC programming
C programming
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 

Recently uploaded

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 

Recently uploaded (20)

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 

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