Pointers Store Address of Variables or A Memory Location
Pointers Store Address of Variables or A Memory Location
Pointers Store Address of Variables or A Memory Location
or a memory location.
Pointer Notation
int i = 3 ;
*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
Let j = &i
Let j = &i
Value of *j is 3
POINTER DECLARATION
• 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 :
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;
Output :
2. Call by Reference :
Sending the addresses of the arguments
Output :
Example
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