Sixth Chapter Array

Source: Internet
Author: User
Tags naming convention

    • Definition of one-dimensional array

For example:

int array[5];

An int is a type specifier, an array is an array name, and [5] is a constant expression, which means that it contains 5 array elements.

Note: (1) The constant expression must be enclosed in brackets [] and a semicolon must be appended to the end of the defined form;

(2) "type specifier" is used to describe the types of elements in an array;

(3) "constant expression" means the length of the array, that is, the number of elements;

(4) The array name is the identifier used to describe the array, and its naming convention is the same as the name of the identifier;

(5) According to the storage allocation method of the array, the order of each element in the array is stored sequentially in contiguous memory.

    • A reference to a one-dimensional array

1#include <stdio.h>2 3 voidMainvoid)4 {5     inta[5], I;6printf"Please input 5 int numbers:\n");7      for(i =0; I <5; i++)8     {9scanf"%d",&a[i]);Ten     } Oneprintf"output in reverse order is: \ n"); A      for(i =4; I >=0; i--) -     { -printf"%d", A[i]); the     } -}

    • Initialization of one-dimensional arrays
(1you can separate the values of each element with commas in the initialization section of the array:inta[5]={ A,4,5,6, +}; (2if the number of element values and the length of the array are equal, you can not specify the length of the arrays:inta[]={ A,4,5,6, +}; (3You can assign an initial value to only a subset of the elements of an array:inta[5]={ A, -,6};//The values of a[0], a[1], a[2] are initialized to 12, 47, 6, respectively.

    • Definition of a two-dimensional array
type specifier array name [variable expression 1][variable expression 2]; int a[2[3]; Note: (1)" variable expression 1" means the number of rows,"  Variable expression 2" indicates the number of columns (2) The order in which the two-dimensional arrays are stored in memory is: Store the first row and then the second row.

    • Initialization of two-dimensional arrays
The initialization method for a two-dimensional array is as follows: (1branches assign initial values to all elements:inta[2][3]={{1,7,6},{2,3, -}}; //The first curly brace is assigned to the first line in order(2you can write all the data in a curly brace:inta[2][3]={1,7,6,2,3, -};//based on the precedence principle of the data element storage, first assign the element of first row, then assign to the element of the second row(3You can assign an initial value to the first part of each row:int[2][3]={{1},{2,6}};//1,2,6 assigned to a[0][0],a[1][0],a[1][1](4If you assign an initial value to all elements, the length of the first dimension can be unspecified, but the length of the second dimension cannot be savedinta[][3]={1,7,6,2,3, -};//You can also assign an initial value to only some elements, but you should assign an initial value to a branch, such as:inta[][3]={{1},{2,6}};

    • Example of a two-dimensional array program

Enter a matrix in row order, and then output in column order precedence

#include <stdio.h>voidMainvoid){    inta[2][3], I, J; printf ("Please enter a 2x3 matrix: \ n");  for(i =0; I <2; i++)    {         for(j =0; J <3; J + +) {scanf ("%d",&A[i][j]); }} printf ("The output matrix is as follows: \ n");  for(j =0; J <3; J + +) {printf ("\ n");  for(i =0; I <2; i++) {printf ("%d", A[i][j]); }    }    return;}

Character array

    • String constants

A character array can be considered a collection of character variables. And it corresponds to a string constant, the so-called string constant is a pair of double quotation marks in the set of character constants. For example: "abc", "Hello" are string constants.

When the string is stored in memory, the system automatically adds a '/s ' as the end-of-string flag that represents the ASCII code value of 0 characters, and does not show that when the string is manipulated, the string ends if it encounters '. '

It is important to note that ' a ' is a character constant and contains only one char, while "a" is a string constant that contains two characters, the character ' a ' and the end of the string identifying the character ' \ n '.

    • Definition of character array
// character arrays include one-dimensional character arrays and multidimensional character arrays (for example in two dimensions) Char array name [variable expression]; Char array name [variable expression 1][variable expression 2]; for example: Char a1[// Declaration 1 string of length 10 char a2[3[ten // declares 3 strings with a length of 10

    • A reference to a character array

Sixth Chapter Array

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.