Use of arrays

Source: Internet
Author: User
Tags array length strcmp

Array

Definition: An array is an ordered collection of data of the same type.

One-dimensional arrays

1, general form: type specifier array name [constant expression]; for example: int a[10]; Element is a[0]----a[9].

2. Variables are not allowed in constant expressions, and can contain constants or symbolic constants.

3. The array element subscript can be any integer constant, integer variable, or any integral expression.

4, you can assign values to the array elements, arrays can also participate in the operation of the same as simple variables.

5. When using a numeric array, you cannot refer to the entire array at a time, only to reference the elements individually.

6. When the whole assignment is needed, it can only be assigned at the same time as the whole definition. Such as

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

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

Or use the memory copy function

7. You can assign values to only a subset of the elements. For example:

int a[10]={5,8,7,6}; An element value that is not assigned after the default is 0.

8. You can assign values to all array elements without specifying the array length, for example:

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

However, neither the initial value nor the specified length is wrong. Example: int a[]; error.

9. Pointers to array elements

int a[10], *p;

The following two sentences are equivalent:
p=&a[0];
P=a;
According to the address arithmetic rule, a+1 is the address of a[1], A+i is the address of a[i].
Here we use pointers to the address of the array element and several representations of the content.
(1). P+i and A+i all represent the addresses of a[i], or they all point to the element I of the array, i.e.
Point to A[i].
(2). * (P+i) and * (A+i) indicate the contents of the objects referred to by P+i and A+i, that is, a[i].
(3). A pointer to an array element can also represent the form of a tuple, which means that it allows
Pointer variables are indexed, such as p[i] and * (p+i) equivalent.

Two-dimensional arrays

1. General form: type specifier array name [constant expression 1][constant expression 2]; for example:

int a[3][4]; can be seen as containing 3 one-dimensional arrays, each one-dimensional array containing 4 elements. Altogether 3*4=12 elements.

All elements are a[0][0],a[0][1],a[0][2],a[0][3]

A[1][0],A[1][1],A[1][2],A[1][3]

A[2][0],A[2][1],A[2][2],A[2][3]

┏━━━━┓┏━┳━┳━┳━┓

A─→┃a[0]┃─→┃0┃1┃2┃3┃
┣━━━━┫┣━╋━╋━╋━┫
┃a[1]┃─→┃4┃5┃6┃7┃
┣━━━━┫┣━╋━╋━╋━┫
┃a[2]┃─→┃8┃9┃10┃11┃
┗━━━━┛┗━┻━┻━┻━┛

A represents the first address of a two-dimensional array, but can also be considered as the first address of the No. 0 row of a two-dimensional array. A+1 represents the first address of line 1th, and a+2 represents the first address of line 2nd. If the first address of this two-dimensional array is 1000, since line No. 0 has 4 integer elements, a+1 is 1008, a+2
It is also 1016 (16-bit). As shown
A[3][4]
A┏━┳━┳━┳━┓
(1000) ─→┃0┃1┃2┃3┃
A+1┣━╋━╋━╋━┫
(1008) ─→┃4┃5┃6┃7┃
A+2┣━╋━╋━╋━┫
(1016) ─→┃8┃9┃10┃11┃
┗━┻━┻━┻━┛

2. An element subscript, like a one-dimensional array, can be any integer constant, integer variable, or any integral expression.

3. When the whole assignment is needed, it can only be assigned at the same time as the whole definition. For example:

int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; correct.

int a[3][4]; a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; error.

4, you can write all the data in a curly brace. For example:

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; correct.

5. You can assign values to only some of the elements. For example:

int a[3][4]={{1},{5},{9}}; the remaining unassigned elements default to 0.

int a[3][4]={{1},{5,6}}; can be seen as an int a[3][4]={{1,0,0,0},{5,6,0,0},{0,0,0,0}};

6. The first dimension length can be omitted when assigning values to all array elements, and the second dimension cannot be omitted. For example:

A[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

Can be written as a[][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

or a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};

7, in a two-dimensional array, we can also use the form of pointers to represent the address of each element.

A[0] is equivalent to * (a+0), a[1] is equivalent to * (a+1), so a[i]+j is equivalent to * (A+i) +j, which
Represents the address of the array element A[i][j].

Therefore, the two-dimensional array element A[i][j] can be represented as * (A[I]+J) or * (* (a+i) +j), which are all associated with
A[I][J] equivalent, or can also be written (* (A+i)) [j].

Character array

1, definition: char a[10]; The character array A has a length of 10. Each element can hold only one character. For example:

a[0]= ' h '; a[1]= ' a '; a[2]= ' P ';

2. Initialize:

Char a[]={' h ', ' A ', ' P ', ' P ', ' Y '}; Note that the length is 5

Char a[]= "Happy";

Char a[]={"Happy"}; Note that the char a[]= "happy" because the end of the string is automatically added, and the length is 6, not 5.

3. There is no string variable in C language, the input, storage, processing and output of string must be implemented by character array. can only be array pointers are not!

4, the input of the string.

scanf (); You can use%c to enter characters such as Char a[6];for (i=0;i<6;i++) scanf ("%c", &a[i]);

You can use%s as a string input, such as Char a[6];scanf ("%s", a), and note that a is not added &, because a is an array name and already represents the first address of the array.

Note: When entered as%s, begins with the first non-whitespace character and terminates at the first white-space character. For example: When you enter how is. Output only how.

Gets (); The function is to enter a string. With scanf (), the function is the same, but the space and carriage return are stored in the array, and finally automatically added ' \ '. Does not appear above the output is not complete the situation.

Called by: Gets (the array name); the header file "stdio.h" needs to be included.

5, the output of the string.

printf (); You can use the%c character-by-characters output, for example: Char a[6];for (i=0;i<6;i++) printf ("%c", A[i]);

You can use%s as a string output, such as Char a[6];p rintf ("%s", a);

Puts (); Outputs a string, ending with wrap.

Invocation form: puts (character array name or string constant); Include header file "Stdio.h"

Commonly used string processing functions (the following function requires the header file "String.h")

1, strlen () function is to test the length of the string. This does not include '/'. Use form strlen (array name or string constant)

2, strcat () function is to connect two strings. Call mode strcat (character array 1, character array 2); The merged string is stored in the character array 1.

3, strcmp () compares two strings for equality. The invocation method is strcmp (string 1, String 2), and the equality value is 0. A positive number when 1>2. A negative number when 1<2.

4, strcpy () copy the string. The invocation method is strcpy (character array 1, string 2), and the contents of 2 are copied to 1. 1 can only be a character array name.

Array of pointers
1. Defines an int *p[10];//pointer array containing 10 pointer elements
Which means that each element is a pointer

2. Initialize:

Char *week_day[8]=

{"Sunday",

"Monday",

"Tuesday",

"Wednesday",

"Thursday",

"Friday",

"Saturday",

Null

}; /* Description array of pointers. Each element in the array points to a string */

Char *week_day[8];

The array must specify a size, char *week_day[];

You can not specify an array size

char * week_day []=

{"Sunday",

"Monday",

"Tuesday",

"Wednesday",

"Thursday",

"Friday",

"Saturday",

Null

};

3. Use

int *p[10];

P[0]= "ASDFGHJ";

Char *a= "ASDFGHJK";

P[1]=a;

4. An array of pointers to the struct body

typedef struct {

Char s1[81];

Char s2[81];

Char s3[81];

} Rec;

Rec *A[10];

a[0]= (REC *) malloc (sizeof (REC));

strcpy (a[0]->s1, "Hello");

Free (a[0]);

As shown in the image on the right:

5. Array of pointers to functions

int Functiona (int event);

int functionb (int event);

.......

int Functionz (int event);

typdef Int (*pfunc) (int event); PFunc is an int (*) (int event) alias

PFunc Functionlist[z+1] =

{

Functiona,

FUNCTIONB,

... ,

Functionz

}; Array of pointers to function name strings

Use:

Functionlist[i] (event);


Array pointers

1. Definition:

Type descriptor (* pointer variable name) [length]

Note that "(* pointer variable name)" is not limited to the parentheses on both sides.
int (*p) [10];//array pointer, which can be used to point to an array of integers containing 10 elements

2. Use

int a[10], b[3][4],

p = &a; Note that taking one address address is equivalent to adding a pointer to the first level

P=b;

*p+1 points to b[0][1], * (p+i) +j points to the array element B[i][j].

struct array

1. Definition of structure array

Corresponding structure variable definition Method 1

struct student

{

Long num;

Char name[20];

char sex;

Char addr[20];

};

struct student students[3];

struct

{

Long num;

Char name[20];

char sex;

Char addr[20];

} Students[3];

2. Initialization of struct arrays

Initialization method: Add the ={initial list after the definition array}

Such as:

struct

{Long num;

Char name[20];

char sex;

Char addr[20];

} students[3]={{10101, "Li Lin", ' M ', "Beijing"},{10102, "Zhang", ' F ', "Shanghai"} ...} ;

3. Use

Students [i].sex= ' F ';

Array as function parameter

1. Array elements as function arguments

An array element is a "one-way value pass" as a function argument, as with a variable as an argument.

Parameters and formal parameter types should be consistent.

Fun (a[2]);//The value of element a[2] is passed.

2. The argument and the formal parameter of the array famous function.

Such as:

Main ()

{int array[10];

......

f (array,10);

......

}

f (int arr[],int n);

{ ......

}//Note that the sizeof (ARR) value is 4, which is just the address of the first element of the array.

Array is the real parameter group name, and ARR is the parameter group name. The array name is the first address of the array, and the actual parameter to the parameter transfer array name is actually the address of the transfer array, and the parameter gets the address and points to the same array. It's like having two different names for the same item.

Similarly, the value of the pointer variable is also an address, and the value of the array pointer variable is the first address of the group, or it can be used as a function parameter.

The following points should also be noted when using array names as function parameters:

A. The type of the parameter group and the real parameter group must be the same, otherwise it will cause an error.

B. The length of the parameter group and the real parameter group can be different, because at the time of invocation, only the first address is transmitted without checking the length of the shape parameter group. When the length of the parameter group is inconsistent with the real parameter group, there is no grammatical error (compile can pass), but the result of the program execution will be inconsistent with the actual, which should be paid attention to.

C. In the function parameter list, the length of the shape parameter group is allowed, or the number of array elements is represented by a variable.

For example, it can be written as:

void fun (int a[])

or write as

void Fun (int a[],int n)

Where the shape parameter group A does not give the length, and the N value dynamically represents the length of the array. The value of n is transmitted by the arguments of the keynote function.

D. Multidimensional arrays can also be used as arguments to functions. You can specify the length of each dimension when defining a function, or you can omit the length of the first dimension. Therefore, the following wording is legal.

int MA (int a[3][10])

Or

int MA (int a[][10]).

and the int MA (int a[3][]) is not allowed.

Use of 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.