C Language Basics (10)-Arrays

Source: Internet
Author: User

I. Definition of an array

An array is a contiguous variable space of the same type in memory.

Two. How the array is stored in memory

All members of the same array are of the same data type, and all members in-memory addresses are contiguous, and the array name is a constant of an address, representing the address of the first element in the array.

Three. Initialization of arrays

3.1 One-dimensional array initialization

inta[Ten] = {1,2,3 };inta[Ten] = {0 };inta[Ten] = {1,2,3,4,5,6,7,8,9,Ten};intA[] = {1,2,3,4,5,6,7,8,9,Ten};//This is equivalent to the above notation.

Attention:

In the C language, it is extremely dangerous to use an array uninitialized, and the system assigns a random value to each element in the array, so it is best to initialize it before using it.

Exercise 1

Define the following array

int a[10] = {0,1,2,3,4,5,6,7,8,9};

Reverses the elements in the array (the maximum value is before, and the minimum value is behind).

#include <stdio.h>voidMain () {inta[Ten] = {0,1,2,3,4,5,6,7,8,9}; //int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};     for(inti =0; I <Ten; i++) {printf ("a[%d]=%d\n", I,a[i]); } printf ("------------------invert the array------------------\ n"); intMin =0;//record the minimum subscript for the current array    intMax =9;//record the maximum subscript for the current array     while(Min <max) {        intTMP = A[min];//record the current smaller valueA[min] = A[max];//assigns a large subscript value to a lower valueA[max] = tmp;//assigns a smaller subscript value to a larger valuemin++; Max--; }     for(inti =0; I <Ten; i++) {printf ("a[%d]=%d\n", I,a[i]); } System ("Pause");}

Operation Result:

C Language Basics (10)-Arrays

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.