Input/Output elements of a one-dimensional array

Source: Internet
Author: User

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 ");
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.