SlideShare a Scribd company logo
Achyut Devkota
Kathford International College
Pointer and DMA
5/21/2015 3:52 PM1 Prepared by Achyut Dev
Introduction
5/21/2015 3:52 PMPrepared by Achyut Dev2
 Within the computer’s memory, every stored data item
occupies one or more contiguous memory cells.
 Suppose v is a variable that represents some
particular data item.
 The compiler will automatically assign memory cells
for this data item.
 The data item can then be accessed if we know the
location (i.e., the address) of the first memory cell.
 let another variable ptrv
ptrv=&v
where ptrv is a pointer variable which point the memory
address of variable v.
Introduction
5/21/2015 3:52 PMPrepared by Achyut Dev3
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
Addres
s
DataVariable Name
int v=10;
int *ptrv
ptr=&v
v
ptrv
10
FFF3
 data item represented by v
(i.e., the data item stored in
v’s memory cells) can be
accessed by the expression
*ptrv.
* is a unary operator, called the
indirection operator
Example
5/21/2015 3:52 PMPrepared by Achyut Dev4
Outpu
t
Example
5/21/2015 3:52 PMPrepared by Achyut Dev5
Output
5/21/2015 3:52 PMPrepared by Achyut Dev6
 Pointer variables can point to numeric or
character variables, arrays, functions or other
pointer variables.
ptrv=&v
 Also, a pointer variable can be assigned the value
of another pointer variable
ptra=ptrv
Pointer Declaration
5/21/2015 3:52 PMPrepared by Achyut Dev7
Syntax
data_type *ptvar;
Example
float u,v;
float *pv;
 The first line declares u and v to be floating-point
variables.
 The second line declares pv to be a pointer
variable whose object is a floating-point quantity.Note that pv represents an address, not a floating-point
quantity.
Passing pointer to a function
5/21/2015 3:52 PMPrepared by Achyut Dev8
 When an argument is passed by value, the data item
is copied to the function. Thus, any alteration made to
the data item within the function is not carried over
into the calling routine.
 When an argument is passed by reference, however
(i.e., when a pointer is passed to a function), the
address of a data item is passed to the function.
 The contents of that address can be accessed freely,
either within the function or within the calling routine.
 An array name is actually a pointer to the array,
Therefore, an array name is treated as a pointer when
it is passed to a function. No need of & sign in an
arguments.
See tutorial Sheet 04 for more
examples
Passing Array to a function
5/21/2015 3:52 PMPrepared by Achyut Dev9
 It is possible to pass a portion of an array, rather
than an entire array, to a function.
 The address of the first array element to be
passed must be specified as an argument.
 The remainder of the array, starting with the
specified array element, will then be passed to
the function.
5/21/2015 3:52 PMPrepared by Achyut Dev10
•The address of z[50] (i.e.,
&z[50]) is passed to the
function process.
• Hence, the last 50
elements of z (i.e., the
elements z [50] through z
[99]) will be available to
process.
Example
5/21/2015 3:52 PMPrepared by Achyut Dev11
Write a function that count vowels, consonants, digit,
whitespace and other characters in a line of text
which is entered through the keyboard. Display
results of counts in main() function.
Calling function:
scanline(line, &vowels, &consonants, &digits, &whitespc,
&other);
Function Header
void scanline(char line[] , int *pv, int *pc,int *pd,int *pw,int *po)
OR
void scanline(char *line, int *pv, int *pc,in t*pd,int *pw,int
*po)
declare line as a pointer rather than an array
Example
5/21/2015 3:52 PMPrepared by Achyut Dev12
Output
Example
5/21/2015 3:52 PMPrepared by Achyut Dev13
Output
Ans:
unary operators are evaluated
from right to left. Thus address
is increase itself.
Pointer –scanf()
5/21/2015 3:52 PMPrepared by Achyut Dev14
 The scanf function requires that the addresses
of the data items being entered into the
computer's memory be specified
 The ampersands provide a means for accessing
the addresses of ordinary single-valued variables.
scanf("%d %f %s",&number,&value,line);
 Ampersands are not required with array names,
since array names themselves represent
addresses.
 If the scanf function is used to enter a single
array element rather than an entire array, the
name of the array element must be preceded by
an ampersand
scanf("%d“,&array[10]);
Pointer and 1-D array
5/21/2015 3:52 PMPrepared by Achyut Dev15
 Array name is a pointer to the first element in the
array.
 if x is a one dimensional array, then the address
of the first array element can be expressed as
either &x[0]or simply as x.
 the address of the second array element can be
written as either &x[1] or as (x+1), and so on.
 In general, the address of array element (i+1) can be
expressed as either &x[i]or as (x+i).
 Array has int, float, double .. data type.Different Data type occupies different memory size
Is there
any
proble
m ?
No problem-C compiler adjusts for this
automatically
Pointer and 1-D array
5/21/2015 3:52 PMPrepared by Achyut Dev16
 &x [i] and (x + i)both represent the address of the
ith element of x.
 it would seem reasonable that x[i]and *(x+i) both
represent the contents of that address, i.e., the
value of the ith element of x.
Example
5/21/2015 3:52 PMPrepared by Achyut Dev17
Output:
Pointer and 1-D array
5/21/2015 3:52 PMPrepared by Achyut Dev18
What is the difference between x[i] and*(x+i)?
value of an
array element
value of the
memory area
whose
address is
that of the
array element
 It is not possible to assign
an arbitrary address to an
array name or to an array
element.
 expressions such as x, (x+i)
and &x [i] cannot appear on
the left side of an
assignment statement.
Can we write
++x ?
Example
5/21/2015 3:52 PMPrepared by Achyut Dev19
Output:
Pointer and 1-D array
5/21/2015 3:52 PMPrepared by Achyut Dev20
 Note : that the address of one array element
cannot be assigned to some other array element.
&line[2] = &line[l]; Error
ptrl=&line[l];
line[2]=*ptrl;
OR
ptrl=line+1;
*(line+2)=*ptrl;
What about these ?
We can do

Problem 1
5/21/2015 3:52 PMPrepared by Achyut Dev21
 Write a program that takes a one-dimensional
array of ‘n’ elements. Pass it to a function that
sorts them in ascending order.
Problem 2
5/21/2015 3:52 PMPrepared by Achyut Dev22
 Write a function that takes an array of size n and
finds the largest adjacent sum and display it in
main function. For example, if given array is {1.2.-
3.-1.6}, then called function must return 5.
Memory allocation
5/21/2015 3:52 PMPrepared by Achyut Dev23
 Two types:
1. Compile time memory allocation or design time
money allocation or static memory allocation.
1. No of memory space is preserved.
2. Disadvantage
1. If the user’s data is less than the reserved bytes, there is
wastage of memory bytes.
2. If the user’s data exceeds the reserved bytes, then
memory overflow occurs.
eg. int array[20]; char line[100];
2. Run time memory allocation or Dynamic
memory allocation.
Dynamic Memory allocation
5/21/2015 3:52 PMPrepared by Achyut Dev24
 An array name is actually a pointer to the first element
within the array
 it should be possible to define the array as a pointer
variable rather than as a conventional array
 A conventional array definition results in a fixed block
of memory being reserved at the beginning of
program execution, whereas this does not occur if the
array is represented in terms of a pointer variable.
 Therefore, the use of a pointer variable to represent
an array requires some type of initial memory
assignment before the array elements are processed.
This is known as dynamic memory allocation
Dynamic Memory allocation
5/21/2015 3:52 PMPrepared by Achyut Dev25
 Suppose x is one dimensional array
we can write
int *x;
rather than
int x[10];
To assign sufficient memory
for x, we can make use of
the library function malloc,
as follows.
x =(int*)malloc(l0*sizeof(int));
more…
x =(int*)calloc(l0,sizeof(int));
x =(int*)realloc(x,20*sizeof(int));
x is not automatically assigned a
memory block when it is defined
as a pointer variable
a block of memory large enough to
store 10 integer quantities will be
reserved in advance when x is
defined as an array
Dynamic Memory allocation
5/21/2015 3:52 PMPrepared by Achyut Dev26
 If the declaration is to include the assignment
of initial values, however, then x must be
defined as an array rather than a pointer
variable.
For example,
int x[10]=(1,2,3,4,5,6,7,8,9,10);
or
int x[]=(1,2,3,4,5,6,7,8,9,10);
Example
5/21/2015 3:52 PMPrepared by Achyut Dev27
void sort_ascending(int*,int*);
void main()
{
int i,e,*a;
printf("Enter no of elements of
matrix:");
scanf("%d",&e);
a=(int*)malloc(e*sizeof(int));
printf("Enter Elements of Matrix ");
for (i=0;i<e; i++)
{ printf("A[%d] : ",i);
scanf("%d",&a[i]);
}
sort_ascending(a,&e);
printf("nnAscending order :n");
for (i=0;i<e;i++)
printf("%d ",a[i]);
getch();
}
void sort_ascending(int *a, int*x)
{
int i,j,temp;
for(i=0;i<*x-1;i++)
{
for(j=i+1; j<*x; j++)
{
if(*(a+i)>*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
}
Why DML ?
5/21/2015 3:52 PMPrepared by Achyut Dev28
 An important advantage of dynamic memory
allocation is the ability to reserve as much
memory as may be required during program
execution, and then release this memory when it
is no longer needed.
 Moreover, this process may be repeated many
times during execution of a program.
Pointer operation
5/21/2015 3:52 PMPrepared by Achyut Dev29
 an integer value can be added to or subtracted
from a pointer variable
 for example, that px is a pointer variable that
represents the address of some variable x. We
can write expressions such as ++px, --px, (px+ 3
),(px+i),and (px-i),where i is an integer variable.
Pointer and Multi-dimentional
array
5/21/2015 3:52 PMPrepared by Achyut Dev30
 A two-dimensional array is actually a collection of
one-dimensional arrays.
 Therefore, we can define a two-dimensional array
as a pointer to a group of contiguous one-
dimensional arrays.
Array declaration
data-type(*ptvar)[expression2];
for higher D-array
data-type(*ptvar)[expression2][expression3]
. . . [expression n];
These parentheses must be
present.
Without parentheses
we would be defining
an array of pointers
rather than a pointer to
a group of arrays
Example
5/21/2015 3:52 PMPrepared by Achyut Dev31
int (*x)[20]; x is defined to be a pointer to a group
of contiguous, one-dimensional, 20-
element integer arrays.
Thus, x points to the first 20-element array, which is
actually the first row (i.e., row 0) of the original two-
dimensional array. Similarly, ( x + 1) points to the second
20-element array, which is the second row (row 1) of the
original two dimensional array, and so on.
For 3-d array
float (*t)[20][30]
Similar for higher d-
array
…
2-D pointer
5/21/2015 3:52 PMPrepared by Achyut Dev32
 we can write
x[2][5]
as
(*(x+2)+5)
 (x + 2) is a pointer to row 2
 ( x + 2) is actually a pointer
to the first element in row 2
 (* (x + 2) + 5) is a pointer
to element 5 in row 2
Problem
5/21/2015 3:52 PMPrepared by Achyut Dev33
 Write three different function a) readmatrix()-
to read elements of array b) processmatrix() -
to perform certain calculation and c)
showmatrix () - to display result and find
i) Sum of two matrix
ii) Multiplication
Problem
5/21/2015 3:52 PMPrepared by Achyut Dev34
Function Declaration
Function definition
Calling Function
Array of Pointer
5/21/2015 3:52 PMPrepared by Achyut Dev35
 A multidimensional array can be expressed in
terms of an array of pointers rather than a pointer
to a group of contiguous arrays.
For two dimensional array
data-type*array[expression1];
rather then
data-type array[expression1][expression2];
for higher d-array
data-type*array[expression1][expression2)...
[expression n-1];
array name and its
preceding asterisk
are not enclosed
in parentheses
Array of Pointer
5/21/2015 3:52 PMPrepared by Achyut Dev36
data-type*array[expression1];
 a right-to-left rule first associates the pairs of
square brackets with array, defining the named
object as an array.
 The preceding asterisk then establishes that the
array will contain pointers.
 Note that the last (i.e., the rightmost) expression
is omitted when defining an array of pointers,
 whereas the first (i.e., the leftmost) expression is
omitted when defining a pointer to a group of
arrays.
Array of Pointer
5/21/2015 3:52 PMPrepared by Achyut Dev37
Fig show the array of
pointer : int*x[l0];
x[2][5], can be accessed
by writing : *(x[2]+5)
x[2] is a
pointer to the
first element
in row
( x[2J+5) points to
element 5 (actually, the
sixth element) within row
2.
5/21/2015 3:52 PMPrepared by Achyut Dev38
Calculate the sum of the elements in two tables of
integersFunction Declaration
Function definition
Calling Function
Dynamic memory allocation
5/21/2015 3:52 PMPrepared by Achyut Dev39
More…
int*p(int a); : indicates a function that accepts an
integer argument, and returns a pointer to
an integer.
int(*p)(int a);: indicates a pointer to a function
that accepts an integer argument and returns an
integer.
int*(*p)(int(*a)[]);
(*p)(...)indicates a pointer to a function.
int*(*p)(...)indicates a pointer to a function
that returns a pointer to an integer.
int(*a)[]represents a pointer to an array of
integers.
represents a pointer to a function that accepts a pointer to
an array of integers as an argument, and returns a pointer to
an integer.
Class work
5/21/2015 3:52 PMPrepared by Achyut Dev40
 WAP to multiply two matrix using following
concept for passing array to a function that
multiply two matrix.
i) Array of a pointer
ii) Pointer of an array (use DMA to allocate
appropriate memory)
5/21/2015 3:52 PMPrepared by Achyut Dev41
Ad

More Related Content

What's hot (20)

Pointer in c
Pointer in cPointer in c
Pointer in c
Imamul Kadir
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentation
SnehalataAgasti
 
Header files in c
Header files in cHeader files in c
Header files in c
HoneyChintal
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
Meghaj Mallick
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
Mahalakshmiv11
 
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
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
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
 
Fixed point and floating-point numbers
Fixed point and  floating-point numbersFixed point and  floating-point numbers
Fixed point and floating-point numbers
MOHAN MOHAN
 
Operand and Opcode | Computer Science
Operand and Opcode | Computer ScienceOperand and Opcode | Computer Science
Operand and Opcode | Computer Science
Transweb Global Inc
 
Program control
Program controlProgram control
Program control
Rahul Narang
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentation
SnehalataAgasti
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
Meghaj Mallick
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
Mahalakshmiv11
 
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
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
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
 
Fixed point and floating-point numbers
Fixed point and  floating-point numbersFixed point and  floating-point numbers
Fixed point and floating-point numbers
MOHAN MOHAN
 
Operand and Opcode | Computer Science
Operand and Opcode | Computer ScienceOperand and Opcode | Computer Science
Operand and Opcode | Computer Science
Transweb Global Inc
 

Viewers also liked (15)

Pointer in C
Pointer in CPointer in C
Pointer in C
Sonya Akter Rupa
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Prabhu Govind
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Ponters
PontersPonters
Ponters
Mukund Trivedi
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Mangalayatan university
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
Tech_MX
 
String in c
String in cString in c
String in c
Suneel Dogra
 
C pointer
C pointerC pointer
C pointer
University of Potsdam
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
Uri Dekel
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Pointers in C
Pointers in CPointers in C
Pointers in C
guestdc3f16
 
Ad

Similar to C programming - Pointer and DMA (20)

Memory Management for C and C++ _ language
Memory Management for C and C++ _ languageMemory Management for C and C++ _ language
Memory Management for C and C++ _ language
23ecuos117
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Lecture_01.2.pptx
Lecture_01.2.pptxLecture_01.2.pptx
Lecture_01.2.pptx
RockyIslam5
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
Prof Ansari
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptxPASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
Arjunkrish9
 
See through C
See through CSee through C
See through C
Tushar B Kute
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
ssuser688516
 
Pointer
PointerPointer
Pointer
manish840
 
Linked list
Linked listLinked list
Linked list
somuinfo123
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
NewsMogul
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
Srikanth
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
Srikanth
 
Memory Management for C and C++ _ language
Memory Management for C and C++ _ languageMemory Management for C and C++ _ language
Memory Management for C and C++ _ language
23ecuos117
 
Lecture_01.2.pptx
Lecture_01.2.pptxLecture_01.2.pptx
Lecture_01.2.pptx
RockyIslam5
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
Prof Ansari
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptxPASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx
Arjunkrish9
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
ssuser688516
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
Srikanth
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
Srikanth
 
Ad

Recently uploaded (20)

A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-1 notes [BCG402-CG&V].pdf
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-1 notes [BCG402-CG&V].pdfCOMPUTER GRAPHICS AND VISUALIZATION :MODULE-1 notes [BCG402-CG&V].pdf
COMPUTER GRAPHICS AND VISUALIZATION :MODULE-1 notes [BCG402-CG&V].pdf
Alvas Institute of Engineering and technology, Moodabidri
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
ZJIT: Building a Next Generation Ruby JIT
ZJIT: Building a Next Generation Ruby JITZJIT: Building a Next Generation Ruby JIT
ZJIT: Building a Next Generation Ruby JIT
maximechevalierboisv1
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
ZJIT: Building a Next Generation Ruby JIT
ZJIT: Building a Next Generation Ruby JITZJIT: Building a Next Generation Ruby JIT
ZJIT: Building a Next Generation Ruby JIT
maximechevalierboisv1
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 

C programming - Pointer and DMA

  • 1. Achyut Devkota Kathford International College Pointer and DMA 5/21/2015 3:52 PM1 Prepared by Achyut Dev
  • 2. Introduction 5/21/2015 3:52 PMPrepared by Achyut Dev2  Within the computer’s memory, every stored data item occupies one or more contiguous memory cells.  Suppose v is a variable that represents some particular data item.  The compiler will automatically assign memory cells for this data item.  The data item can then be accessed if we know the location (i.e., the address) of the first memory cell.  let another variable ptrv ptrv=&v where ptrv is a pointer variable which point the memory address of variable v.
  • 3. Introduction 5/21/2015 3:52 PMPrepared by Achyut Dev3 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 Addres s DataVariable Name int v=10; int *ptrv ptr=&v v ptrv 10 FFF3  data item represented by v (i.e., the data item stored in v’s memory cells) can be accessed by the expression *ptrv. * is a unary operator, called the indirection operator
  • 4. Example 5/21/2015 3:52 PMPrepared by Achyut Dev4 Outpu t
  • 5. Example 5/21/2015 3:52 PMPrepared by Achyut Dev5 Output
  • 6. 5/21/2015 3:52 PMPrepared by Achyut Dev6  Pointer variables can point to numeric or character variables, arrays, functions or other pointer variables. ptrv=&v  Also, a pointer variable can be assigned the value of another pointer variable ptra=ptrv
  • 7. Pointer Declaration 5/21/2015 3:52 PMPrepared by Achyut Dev7 Syntax data_type *ptvar; Example float u,v; float *pv;  The first line declares u and v to be floating-point variables.  The second line declares pv to be a pointer variable whose object is a floating-point quantity.Note that pv represents an address, not a floating-point quantity.
  • 8. Passing pointer to a function 5/21/2015 3:52 PMPrepared by Achyut Dev8  When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine.  When an argument is passed by reference, however (i.e., when a pointer is passed to a function), the address of a data item is passed to the function.  The contents of that address can be accessed freely, either within the function or within the calling routine.  An array name is actually a pointer to the array, Therefore, an array name is treated as a pointer when it is passed to a function. No need of & sign in an arguments. See tutorial Sheet 04 for more examples
  • 9. Passing Array to a function 5/21/2015 3:52 PMPrepared by Achyut Dev9  It is possible to pass a portion of an array, rather than an entire array, to a function.  The address of the first array element to be passed must be specified as an argument.  The remainder of the array, starting with the specified array element, will then be passed to the function.
  • 10. 5/21/2015 3:52 PMPrepared by Achyut Dev10 •The address of z[50] (i.e., &z[50]) is passed to the function process. • Hence, the last 50 elements of z (i.e., the elements z [50] through z [99]) will be available to process.
  • 11. Example 5/21/2015 3:52 PMPrepared by Achyut Dev11 Write a function that count vowels, consonants, digit, whitespace and other characters in a line of text which is entered through the keyboard. Display results of counts in main() function. Calling function: scanline(line, &vowels, &consonants, &digits, &whitespc, &other); Function Header void scanline(char line[] , int *pv, int *pc,int *pd,int *pw,int *po) OR void scanline(char *line, int *pv, int *pc,in t*pd,int *pw,int *po) declare line as a pointer rather than an array
  • 12. Example 5/21/2015 3:52 PMPrepared by Achyut Dev12 Output
  • 13. Example 5/21/2015 3:52 PMPrepared by Achyut Dev13 Output Ans: unary operators are evaluated from right to left. Thus address is increase itself.
  • 14. Pointer –scanf() 5/21/2015 3:52 PMPrepared by Achyut Dev14  The scanf function requires that the addresses of the data items being entered into the computer's memory be specified  The ampersands provide a means for accessing the addresses of ordinary single-valued variables. scanf("%d %f %s",&number,&value,line);  Ampersands are not required with array names, since array names themselves represent addresses.  If the scanf function is used to enter a single array element rather than an entire array, the name of the array element must be preceded by an ampersand scanf("%d“,&array[10]);
  • 15. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev15  Array name is a pointer to the first element in the array.  if x is a one dimensional array, then the address of the first array element can be expressed as either &x[0]or simply as x.  the address of the second array element can be written as either &x[1] or as (x+1), and so on.  In general, the address of array element (i+1) can be expressed as either &x[i]or as (x+i).  Array has int, float, double .. data type.Different Data type occupies different memory size Is there any proble m ? No problem-C compiler adjusts for this automatically
  • 16. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev16  &x [i] and (x + i)both represent the address of the ith element of x.  it would seem reasonable that x[i]and *(x+i) both represent the contents of that address, i.e., the value of the ith element of x.
  • 17. Example 5/21/2015 3:52 PMPrepared by Achyut Dev17 Output:
  • 18. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev18 What is the difference between x[i] and*(x+i)? value of an array element value of the memory area whose address is that of the array element  It is not possible to assign an arbitrary address to an array name or to an array element.  expressions such as x, (x+i) and &x [i] cannot appear on the left side of an assignment statement. Can we write ++x ?
  • 19. Example 5/21/2015 3:52 PMPrepared by Achyut Dev19 Output:
  • 20. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev20  Note : that the address of one array element cannot be assigned to some other array element. &line[2] = &line[l]; Error ptrl=&line[l]; line[2]=*ptrl; OR ptrl=line+1; *(line+2)=*ptrl; What about these ? We can do 
  • 21. Problem 1 5/21/2015 3:52 PMPrepared by Achyut Dev21  Write a program that takes a one-dimensional array of ‘n’ elements. Pass it to a function that sorts them in ascending order.
  • 22. Problem 2 5/21/2015 3:52 PMPrepared by Achyut Dev22  Write a function that takes an array of size n and finds the largest adjacent sum and display it in main function. For example, if given array is {1.2.- 3.-1.6}, then called function must return 5.
  • 23. Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev23  Two types: 1. Compile time memory allocation or design time money allocation or static memory allocation. 1. No of memory space is preserved. 2. Disadvantage 1. If the user’s data is less than the reserved bytes, there is wastage of memory bytes. 2. If the user’s data exceeds the reserved bytes, then memory overflow occurs. eg. int array[20]; char line[100]; 2. Run time memory allocation or Dynamic memory allocation.
  • 24. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev24  An array name is actually a pointer to the first element within the array  it should be possible to define the array as a pointer variable rather than as a conventional array  A conventional array definition results in a fixed block of memory being reserved at the beginning of program execution, whereas this does not occur if the array is represented in terms of a pointer variable.  Therefore, the use of a pointer variable to represent an array requires some type of initial memory assignment before the array elements are processed. This is known as dynamic memory allocation
  • 25. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev25  Suppose x is one dimensional array we can write int *x; rather than int x[10]; To assign sufficient memory for x, we can make use of the library function malloc, as follows. x =(int*)malloc(l0*sizeof(int)); more… x =(int*)calloc(l0,sizeof(int)); x =(int*)realloc(x,20*sizeof(int)); x is not automatically assigned a memory block when it is defined as a pointer variable a block of memory large enough to store 10 integer quantities will be reserved in advance when x is defined as an array
  • 26. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev26  If the declaration is to include the assignment of initial values, however, then x must be defined as an array rather than a pointer variable. For example, int x[10]=(1,2,3,4,5,6,7,8,9,10); or int x[]=(1,2,3,4,5,6,7,8,9,10);
  • 27. Example 5/21/2015 3:52 PMPrepared by Achyut Dev27 void sort_ascending(int*,int*); void main() { int i,e,*a; printf("Enter no of elements of matrix:"); scanf("%d",&e); a=(int*)malloc(e*sizeof(int)); printf("Enter Elements of Matrix "); for (i=0;i<e; i++) { printf("A[%d] : ",i); scanf("%d",&a[i]); } sort_ascending(a,&e); printf("nnAscending order :n"); for (i=0;i<e;i++) printf("%d ",a[i]); getch(); } void sort_ascending(int *a, int*x) { int i,j,temp; for(i=0;i<*x-1;i++) { for(j=i+1; j<*x; j++) { if(*(a+i)>*(a+j)) { temp=*(a+i); *(a+i)=*(a+j); *(a+j)=temp; } } } }
  • 28. Why DML ? 5/21/2015 3:52 PMPrepared by Achyut Dev28  An important advantage of dynamic memory allocation is the ability to reserve as much memory as may be required during program execution, and then release this memory when it is no longer needed.  Moreover, this process may be repeated many times during execution of a program.
  • 29. Pointer operation 5/21/2015 3:52 PMPrepared by Achyut Dev29  an integer value can be added to or subtracted from a pointer variable  for example, that px is a pointer variable that represents the address of some variable x. We can write expressions such as ++px, --px, (px+ 3 ),(px+i),and (px-i),where i is an integer variable.
  • 30. Pointer and Multi-dimentional array 5/21/2015 3:52 PMPrepared by Achyut Dev30  A two-dimensional array is actually a collection of one-dimensional arrays.  Therefore, we can define a two-dimensional array as a pointer to a group of contiguous one- dimensional arrays. Array declaration data-type(*ptvar)[expression2]; for higher D-array data-type(*ptvar)[expression2][expression3] . . . [expression n]; These parentheses must be present. Without parentheses we would be defining an array of pointers rather than a pointer to a group of arrays
  • 31. Example 5/21/2015 3:52 PMPrepared by Achyut Dev31 int (*x)[20]; x is defined to be a pointer to a group of contiguous, one-dimensional, 20- element integer arrays. Thus, x points to the first 20-element array, which is actually the first row (i.e., row 0) of the original two- dimensional array. Similarly, ( x + 1) points to the second 20-element array, which is the second row (row 1) of the original two dimensional array, and so on. For 3-d array float (*t)[20][30] Similar for higher d- array …
  • 32. 2-D pointer 5/21/2015 3:52 PMPrepared by Achyut Dev32  we can write x[2][5] as (*(x+2)+5)  (x + 2) is a pointer to row 2  ( x + 2) is actually a pointer to the first element in row 2  (* (x + 2) + 5) is a pointer to element 5 in row 2
  • 33. Problem 5/21/2015 3:52 PMPrepared by Achyut Dev33  Write three different function a) readmatrix()- to read elements of array b) processmatrix() - to perform certain calculation and c) showmatrix () - to display result and find i) Sum of two matrix ii) Multiplication
  • 34. Problem 5/21/2015 3:52 PMPrepared by Achyut Dev34 Function Declaration Function definition Calling Function
  • 35. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev35  A multidimensional array can be expressed in terms of an array of pointers rather than a pointer to a group of contiguous arrays. For two dimensional array data-type*array[expression1]; rather then data-type array[expression1][expression2]; for higher d-array data-type*array[expression1][expression2)... [expression n-1]; array name and its preceding asterisk are not enclosed in parentheses
  • 36. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev36 data-type*array[expression1];  a right-to-left rule first associates the pairs of square brackets with array, defining the named object as an array.  The preceding asterisk then establishes that the array will contain pointers.  Note that the last (i.e., the rightmost) expression is omitted when defining an array of pointers,  whereas the first (i.e., the leftmost) expression is omitted when defining a pointer to a group of arrays.
  • 37. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev37 Fig show the array of pointer : int*x[l0]; x[2][5], can be accessed by writing : *(x[2]+5) x[2] is a pointer to the first element in row ( x[2J+5) points to element 5 (actually, the sixth element) within row 2.
  • 38. 5/21/2015 3:52 PMPrepared by Achyut Dev38 Calculate the sum of the elements in two tables of integersFunction Declaration Function definition Calling Function Dynamic memory allocation
  • 39. 5/21/2015 3:52 PMPrepared by Achyut Dev39 More… int*p(int a); : indicates a function that accepts an integer argument, and returns a pointer to an integer. int(*p)(int a);: indicates a pointer to a function that accepts an integer argument and returns an integer. int*(*p)(int(*a)[]); (*p)(...)indicates a pointer to a function. int*(*p)(...)indicates a pointer to a function that returns a pointer to an integer. int(*a)[]represents a pointer to an array of integers. represents a pointer to a function that accepts a pointer to an array of integers as an argument, and returns a pointer to an integer.
  • 40. Class work 5/21/2015 3:52 PMPrepared by Achyut Dev40  WAP to multiply two matrix using following concept for passing array to a function that multiply two matrix. i) Array of a pointer ii) Pointer of an array (use DMA to allocate appropriate memory)
  • 41. 5/21/2015 3:52 PMPrepared by Achyut Dev41