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

Pointers

  • 1.
  • 2.
    Pointer Declaration Consider thedeclarclaation, 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 atthe 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 rememberthat 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 isa 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 atthe 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 ( "nAddressof 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 thatwhen 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 followingprogram 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 thesecond 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
  • 11.
  • 12.
    Storage class • Everyvariable 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 ofStorage Class Reserved words 1 Automatic auto 2 Register register 3 Static static 4 External extern 12/20/2020 14
  • 15.
    Automatic variables • Bydefault 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() { floatf1 = 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 variablesare 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); voidmain() { 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 isauto • 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 variablesare 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 • Itrefers 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 intx=5; int y = 10,z; z = y/x; printf(“x = %d, y = %d, z = %d”, x, y, z); } 12/20/2020 22
  • 23.
  • 24.
    C Standard Library Everyimplementation 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 LibraryFunction 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 CStandard 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