Displaying arrays' elements and their respective indices in various format
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows 2003 Server Standard Edition
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info:
To do: Printing arrays' elements and their respective indices in various formats using pointer notation
To show: How to use array's subscript and pointer notations in C programming
// Using subscript and pointer notations with arrays
#include <stdio.h>
void main(void)
{
int i, offset, b[] = {10, 20, 30, 40};
// set bPtr to point to array b
int *bPtr = b;
printf("So many notations?????....\n");
// ....separating code in multiple lines
printf("Array b printed with: \n"
"Array subscript notation\n");
for(i=0; i<=3; i++)
printf("b[%d] = %d\n", i, b[i]);
printf("\nPointer/offset notation where \n"
"the pointer is the array name\n");
for(offset = 0; offset <=3; offset++)
printf("*(b + %d) = %d\n", offset, *(b + offset));
printf("\nPointer subscript notation\n");
for(i=0; i<=3; i++)
printf("bPtr[%d] = %d\n",i,bPtr[i]);
printf("\nPointer/offset notation\n");
for(offset = 0; offset <=3; offset++)
printf("*(bptr + %d) = %d\n", offset, *(bPtr + offset));
}
Output example:
So many notations?????....
Array b printed with:
Array subscript notation
b[0] = 10
b[1] = 20
b[2] = 30
b[3] = 40
Pointer/offset notation where
the pointer is the array name
*(b + 0) = 10
*(b + 1) = 20
*(b + 2) = 30
*(b + 3) = 40
Pointer subscript notation
bPtr[0] = 10
bPtr[1] = 20
bPtr[2] = 30
bPtr[3] = 40
Pointer/offset notation
*(bptr + 0) = 10
*(bptr + 1) = 20
*(bptr + 2) = 30
*(bptr + 3) = 40
Press any key to continue . . .