C-language one-dimensional arrays

Source: Internet
Author: User

one-dimensional arrays1. Definition of one-dimensional arrays

* The form of the definition is: type array name [number of elements]

int a[5];

* [] can only be placed after the name of the array, the following are the wrong wording:

Int[5] A; Error int[] b; Error

* [] The number must be a fixed value, can be a constant (such as 6, 8), a constant expression (such as 3+4, 5*7). You must never use a variable or variable expression to represent the number of elements, and in most cases do not omit the number of elements (except when the array is initialized as a function's formal parameters and arrays)

The following are the correct wording:

int  a[5];   INTEGER constant int  b[' A '];  Character constant, is actually 65int  c[3*4];  Integer constant expression

The following are the wrong wording:

int a[]; No number of elements specified, error int i = 9;int A[i]; Use variables to make the number of elements, error

2. Initialization of one-dimensional arrays

  2.1 Class at definition time

int  Arr[3] = {1,,2,3};

 2.2 Define and assign values first

int arr[3];arr[0] = 1;arr[1] = 2;arr[2] = 3;

  2.3 Error procedure

int = {Arr[3];arr};
Because ARR is an address and is a constant, constants cannot be re-assigned, such as 10=8; this is wrong.

  

3. Storage of one-dimensional arrays

When you define an array, the system allocates a contiguous amount of storage space by array type and number to store the array elements. Note that the array name represents the address of the entire array, which is the starting address of the array.

For example char arr[3] = {' A ', ' B ', ' C '};

In-memory allocations are as follows

Address Array Store Contents

0x1101 Arr[0] A

0x1102 Arr[1] B

0x1103 Arr[2] C

That is, arr's memory address is arr or &arr[0] = 0x1101

C-language one-dimensional 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.