-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArrayUsingPointers.c
58 lines (40 loc) · 1.28 KB
/
ArrayUsingPointers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
int main(){
int array[5]={23,32,12,34,4};
int *ptr ;
// ptr = &array[0];
ptr = array;
// for(int i=0 ; i<5 ; i++){
// printf("\nThe element no. %d is : %d",i+1,*ptr);
// ptr++;
// }
// OR
for(int i=0 ; i<5 ; i++){
printf("\nThe element no. %d is : %d",i+1,*(ptr+i));
}
int ar[3];
int *p;
// p = &ar[0];
p = ar;
for(int i=0;i<3;i++){
printf("\nenter the value of element %d : ",i+1);
scanf("%d",p);
p++;
}
// Make sure that pointer again points back to first array element
p = ar; // This is important
for(int i=0;i<3;i++){
// printf("\nThe value of element %d is : %d ",i+1,ar[i]);
// OR
// printf("\nThe value of element %d is : %d ",i+1,*(p+i));
// OR ==> pointer in array notation
// i[ptr] is equivalent to arr[i], i[arr] and ptr[i]
// I have used i[ptr] syntax for knowledge. You can also use ptr[i]
// printf("\nThe value of element %d is : %d ",i+1,i[p]);
// printf("\nThe value of element %d is : %d ",i+1,p[i]);
// OR
printf("\nThe value of element %d is : %d ",i+1,*p);
p++;
}
return 0;
}