Credits

Sunday, March 1, 2009

More Pointer Sample

You can access the elements of array through pointers. Accessing element through pointers can give you a more flexible and powerful control on your program. However, improper use of pointers may bring unwanted results and may be difficult to trace.

Somehow, your use of pointers can make your program less in codes but more in application.



[+/-] show/hide




#include stdio.h /*enclose this in <> */
main
{
int num[]={2,3,4,5,6,7,8};
int *xnum,x;
xnum=num;
for (x=0;x<6;x++)
printf(" % d",num[x]); /* this displays 2 3 4 5 6 7 8 */

for (x=0;x<6;x++)
printf(" % d",*xnum++); /* this displays 2 3 4 5 6 7 8 */

for (x=5;x>0;x--)
printf(" % d",*(xnum+x)); /* this displays 8 7 6 5 4 3 2 */

for (x=5;x>0;x-=2)
printf(" % d",*(xnum+x)); /* this displays 8 5 3 */

getch();}

AddThis