SlideShare une entreprise Scribd logo
1  sur  26
POINTER CONCEPT
12/20/2020 1
Pointer Declaration
Consider the declarclaation,
int i=3;
This declaration tells the C Compiler to,
(a) Reserve space in memory to hold the integer value.
(b) Associate the names i with this memory location.
(c) Store the value3 at this location.
i
65524
We can see that the computer has selected memory location 65524 as the place to store the value 3. It is not fixed because
the computer can change the location later. We can print this address number through the following program:
main( )
{
int i=3;
printf(“n Address of i= %u”, &i);
printf(“nValue of i= %d:, i);
}
Output: Address of i= 65524
Value of i= 3.
12/20/2020 2
3
Location Name
Value at Location
Location Number
• Look at the first printf( ) statement carefully. ‘&’ used in this statement is C’s ‘address of’ operator.
• The expression &i returns the address of the variable i, which in this case happens to be 65524.
• Hence it is printed out using %u, which is a format specifier for printing an unsigned integer.
• We have been using the ‘&’ operator all the time in the scanf( ) statement.
• The other pointer operator available in C is ‘*’, called ‘value at address’ operator.
• It gives the value stored at a particular address.The ‘value at address’ operator is also called ‘indirection’ operator.
• Observe carefully the output of the following program:
main( )
{
int i = 3 ;
printf ( "nAddress of i = %u", &i ) ;
printf ( "nValue of i = %d", i ) ;
printf ( "nValue of i = %d", *( &i ) ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3
• Note that printing the value of *( &i ) is same as printing the value of i.
• The expression &i gives the address of the variable i.This address can be collected in a variable, by saying,
j = &i ;
12/20/2020 3
• But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the
address of other variable (i in this case).
• Since j is a variable the compiler must provide it space in the memory. Once again, the following memory
map would illustrate the contents of i and j.
i j
65524 65522
• As you can see, i’s value is 3 and j’s value is i’s address.
• But wait, we can’t use j in a program without declaring it. And since j is a variable that contains the address of
i, it is declared as,
int *j ;
• This declaration tells the compiler that j will be used to store the address of an integer value.
• In other words j points to an integer. How do we justify the usage of * in the declaration,
int *j ;
• Let us go by the meaning of *. It stands for ‘value at address’.Thus, int *j would mean, the value at the
address contained in j is an int.
12/20/2020 4
3 65524
• Here is a program that demonstrates the relationships we have been discussing.
main( )
{
int i = 3 ;
int *j ;
j = &i ;
printf ( "nAddress of i = %u", &i ) ;
printf ( "nAddress of i = %u", j ) ;
printf ( "nAddress of j = %u", &j ) ;
printf ( "nValue of j = %u", j ) ;
printf ( "nValue of i = %d", i ) ;
printf ( "nValue of i = %d", *( &i ) ) ;
printf ( "nValue of i = %d", *j ) ;
}
The output of the above program would be:
Address of i = 65524
Address of i = 65524
Address of j = 65522
Value of j = 65524
Value of i = 3
Value of i = 3
Value of i = 3
12/20/2020 5
• Look at the following declarations,
int *alpha ;
char *ch ;
float *s ;
• The declaration float *s does not mean that s is going to contain a floating-point value. What it means is, s is going to
contain the address of a floating-point value.
• Similarly, char *ch means that ch is going to contain the address of a char value. Or in other words, the value at address
stored in ch is going to be a char.
Double Pointer:
The concept of pointers can be further extended. Pointer, we know is a variable that contains address of another
variable. Now this variable itself might be another pointer.Thus, we now have a pointer that contains another pointer’s
address.
The following example should make this point clear.
main( )
{
int i = 3, *j, **k ;
j = &i ;
k = &j ;
printf ( "nAddress of i = %u", &i ) ;
printf ( "nAddress of i = %u", j ) ;
printf ( "nAddress of i = %u", *k ) ;
printf ( "nAddress of j = %u", &j ) ;
printf ( "nAddress of j = %u", k ) ;
12/20/2020 6
printf ( "nAddress of k = %u", &k ) ;
printf ( "nValue of j = %u", j ) ;
printf ( "nValue of k = %u", k ) ;
printf ( "nValue of i = %d", i ) ;
printf ( "nValue of i = %d", * ( &i ) ) ;
printf ( "nValue of i = %d", *j ) ;
printf ( "nValue of i = %d", **k ) ;
}
The output of the above program would be:
Address of i = 65524
Address of i = 65524
Address of i = 65524
Address of j = 65522
Address of j = 65522
Address of k = 65520
Value of j = 65524
Value of k = 65522
Value of i = 3
Value of i = 3
Value of i = 3
Value of i = 3
12/20/2020 7
• Remember that when you run this program the addresses that get printed might turn out to be something different,
However, with these addresses too the relationship between i, j and k can be easily established.
i j k
65524 65522 65520
• Observe how the variables j and k have been declared,
int i, *j, **k ;
• Here, i is an ordinary int, j is a pointer to an int (often called an integer pointer), whereas k is a pointer to an integer
pointer.
• We can extend the above program still further by creating a pointer to a pointer to an integer pointer.
Back to Function Calls
The two types of function calls—call by value and call by reference. Arguments can generally be passed to functions in
one of the two ways:
(a) sending the values of the arguments
(b) sending the addresses of the arguments
• In the first method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal
arguments of the called function.
• With this method the changes made to the formal arguments in the called function have no effect on the values of actual
arguments in the calling function.
12/20/2020 8
3 65524 65522
• The following program illustrates the ‘Call byValue’.
main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t = x ;
x = y ;
y = t ;
printf ( "nx = %d y = %d", x, y ) ;
}
The output of the above program would be:
x = 20 y = 10
a = 10 b = 20
• Note that values of a and b remain unchanged even after exchanging the values of x and y.
12/20/2020 9
• In the second method (call by reference) the addresses of actual arguments in the calling function are copied into formal
arguments of the called function.
• This means that using these addresses we would have an access to the actual arguments and hence we would be able to
manipulate them.
• The following program illustrates this fact.
main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "na = %d b = %d", a, b ) ;
}
swapr( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}
• The output of the above program would be:
a = 20 b = 10
• Note that this program manages to exchange the values of a and b using their addresses stored in x and y.
12/20/2020 10
Storage class
Storage class
• Every variable or constant possesses a data type
• In addition to the data type, a variable possesses an attribute called storage class.
where v1,v2 are the variables
data_type is the valid data type in c language
storage_class gives information regarding lifetime and scope.
12/20/2020 12
Storage_class data_type v1,v2,v3,………………..vn.
Storage class cont’• Lifetime
Lifetime or Longetivity of a variable refers to the duration for which the variable
retains a given value during the execution of a program.
• Scope
It is the portion of the program in which the variable may be visible or available .It
can classified into two types
Type 1:
Local variable (or) private variable (or) internal variable.
Type 2:
Global variable (or) public variable (or) external variable.
12/20/2020 13
Types
Sl.No Types of Storage Class Reserved words
1 Automatic auto
2 Register register
3 Static static
4 External extern
12/20/2020 14
Automatic variables
• By default all variables are automatic storage class.
• Their memory space is automatically allocated as the variable is declared.
• These variables are given only temporary memory space and after the execution all
the automatic variables will get disposed.
• It can not be accessed directly by other functions.
12/20/2020 15
Sample Program
#include<stdio.h>
Void main()
{
float f1 = 10.2;
{
float f1 = 40.00;
{
float f1 = 33.9;
printf(“the value of f1 in block 3 is %f”,f1);
}
printf(“the value of f1 in block 2 is %f”,f1);
}
printf(“the value of f1 in block 1 is %f”,f1);
}
12/20/2020 16
StaticVariables
• The variables are declared with static keyword.
• It can be internal static (or) external static based on the place of its
declaration.
• Both are declared using the keyword static.
• The storage used by an static variable in a block is not lost after the
completion of the execution.
• Even after the termination of the block, the value of the static variable
is available.
• If the value is changed during the execution the recent value is
retained.
• When the execution enters the next time the same block, the
initialiser is neglected and the recently stored value is retained.
12/20/2020 17
Sample Program
#include<stdio.h>
int fun(int);
void main()
{
int i,x;
for(i=1;i<=10;i++)
{
x = fun(i);
printf("The IValue is:”);
}
}
12/20/2020 18
int fun(int a)
{
auto int sum = 100;
sum = sum + a;
return(sum);
}
Output
When sum is auto
• The IValue is:101
• The IValue is:102
• The IValue is:103
• The IValue is:104
• The IValue is:105
• The IValue is:106
• The IValue is:107
• The IValue is:108
• The IValue is:109
• The IValue is:110
12/20/2020 19
When sum is static
• The I Value is:101
• The I Value is:103
• The I Value is:106
• The I Value is:110
• The I Value is:115
• The I Value is:121
• The I Value is:128
• The I Value is:136
• The I Value is:145
• The I Value is:155
RegisterVariables
• The variables are declared using register keyword.
• The scope and lifetime is same as that of automatic variables.
• For any given operation the data available in the memory should be transferred to the CPU
registers.
• So if the data are stored in register itself then the time for data transfer from the memory to the
register is saved.
• Only a few values can be placed at a time and hence it is advisable to use limited register
variables.
12/20/2020 20
main()
{
register int i;
for(i=0;i<10;i++)
printf(“%d”,i);
}
External variables
• It refers to the location outside a block i.e., prior to the main() or beyond the closing
braces ( } ) of the outermost block in a program .
• The value is available throughout the program because of its global scope.
• Whenever an external variable is modified in a block, the effect is propagated to all
places where ever it is used.
12/20/2020 21
Sample Program
main()
{
extern int x=5;
int y = 10,z;
z = y/x;
printf(“x = %d, y = %d, z = %d”, x, y, z);
}
12/20/2020 22
C Standard Library
C Standard Library
Every implementation of C comes with a standard library of
predefined functions.
Note that, in programming, a library is a collection of functions.
The functions that are common to all versions of C are known as the
C Standard Library.
12/20/2020 24
C Standard Library Function Examples
12/20/2020 25
Function
Name
Math
Name Value Example
abs(x) absolute
value
|x| abs(-1) returns 1
sqrt(x) square root x0.5 sqrt(2.0) returns 1.414
…
exp(x) exponential ex exp(1.0) returns 2.718
…
log(x) natural
logarithm
ln x log(2.718…) returns 1.0
sin(x) sine sin x sin(3.14…) returns 0.0
cos(x) cosine cos x cos(3.14…) returns -1.0
tan(x) tangent tan x tan(3.14…) returns 0.0
ceil(x) ceiling ┌ x ┐ ceil(2.5) returns 3.0
floor(x) floor └ x ┘
floor(2.5) returns 2.0
Using the C Standard Math Library
If you’re going to use functions like cos that are from the part of
the C standard library that has to do with math, then you
need to do two things:
1. In your source code, immediately below the
#include <stdio.h>
you must also put
#include <math.h>
2. When you compile, you must append -lm to the end of
your compile command:
gcc -o funcargs funcargs.c –lm
(Note that this is hyphen ell em, NOT hyphen one em.)
12/20/2020 26

Contenu connexe

Tendances

Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++Pranav Ghildiyal
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
Pointers in Programming
Pointers in ProgrammingPointers in Programming
Pointers in ProgrammingHamadZia1
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manualAnil Bishnoi
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02Pooja Gupta
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]ecko_disasterz
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]Abhishek Sinha
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Himanshu Kaushik
 

Tendances (20)

Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
C Programming
C ProgrammingC Programming
C Programming
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Pointers in Programming
Pointers in ProgrammingPointers in Programming
Pointers in Programming
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Ansi c
Ansi cAnsi c
Ansi c
 
Exam for c
Exam for cExam for c
Exam for c
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
C Programming
C ProgrammingC Programming
C Programming
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 

Similaire à Pointers

[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to PointersMuhammad Hammad Waseem
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...RSathyaPriyaCSEKIOT
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptxramanathan2006
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxKrishanPalSingh39
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variableimtiazalijoono
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...kushwahashivam413
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 

Similaire à Pointers (20)

[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
Pointers.pptx
Pointers.pptxPointers.pptx
Pointers.pptx
 
C important questions
C important questionsC important questions
C important questions
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
C code examples
C code examplesC code examples
C code examples
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 

Plus de Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
More About Strings
More About StringsMore About Strings
More About Strings
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Lists and Tuples
Lists and TuplesLists and Tuples
Lists and Tuples
 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 

Dernier

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Dernier (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

Pointers

  • 2. Pointer Declaration Consider the declarclaation, int i=3; This declaration tells the C Compiler to, (a) Reserve space in memory to hold the integer value. (b) Associate the names i with this memory location. (c) Store the value3 at this location. i 65524 We can see that the computer has selected memory location 65524 as the place to store the value 3. It is not fixed because the computer can change the location later. We can print this address number through the following program: main( ) { int i=3; printf(“n Address of i= %u”, &i); printf(“nValue of i= %d:, i); } Output: Address of i= 65524 Value of i= 3. 12/20/2020 2 3 Location Name Value at Location Location Number
  • 3. • Look at the first printf( ) statement carefully. ‘&’ used in this statement is C’s ‘address of’ operator. • The expression &i returns the address of the variable i, which in this case happens to be 65524. • Hence it is printed out using %u, which is a format specifier for printing an unsigned integer. • We have been using the ‘&’ operator all the time in the scanf( ) statement. • The other pointer operator available in C is ‘*’, called ‘value at address’ operator. • It gives the value stored at a particular address.The ‘value at address’ operator is also called ‘indirection’ operator. • Observe carefully the output of the following program: main( ) { int i = 3 ; printf ( "nAddress of i = %u", &i ) ; printf ( "nValue of i = %d", i ) ; printf ( "nValue of i = %d", *( &i ) ) ; } The output of the above program would be: Address of i = 65524 Value of i = 3 Value of i = 3 • Note that printing the value of *( &i ) is same as printing the value of i. • The expression &i gives the address of the variable i.This address can be collected in a variable, by saying, j = &i ; 12/20/2020 3
  • 4. • But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable (i in this case). • Since j is a variable the compiler must provide it space in the memory. Once again, the following memory map would illustrate the contents of i and j. i j 65524 65522 • As you can see, i’s value is 3 and j’s value is i’s address. • But wait, we can’t use j in a program without declaring it. And since j is a variable that contains the address of i, it is declared as, int *j ; • This declaration tells the compiler that j will be used to store the address of an integer value. • In other words j points to an integer. How do we justify the usage of * in the declaration, int *j ; • Let us go by the meaning of *. It stands for ‘value at address’.Thus, int *j would mean, the value at the address contained in j is an int. 12/20/2020 4 3 65524
  • 5. • Here is a program that demonstrates the relationships we have been discussing. main( ) { int i = 3 ; int *j ; j = &i ; printf ( "nAddress of i = %u", &i ) ; printf ( "nAddress of i = %u", j ) ; printf ( "nAddress of j = %u", &j ) ; printf ( "nValue of j = %u", j ) ; printf ( "nValue of i = %d", i ) ; printf ( "nValue of i = %d", *( &i ) ) ; printf ( "nValue of i = %d", *j ) ; } The output of the above program would be: Address of i = 65524 Address of i = 65524 Address of j = 65522 Value of j = 65524 Value of i = 3 Value of i = 3 Value of i = 3 12/20/2020 5
  • 6. • Look at the following declarations, int *alpha ; char *ch ; float *s ; • The declaration float *s does not mean that s is going to contain a floating-point value. What it means is, s is going to contain the address of a floating-point value. • Similarly, char *ch means that ch is going to contain the address of a char value. Or in other words, the value at address stored in ch is going to be a char. Double Pointer: The concept of pointers can be further extended. Pointer, we know is a variable that contains address of another variable. Now this variable itself might be another pointer.Thus, we now have a pointer that contains another pointer’s address. The following example should make this point clear. main( ) { int i = 3, *j, **k ; j = &i ; k = &j ; printf ( "nAddress of i = %u", &i ) ; printf ( "nAddress of i = %u", j ) ; printf ( "nAddress of i = %u", *k ) ; printf ( "nAddress of j = %u", &j ) ; printf ( "nAddress of j = %u", k ) ; 12/20/2020 6
  • 7. printf ( "nAddress of k = %u", &k ) ; printf ( "nValue of j = %u", j ) ; printf ( "nValue of k = %u", k ) ; printf ( "nValue of i = %d", i ) ; printf ( "nValue of i = %d", * ( &i ) ) ; printf ( "nValue of i = %d", *j ) ; printf ( "nValue of i = %d", **k ) ; } The output of the above program would be: Address of i = 65524 Address of i = 65524 Address of i = 65524 Address of j = 65522 Address of j = 65522 Address of k = 65520 Value of j = 65524 Value of k = 65522 Value of i = 3 Value of i = 3 Value of i = 3 Value of i = 3 12/20/2020 7
  • 8. • Remember that when you run this program the addresses that get printed might turn out to be something different, However, with these addresses too the relationship between i, j and k can be easily established. i j k 65524 65522 65520 • Observe how the variables j and k have been declared, int i, *j, **k ; • Here, i is an ordinary int, j is a pointer to an int (often called an integer pointer), whereas k is a pointer to an integer pointer. • We can extend the above program still further by creating a pointer to a pointer to an integer pointer. Back to Function Calls The two types of function calls—call by value and call by reference. Arguments can generally be passed to functions in one of the two ways: (a) sending the values of the arguments (b) sending the addresses of the arguments • In the first method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. • With this method the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. 12/20/2020 8 3 65524 65522
  • 9. • The following program illustrates the ‘Call byValue’. main( ) { int a = 10, b = 20 ; swapv ( a, b ) ; printf ( "na = %d b = %d", a, b ) ; } swapv ( int x, int y ) { int t ; t = x ; x = y ; y = t ; printf ( "nx = %d y = %d", x, y ) ; } The output of the above program would be: x = 20 y = 10 a = 10 b = 20 • Note that values of a and b remain unchanged even after exchanging the values of x and y. 12/20/2020 9
  • 10. • In the second method (call by reference) the addresses of actual arguments in the calling function are copied into formal arguments of the called function. • This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them. • The following program illustrates this fact. main( ) { int a = 10, b = 20 ; swapr ( &a, &b ) ; printf ( "na = %d b = %d", a, b ) ; } swapr( int *x, int *y ) { int t ; t = *x ; *x = *y ; *y = t ; } • The output of the above program would be: a = 20 b = 10 • Note that this program manages to exchange the values of a and b using their addresses stored in x and y. 12/20/2020 10
  • 12. Storage class • Every variable or constant possesses a data type • In addition to the data type, a variable possesses an attribute called storage class. where v1,v2 are the variables data_type is the valid data type in c language storage_class gives information regarding lifetime and scope. 12/20/2020 12 Storage_class data_type v1,v2,v3,………………..vn.
  • 13. Storage class cont’• Lifetime Lifetime or Longetivity of a variable refers to the duration for which the variable retains a given value during the execution of a program. • Scope It is the portion of the program in which the variable may be visible or available .It can classified into two types Type 1: Local variable (or) private variable (or) internal variable. Type 2: Global variable (or) public variable (or) external variable. 12/20/2020 13
  • 14. Types Sl.No Types of Storage Class Reserved words 1 Automatic auto 2 Register register 3 Static static 4 External extern 12/20/2020 14
  • 15. Automatic variables • By default all variables are automatic storage class. • Their memory space is automatically allocated as the variable is declared. • These variables are given only temporary memory space and after the execution all the automatic variables will get disposed. • It can not be accessed directly by other functions. 12/20/2020 15
  • 16. Sample Program #include<stdio.h> Void main() { float f1 = 10.2; { float f1 = 40.00; { float f1 = 33.9; printf(“the value of f1 in block 3 is %f”,f1); } printf(“the value of f1 in block 2 is %f”,f1); } printf(“the value of f1 in block 1 is %f”,f1); } 12/20/2020 16
  • 17. StaticVariables • The variables are declared with static keyword. • It can be internal static (or) external static based on the place of its declaration. • Both are declared using the keyword static. • The storage used by an static variable in a block is not lost after the completion of the execution. • Even after the termination of the block, the value of the static variable is available. • If the value is changed during the execution the recent value is retained. • When the execution enters the next time the same block, the initialiser is neglected and the recently stored value is retained. 12/20/2020 17
  • 18. Sample Program #include<stdio.h> int fun(int); void main() { int i,x; for(i=1;i<=10;i++) { x = fun(i); printf("The IValue is:”); } } 12/20/2020 18 int fun(int a) { auto int sum = 100; sum = sum + a; return(sum); }
  • 19. Output When sum is auto • The IValue is:101 • The IValue is:102 • The IValue is:103 • The IValue is:104 • The IValue is:105 • The IValue is:106 • The IValue is:107 • The IValue is:108 • The IValue is:109 • The IValue is:110 12/20/2020 19 When sum is static • The I Value is:101 • The I Value is:103 • The I Value is:106 • The I Value is:110 • The I Value is:115 • The I Value is:121 • The I Value is:128 • The I Value is:136 • The I Value is:145 • The I Value is:155
  • 20. RegisterVariables • The variables are declared using register keyword. • The scope and lifetime is same as that of automatic variables. • For any given operation the data available in the memory should be transferred to the CPU registers. • So if the data are stored in register itself then the time for data transfer from the memory to the register is saved. • Only a few values can be placed at a time and hence it is advisable to use limited register variables. 12/20/2020 20 main() { register int i; for(i=0;i<10;i++) printf(“%d”,i); }
  • 21. External variables • It refers to the location outside a block i.e., prior to the main() or beyond the closing braces ( } ) of the outermost block in a program . • The value is available throughout the program because of its global scope. • Whenever an external variable is modified in a block, the effect is propagated to all places where ever it is used. 12/20/2020 21
  • 22. Sample Program main() { extern int x=5; int y = 10,z; z = y/x; printf(“x = %d, y = %d, z = %d”, x, y, z); } 12/20/2020 22
  • 24. C Standard Library Every implementation of C comes with a standard library of predefined functions. Note that, in programming, a library is a collection of functions. The functions that are common to all versions of C are known as the C Standard Library. 12/20/2020 24
  • 25. C Standard Library Function Examples 12/20/2020 25 Function Name Math Name Value Example abs(x) absolute value |x| abs(-1) returns 1 sqrt(x) square root x0.5 sqrt(2.0) returns 1.414 … exp(x) exponential ex exp(1.0) returns 2.718 … log(x) natural logarithm ln x log(2.718…) returns 1.0 sin(x) sine sin x sin(3.14…) returns 0.0 cos(x) cosine cos x cos(3.14…) returns -1.0 tan(x) tangent tan x tan(3.14…) returns 0.0 ceil(x) ceiling ┌ x ┐ ceil(2.5) returns 3.0 floor(x) floor └ x ┘ floor(2.5) returns 2.0
  • 26. Using the C Standard Math Library If you’re going to use functions like cos that are from the part of the C standard library that has to do with math, then you need to do two things: 1. In your source code, immediately below the #include <stdio.h> you must also put #include <math.h> 2. When you compile, you must append -lm to the end of your compile command: gcc -o funcargs funcargs.c –lm (Note that this is hyphen ell em, NOT hyphen one em.) 12/20/2020 26