Professional Documents
Culture Documents
Programming II
Omega Semester
2019/2020 Session
Module 4
Arrays and Pointers
Module 4b
Pointers
4b.1 What is a pointer
• A pointer is a variable that stores the address of
another variable.
• The addresses are the location number always
contains whole number, therefore pointer contains
whole number.
• It is called pointer because it points to a particular
location in memory by storing the address of that
location.
• Pointers are used in C program to access the
memory and manipulate the address.
int *p;
OUTPUT
p= &var; Value of variable var is: 40
Value of variable var is: 40
Address of variable var is: 0x7fff5ed98c4c
printf("Value of variable var is: %d", var); Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
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;
}
1. #include<stdio.h>
2. int main(){
3. int num=70;
4. int *x;//pointer to int
5. x=&num ;//stores the address of num variable
6. printf("Address of x variable is %u \n",x);
7. x=x+1;
8. printf("After increment: Address of x variable is %u \n",x); // in our case, x will get
incremented by 4 bytes.
9. return 0;
10. }
OUTPUT
Address of x variable is 1239347716
After increment: Address of x variable is 1239347720
1. #include<stdio.h>
2. int main(){
3. int num=70;
4. int *x;//pointer to int
5. x=&num ;//stores the address of num variable
6. printf("Address of x variable is %u \n",x);
7. x=x-3; //subtracting 3 from pointer variable
8. printf(" subtracting 3: Address of x variable is %u \n",x);
9. return 0;
10. }
OUTPUT
Address of x variable is 3917007556
subtracting 3: Address of x variable is 3917007544
#include <stdio.h>
int main() OUTPUT
{ &x[0] = 6356724
int x[6]; &x[1] = 6356728
int i; &x[2] = 6356732
for(i = 0; i < 6; ++i)
{
&x[3] = 6356736
printf("&x[%d] = %u\n", i, &x[i]); &x[4] = 6356740
} &x[5] = 6356744
printf("Address of array x: %u", x); Address of array x: 6356724
return 0; Notice that, printing &x[0]
}
and x gave us the same
result.
1. int x[10] = {12, 20, 39, 43, 54, 60, 70, 79, 89, 96}, *y;
2. y = &x[0]; // y points to the beginning of the array
3. printf("%d\n", x[0]); // outputs 12
4. printf("%d\n", *y); // also outputs 12
5. printf("%d\n", *y+1); // outputs 13 (12 + 1)
6. printf("%d\n", (*y)+1); // also outputs 13
7. printf("%d\n", *(y+1)); // outputs x[1] or 20
8. y+=2; // y now points to x[2]
9. printf("%d\n", *y); // prints out 39
10. *y = 38; // changes x[2] to 38
11. printf("%d\n", *y-1); // prints out x[2] - 1 or 37
12. *y++; // sets y to point at the next array element
13. printf("%d\n", *y); // outputs x[3] (43)
14. (*y)++; // sets what y points to to be 1 greater
15. printf("%d\n", *y); // outputs the new value of x[3] (44)
5. int* ptr;
6. ptr = &x[3];
7. printf("*ptr = %d \n", *ptr);
8. printf("*(ptr+1) = %d \n", *(ptr+1));
9. printf("*(ptr-1) = %d \n", *(ptr-1));
10. printf("*(ptr-2) = %d \n", *(ptr-2));
11.
12. return 0;
13. }