Input/Output elements of a one-dimensional array
Method 1: array method -- access array elements in the form of a [I]
# Include
Int main ()
{
Int I, a [10], * ptr =;
For (I = 0; I <= 9; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <= 9; I ++)
Printf ("% 4d", a [I]);
Printf ("\ n ");
}
Method 2: pointer method-using the * (ptr + I) method for indirect access to array elements
# Include
Int main ()
{
Int I, a [10], * ptr =;
For (I = 0; I <= 9; I ++)
Scanf ("% d", ptr + I );
For (I = 0; I <= 9; I ++)
Printf ("% 4d", * (ptr + I ));
Printf ("\ n ");
}
Or
# Include
Int main ()
{
Int I, a [10], * ptr =;
For (I = 0; I <= 9; I ++)
Scanf ("% d", ptr ++ );
Ptr = a; // the pointer variable points to the first address of the array.
For (I = 0; I <= 9; I ++)
Printf ("% 4d", * ptr ++ );
Printf ("\ n ");
}
Method 3: Use the array name method to access array elements in the form of * (a + I)
# Include
Int main ()
{
Int I, a [10], * ptr =;
For (I = 0; I <= 9; I ++)
Scanf ("% d", a + I );
For (I = 0; I <= 9; I ++)
Printf ("% 4d", * (a + I ));
Printf ("\ n ");
}
Method 4: Use ptr [I] to access array elements by pointer subscript
# Include
Int main ()
{
Int I, a [10], * ptr =;
For (I = 0; I <= 9; I ++)
Scanf ("% d", & ptr [I]);
// Ptr = a; // the pointer variable points to the first address of the array.
For (I = 0; I <= 9; I ++)
Printf ("% 4d", ptr [I]);
Printf ("\ n ");
}