Pointers Store Address of Variables or A Memory Location

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Pointers store address of variables

or a memory location.
Pointer Notation

• Consider the declaration,

int i = 3 ;

This declaration tells the C compiler to:


(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
We may represent i’s location in memory by the following
memory map.
*ptr

*ptr
directs you towards the value
in address stored in ptr.
directs you towards the value
*ptr in address stored in ptr.
directs you towards the value
*ptr in address stored in ptr.

Let j = &i

Then what is the value in j ?


directs you towards the value
*ptr in address stored in ptr.

Let j = &i

j holds the value 65524 ( address of i)

What is the value of *j ?


directs you towards the value
*ptr in address stored in ptr.

Let j = &i

j holds the value 65524 ( address of i)

Value of *j is 3
POINTER DECLARATION

data- type *pointer_variable;


where pointer_variable is the name of the pointer variable, and data-type refers to the data type of the
pointer’s object. Remember that an asterisk must precede pointer_variable.
Example:
int *ptr;
float u, v, *pv;
The first example has a pointer variable by name ptr which can point to any integer variable.
The second example has two floating point variables u and v and also a floating point pointer variable pv
which can point to any floating point variable.
i.e, the type of a pointer depends on the type of the variable it points to.

Every pointer points to some data type.


POINTER INITIALISATION
• Within a variable declaration, a pointer variable can be initialized by assigning it
the address of another variable.
float u, v; / * floating-point variable declarations */
float *pv; /* pointer variable declaration */
.....
pv = &v; / * assign v ' s address to pv */
& and *

There are two unary operations to consider.

• The * operator: If ptr is a pointer variable, then ∗ptr gives you the
content of the location pointed to by ptr.
• The & operator: If v is a variable, then &v is the address of the
variable.
We can print this address number through the following
program:
Let , j = &i ;
• 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.
• The following memory map would illustrate the contents of i and j.

Value of i

Address of i
Here is a program that demonstrates the relationships we have
been discussing.
Output :
Example

#include <stdio.h>
main( )
{ Let u’s address is 18d57f1c
int u = 3; and
v’s address is 18d57f18
int v;
int *pu; /* pointer to an integer */
int *pv; /* pointer to an integer */
pu = &u ; / * assign address of u to pu */
v = *pu; / * assign value of u to v */
pv = &v; /* assign address of v to pv */
printf("\nu=%d &u=%x pu=%x *pu=%d" , u, &u, pu, *pu);
printf("\n\nv=%d &v=%x pv=%x *pv=%d", v, &v, pv, *pv);
}
Output :

• u=3 &u=18d57f1c pu=18d57f1c *pu=3


• v=3 &v=18d57f18 pv=18d57f18 *pv=3
What is the output of this program?

Let
Address of i is 65524
Address of j is 65522
Address of k = 65520
Output :
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
Pointer Expressions
If p1 and p2 are two pointers, the following statements are valid:
sum = *p1 + *p2;
prod = *p1 * *p2;
prod = (*p1) * (*p2);
*p1 = *p1 + 2;
x = *p1 / *p2 + 5;
What are not allowed?
Add two pointers.
p1 = p1 + p2;
Multiply / divide a pointer in an expression.
p1 = p2 / 5;
p1 = p1 – p2 * 10;
CONT>>>>>
If p1 is an integer pointer, then
p1++;
will increment the value of p1 by 4,Where 4 is the size of an integer variable.
#include <stdio.h>
int main()
{
int var []={1, 2, 3, 4};

int *ptr;
ptr = &var;

printf("value of *ptr after ptr=&var is %d\n" , *ptr);


*ptr++;
printf("value of *ptr now after *ptr++ is %d \n", *ptr);
*ptr++;
printf("value of *ptr now after *ptr++ is %d \n", *ptr);
*ptr++;
printf("value of *ptr now after *ptr++ is %d \n", *ptr);
*ptr++;
printf("value of *ptr now after *ptr++ is %d \n", *ptr);
return 0;
}
Functions :
• 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
1. Call by value :
Sending the values of the arguments
• In this 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.
Function using call by value method:
Function using call by value method:

Output :
2. Call by Reference :
Sending the addresses of the arguments

• Pointers are often passed to a function as arguments.


• When an argument is passed by reference,
i.e., when a pointer is passed to a function,
the address of a data item is passed to the function.
• 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.
• Any change that is made to the data item (i.e., to the contents of the address) will
be recognized in both the function and the calling routine.
DECLARATION
• Function prototype
void funct2( int *pu, int *pv);
• The function declaration could also have been written without any variable names,
as
void funct2( int *, int *);
Function using call by reference method:
Function using call by reference method:

Output :
Example

#include <stdio. h> void functl( int u, int v)


void functl( int u, int v); / * function prototype */ {
void funct2( int *pu, int *pv); / * function prototype with u = 0;
pointers */ v = 0;
main ( ) printf( " \ n Withinfunctl : u=%d v=%d", u, v);
{ return;
int u= 1; }
int v = 3; void funct2( int *pu, int *pv)
printf( " \ n Before calling functl : u=%d v=%d", u, v); {
functl (u, v); *pu = 0;
printf( "\ n After calling functl : u=%d v=%d", u, v); *pv = 0;
printf(“\n\nBefore calling funct2: u=%d v=%d" u, v) ; printf( " \ n Within funct2: *pu=%d *pv=%d" , *pu, *pv) ;
funct2(&u, &v); //passing pointers return;
printf( " \ n After calling funct2: u=%d v=%d", u, v); }
}
Output

Before calling functl : u=l v=3


Within functl : u=0 v=0
After calling functl : u=l v=3
Before calling funct2: u=l v=3
Within funct2: *pu=0 *pv=0
After calling funct2: u=0 v=0
A function using both the methods
A function using both the methods

Output :
Example
/* count the number of vowels, consonants, digits , whitespace characters,and
'"other" characters in a line of text */

#include <stdio.h>
#include <ctype.h>

/* function prototype */
void scan-line(char line[ ] , int *pv, int *pc, int *pd, int *pw, int *PO);

main( )
{
char line[80]; /* line of text */
int vowels = 0; /* vowel counter */
int consonants = 0; /* consonant counter */
int digits = 0; / * digit counter */
int whitespc = 0; / * whitespace counter */
int other = 0; /* remaining character counter */
printf("Enter a line of text below: \n”);
scanf ( “%[^\n]”, line ) ;
scan-line(line, &vowels, &consonants, &digits, &whitespc, &other);
printf("\nNo. of vowels: %d”, vowels);
printf(“\nNo. of consonants: %d", consonants);
%[^\n] will read
printf( "\nNo. of digits : %d", digits) ; all characters until
printf("\nNo. of whitespace characters: %d", whitespc); you press \n or
enter key.
printf(“\nNo. of other characters: %d", other);
}
void scan-line(char line[ ] , int *pv, int *pc, int *pd, int *pw, int *PO)
/* analyze the characters in a line of text */
{
char c; /* uppercase character */
int count = 0; /* character counter */
while ( ( c = toupper(line[count])) ! = ‘\ 0’ ) {
if(c == ' A ‘|| c == ' E ' || c == ' I ' ||c == ‘O’|| c == 'U')
++ *pv; /* vowel */
else if (c >= ' A ' && c <= ' Z ' )
++ *pc; /* consonant */
else if (c >= '0'&& c <= '9')
++ *pd; /* d i g i t * /
else if (c == ‘ ‘ || c == ' \ t ' )
++ *pw; /* whitespace */
else
++ *po; /* other */
++count;
}
return;}
Returning a pointer

• A function can also return a pointer to the calling portion of the program.
• To do so, the function definition and any corresponding function declarations
must indicate that the function will return a pointer.
• This is accomplished by preceding the function name by an asterisk. The asterisk
must appear in both the function definition and the function declarations.
Example

#include <stdio. h> double *scan(double f [ ] )


double *scan(double z [ ] ) ; {
main( ) double *pf; /* pointer declaration */
{ /* process elements of f */
double z[100]; /* array declaration */ pf=.....;
double *pz; / * pointer declaration */ return(pf) ;
/* enter values for elements of z */ }
pz = scan(z);
…………………
}

You might also like