Write a C program to illustrate the usage of pointers with arrays.
int main() {
int data[100], n, i;
/* get the number of inputs from user */
printf("Enter the no of data:");
scanf("%d", &n);
/* get the input entries from user */
printf("Enter your inputs:\n");
for (i = 0; i < n; i++)
scanf("%d", &data[i]);
/* usage of pointers with arrays */
for (i = 0; i < n; i++) {
printf("Address and value of data[%d]:\n", i);
printf("Addr(ptr): 0x%x,Addr(arry): 0x%x, val: %d\n\n",
(int)(data + i), (int)&data[i], *(data + i));
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the no of data:5
Enter your inputs:
100
200
300
400
500
Address and value of data[0]:
Addr(ptr): 0xbf9c7ab8,Addr(arry): 0xbf9c7ab8, val: 100
Address and value of data[1]:
Addr(ptr): 0xbf9c7abc,Addr(arry): 0xbf9c7abc, val: 200
Address and value of data[2]:
Addr(ptr): 0xbf9c7ac0,Addr(arry): 0xbf9c7ac0, val: 300
Address and value of data[3]:
Addr(ptr): 0xbf9c7ac4,Addr(arry): 0xbf9c7ac4, val: 400
Address and value of data[4]:
Addr(ptr): 0xbf9c7ac8,Addr(arry): 0xbf9c7ac8, val: 500
Enter the no of data:5
Enter your inputs:
100
200
300
400
500
Address and value of data[0]:
Addr(ptr): 0xbf9c7ab8,Addr(arry): 0xbf9c7ab8, val: 100
Address and value of data[1]:
Addr(ptr): 0xbf9c7abc,Addr(arry): 0xbf9c7abc, val: 200
Address and value of data[2]:
Addr(ptr): 0xbf9c7ac0,Addr(arry): 0xbf9c7ac0, val: 300
Address and value of data[3]:
Addr(ptr): 0xbf9c7ac4,Addr(arry): 0xbf9c7ac4, val: 400
Address and value of data[4]:
Addr(ptr): 0xbf9c7ac8,Addr(arry): 0xbf9c7ac8, val: 500
No comments:
Post a Comment