Professional Documents
Culture Documents
5. Pointers
5. Pointers
5. Pointers
number Variable
178 value
4523 Address
178
How to access value of Variable
For Example:
n 4522 int n;
m int m;
4526
178
n 4522 int n;
6
m int m;
4526
num = &n sets the pointer num to the address of the variable
n.
Concept of Pointers
n 4522 int n;
6
m 6 int m;
4526
Here, the asterisk (*) tells us that the variable is a pointer variable.
ptr is the name given to the pointer variable .
ptr points to the memory location of a variable of type int .
4550 address
Valid declarations:
int* p;
int *p; // most popular way of declaration
int * p;
Initializing Pointers
Ex 1:
int n;
int *num;
num= &n;
Ex 2:
float f; // wrong
int *p
p= &f;
Example
main()
{
int number, Y;
int *ptr;
value of number is 5
number = 5;
ptr= &number; value of ptr is the address of marks
(=4523)
Y = *ptr
} value of Y is 5
int *j;
int i=10; j=&i;
Variable name
i
j
10 Value in Variable
6734
6734 Address of variable
3256
main()
{ j is a pointer variable that contains address of i.
int i=10;
int *j;
j=&i; address of i=6734
printf(“%d”, &i); address of j=3256
printf(“%d”, &j); value of j=6734
printf(“%d”, j); value of i=10
printf(“%d”, i); value of i=10
printf(“%d”, *j); value of i=10
printf(“%d”, *(&i));
}