You are on page 1of 17

Pointers

What is the need for pointers


• To allocate memory locations based on the
demand
• ie whenever memory location needed allocate
it dynamically during the execution
time(Running time)
• Likewise whenever a data is deleted a memory
location can be freed so that it could be
utilized for some other time
What is Pointer?
• A pointer is merely an address of where a datum or structure is
stored

Pointer Data Memory location

1000
2000 50
1000
A
• Address of A ?
1000
• Address of pointer ?
2000
How to declare a pointer?
use * preceding the variable name

int *x;
Datatype of a pointer
All pointers are typed based on the type of entity
that they point to
Pointer to integer data
int *x
Pointer to float type data
float *y
Pointer to Character type data
char *z
How to make a pointer to point?
int a; 1000 50 2000 1000
a
a=50; x

int *x;
How to make the pointer x to point the data a?
use & before the variable
x = &a ;

& means “ address of ”


“Address of”(&) Operator
• Used to display / retrieve the address of a
variable using ampersand sign.
• Eg:
• int num=10;
• printf("Address of var is: %p", &num);
• %p – Address in hexadecimal format
• printf("Address of var is: %u", &num);
• %u- Address in unsigned int format
“Value at Address”(*) Operator
• Int a = 10;
• Int *p;
• p = &a;
• *p would give us the value of the variable a.
• printf("%d", *p);
Example of Pointer demonstrating
the use of & and *
• #include <stdio.h>
• int main()
• {
• /* Pointer of integer type, this can hold the address of a integer type variable.
*/
• int *p;
• int var = 10;
• /* Assigning the address of variable var to the pointer * p. The p can hold the
address of var because var is an integer type variable. */
• p= &var;
• printf("Value of variable var is: %d", var);
• printf("\nValue of variable var is: %d", *p);
• printf("\nAddress of variable var is: %p", &var);
• printf("\nAddress of variable var is: %p", p);
• printf("\nAddress of pointer p is: %p", &p);
• return 0;
• }
output
• Value of variable var is: 10
• Value of variable var is: 10
• Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
Example
• int main()
• {
• int var =10;
• int *p;
• p= &var;
• printf ( "Address of var is: %u", &var);
• printf ( "\nAddress of var is: %u", p);
• printf ( "\nValue of var is: %d", var);
• printf ( "\nValue of var is: %d", *p);
• printf ( "\nValue of var is: %d", *( &var));
• /* Note I have used %p for p's value as it represents an address*/
• printf( "\nValue of pointer p is: %u", p);
• printf ( "\nAddress of pointer p is: %u", &p);
• return 0;
• }
Output
• Address of var is: 281583540
• Address of var is: 281583540
• Value of var is: 10
• Value of var is: 10
• Value of var is: 10
• Value of pointer p is: 281583540
• Address of pointer p is: 281583544
Call by Value

• Copy of actual arguments are passed to formal arguments of


called function

• Changes in called function do not affect actual parameter values

• Use when function does not need to modify actual arguments


• Avoids accidental changes
Call by Value
void main()
void swap(int a, int b)
{
{
int a=100, b=200;
int temp;
clrscr();
temp=a;
swap(a, b); // passing value to function
a=b;
printf("\nValue of a: %d",a);
b=temp;
printf("\nValue of b: %d",b);
getch(); }
}

Output:

Value of a:100

Value of b:200
Call by Reference

• Passes original argument (address of the

arguments - Pass address).


• Changes in called function will affect

original
• Only used with trusted functions
Call by reference
void main()
{
int a=100, b=200;
void swap(int *a, int *b)
{
swap(&a, &b); // passing address / reference
int temp;
to function
temp=*a;
printf("\nValue of a: %d",a);
*a=*b;
printf("\nValue of b: %d",b);
*b=temp;
}
}
Output:

Value of a:200

Value of b:100
Call by value Call by reference
Calling function sends copies to data Calling function sends address of data

Formal parameters are ordinary Formal parameters are pointer


variables variables

Any changes in formal parameters does Any changes in formal parameters also
not affect the actual parameters affects the actual parameters

You might also like