Professional Documents
Culture Documents
Topics:-
• Basic of pointers
• Pointer to pointer
• Pointer and Array
• Pointer to Array
• Array of Pointers
• Functions returning a pointer
What is Pointer ?
• Every data item in the computer is stored in the memory in one or more
adjacent locations depending upon it’s type.
• Computers memory is sequential collection of storage cells.
Address 0 1 2 65535
Memory Organization
Continue..
Representation of x=5 5
Addr of X
In memory
P X
Pointer is a variable that
Link between variables x and p
contain the address which
is a location of another
variable in the memory.
Advantages of Pointers :
• Pointers are more efficient in handling arrays and data tables.
• Pointers can pass arrays and string from one function to another.
• Pointers can create complex data structures, such as linked lists, stacks, queues.
• Pointers increase the execution speed and thus reduce the program execution time.
Address(&) and Dereferencing(*) operators
• To access the address variable x, the operator & is used.
• ‘&’ is called ‘address of operator’ and it is unary operator.
• We can assign the address of x to another variable p as:
• p=&x; //Assigns 1002(address of x) to p .thus p is a pointer variable.
• Accessing the value of variable through pointer(using *
operator)
• The value represented by x, can be accessed by the expression *P,
where * is called ‘value at the address’ operator and it is a unary
operator.
• *p and x represent same value
• x and *p represent 5.
• * is called ‘dereferencing or indirection operator’.
Pointer declaration
• Syntax:
data_type *ptvar;
• Examples:
int *p;
float *abc;
char * status;
Pointer Initialization
• Syntax:
datatype *ptr_var=&addr_var;
• Example:
int a=5,x,*aptr;
aptr=&a; //assign addr of a to pointer aptr
x=*aptr; //asssign value at address pointed by ‘aptr’ to x i.e x=5
*aptr=0; // assign value 0 to a
Example
Example
Example
Pointer Arithmetic
Operation Expression Meaning
Increment Ptr++; Increment ptr to point to next location of
same type. If ptr is a pointer to integer(int
occupie 2 byte), if ptr=2003 then ptr++ gives
ptr=2005.
If ptr is a pointer to character(char occupies 1
byte), if ptr=2003 then ptr++ gives ptr=2004.
• The base address is the location of the first element (index 0) of the array.
• The compiler defines the array name as a constant pointer which contains the
address of the first element of the array.
• For Example :
int x[5] = {1,2,3,4,5};
• Suppose, the base address of x is 1000, then the five elements will be
stored as follows :
Elements ----->
Value -----> 1 2 3 4 5
Address ----->
• Here the array name x is a constant pointer that contains the address of
first element, x[0].
• Therefore x = &x[0] = 1000;
• If we declare p as an integer pointer, then we can write p = x;
therefore p = &x[0];
• Now we can access every value of array x using p++ as follows :
p = &x[0] ( = 1000)
p+1 = &x[1] ( = 1002)
p+2 = &x[2] ( = 1004)
p+3 = &x[3] ( = 1006)
p+4 = &x[4] ( = 1008)
• Now we can access array elements using pointer instead of array.
Pointer as function arguments
• When the function change( ) is called, the address of variable x is passed
into the function change( ).
• Inside the change( ), the variable p is declared as pointer therefore p is
the address of variable x.
• The statement *p = *p + 10;
• add 10 to the value stored at the address p, so the value of x is changed
from 20 to 30. Therefore the output of the program will be 30.
Example :
main( )
{
int x;
x = 20;
change(&x);
printf(“%d\n”,x);
}
change(int *p)
{
*p = *p + 10;
}
(IMP)Swapping two number using pointer
#include<stdio.h>
void swap(int *num1, int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
} Enter the first number : 12
Enter the Second number : 22
int main() First number : 22
{ Second number : 12
int num1, num2;
printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
swap(&num1, &num2);
printf("\nFirst number : %d", num1);
printf("\nSecond number : %d", num2);
return (0);
}
C Program to Read integers into an array and Reversing them
using Pointers
ptr = &arr[size - 1];