The most, most fundamental, most basic c---construction data type: array

Source: Internet
Author: User
Tags array definition strcmp

Array definition: A set of variables of the same data type arranged in a certain order; the name of the set is called the array name ; Each variable has a number, which is called subscript ; Each variable that makes up the array is also called an element of the array.

About "is an array a data type"? (The answer to this question is online query)

1 , Construction type:

a , interpreted from the definition of a type: each constructor (that is, each abstract value in the set of abstract values that make up the type) is composed of several data that belong to a certain data type, in accordance with certain rules.

b , interpreted from a data structure perspective: Each node (data element) in a struct is a data type that belongs to a data category.

C , in summary, the structure is analyzed from the data type angle, which is a data type (a value set + an action set), and the type is constructed from several data that belong to a data type.

D , Myth: Construction types are not constructed by some "types", but by some "type data".

2 , an array is a construct, so from a type point of view, an array is certainly a type (in fact, an array is really an important data type in a medium and high-level language).


One-dimensional arrays

A one-dimensional array is the simplest array, and its logical structure is a linear table

Definition: Type + array name [array size (constant expression: constant and symbolic constant)], describes a set of identical data types ( when defining an array, be sure to specify the number of array elements )

Array Memory: Sum of all members, memory is contiguous

Initialize: array name []={};

access to array elements: array name [subscript: 0-Array size-1]

array out of bounds: subscript out of range

" gadget ": sizeof can be used to obtain the length of an array.

Array length = sizeof (array name)/sizeof (array element type)

Note : a one-dimensional array name is the first address (first address: The address of the first element of the array)

two-dimensional arrays definition: Type + array name [Dimension 1: Row] [Dimension 2: Column]

Memory: Continuous storage, store by row, treat each row of a two-dimensional array as a one-dimensional array

Initialize: array name []={{},{},{}};

1. You can only assign the initial value to some elements, and the elements without the initial values automatically take 0.

2. If all elements are assigned an initial value, the length of the first dimension may not be given.

access to array elements: array name [subscript: 0-Row Size-1] [subscript: 0-Column size-1]

traversal of an array

Note : You must specify the length of the two-dimensional (that is, the value of the specified column), or the compilation does not pass

character Array

Definition: An array for storing character data, each element holding one character

because character types and integers are common to each other, you can also define an integer array to hold character data

One-dimensional arrays are initialized with string constants

string constants have their own independent memory, just copy the memory characters to the array

End-of-string marker: ' + ', in order to measure the actual length of the string, takes one byte

Char c[10]= "AB\0CD"; The string ends with a .

Two input and output modes: input and output one character with the format character "%c"

Enter and output the entire string with the format "%s", which is the input and output of the string

String handler function :

puts function: puts (character array) outputs a string (a sequence of characters ending with '% ') to the terminal

puts the function output string can contain escape characters

gets function: Gets (character array) enters a string from the terminal into a character array, and gets a function value (typically used by the Get function to enter a string into a character array, rather than the value of its function)

Note: the puts function and the Get function can only input and output a single string

strcat function: String join function strcat (character array 1, character array 2)

The string 2 is appended to the string 1, the result is placed in the character array 1, after the function call to get a function value----character array 1 address

strcpy function: string copy function strcpy (character array 1, character array 2)

Copy the string 2 into the character array 1

Note : You cannot assign a character constant or a character array directly to an array of characters using an assignment statement

strncpy functions: strncpy (character array 1, character array 2, constant N)

copies the first n characters in the character array 2 to the character array 1, replacing the first n characters in the character array 1

strcmp function: string comparison function strcmp (string 1, string 2)

compares two strings from left to right by character (by ASCII value size comparison)

Note : The English letter position is larger, lower case letters > larger letters

The return value is a positive number, negative, 0

strlen function: function strlen (character array) to measure the length of a string

The value of the function is the actual length of the string (does not contain '% ')

sizeof the difference between the strlen and the :

sizeof: to find the size of the data type

strlen: to find the length of the string (not including " \")

strlwr function: Convert large uppercase letters in a string to lowercase strlwr (string)

STRUPR function: Replace lowercase letters in a string with uppercase letters STRUPR (string)

Array Usage rules

1. The type of the array is actually the value type of the exponent group element. For the same array, all of its elements have the same data type.

2. The writing rules of the array name shall conform to the written provisions of the identifiers.

3. The array name cannot be the same as other variable names.

4. You can assign an initial value to only a subset of elements. When the number of values in the {} is less than the number of elements, only the previous elements are assigned values. For example: static int a[10]={0,1,2,3,4}; indicates that only a[0]~a[4]5 elements are assigned, and then 5 elements are automatically assigned a value of 0.

5. You can assign values to elements only, not to the whole array. For example, all 10 elements are assigned a value of 1, can only be written as: Static inta[10]={1,1,1,1,1,1,1,1,1,1}, and cannot be written as: static int a[10]=1; (Note: This is true in the C language, but not in all areas involving arrays)

6. If you do not assign an initial value to an initialized array, all elements are 0 values.

7. If you assign a value to all elements, in the array description, you can not give the number of elements of the group. For example: static int a[5]={1,2,3,4,5}; can be written as: Static inta[]={1,2,3,4,5}; Dynamic assignment can be dynamically assigned to an array during program execution. The loop statement is then used with the SCANF function to assign values to the array element.


This article is from the "Graduation---Awakening---walk" blog, please be sure to keep this source http://tjy888.blog.51cto.com/5576345/1714411

The most, most fundamental, most basic c---construction data type: 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.