Intro To Pointer
Intro To Pointer
variable.
◦ The only difference from other
variables is that it stores the
memory address other variables.
◦ This variable may be of type
int, char, array, structure, function
or any other.
Normal variables
◦ Contain a specific value (direct reference)
Pointer variables
◦ Contain memory addresses as their values
int y = 5;
int *yPtr;
yPtr = &y; // yptr is the address of y
// yPtr “points to” y
a=5;
* ptr;
ptr=&a;
1. Name of variable : a
4. Name of variable: ptr
2. Value of variable: 5
5. Value of variable: 1025
3. Address: 1025 (assume)
6. Address: 5000 (assume)
Call by reference with pointer arguments
◦ Pass address of argument using & operator
◦ Arrays are not passed with & because the array
name is already a pointer
* operator
◦ Used as nickname for variable inside of function
Output: 1004
Output: 1024
#include<stdio.h> Difference= 2
int main() #include<stdio.h>
{ int main()
int *p=(int *)1000; {
int *temp; float *p=(float *)1000;
temp=p; float *q=(float *)2000;
p=p+2; printf("Difference= %d",q-p);
printf("%d %d\n",temp,p); return 0;
printf("difference= %d",p- }
temp); Output: Difference= 250
return 0;
}
Output: 1000 1008
Arithmetic operations can be performed on
pointers
◦ Increment/decrement pointer (++ or --)
◦ Add an integer to a pointer
( + or += , - or -=)
◦ Pointers may be subtracted from each other
◦ Operations meaningless unless performed on an
array
operation Description