C language-array and pointer

Source: Internet
Author: User

An array consists of a series of identical elements. The array declaration is used to tell the compiler that an array is required. The value declaration includes the number of array elements and the element type.

To access elements in the array, you can use subscript (index) to represent a single element,Index is counted from 0..

 

Array initialization:

For example

days[12]={31,29,31,30,31,30,31,31,30,31,30,31}

 

If the array is not initialized

For example

int no_data[4]

When an array element is printed, the value of the array is variable. The value used by the compiler is an existing value in the storage unit.

 

The number of elements in the initialization list should be the same as the array size. If the two are inconsistent:

1. For example, when the number of values is less than the number of array elements, the Redundant Array elements are initialized to 0. Example:

 1 #include <stdio.h>
2 #define SIZE 4
3 int main(void)
4 {
5 int some_data[SIZE]={1492,1066};
6 int i;
7 printf("%2s%14s\n","i","some_data[i]");
8 for(i=1;i<SIZE;i++)
9 printf("%2d%14d\n",i,some_data[i]);
10 return 0;
11 }

Output:

 i  some_data[i]
1 1066
2 0
3 0

2. If the number of items in the initialization list is greater than the array size, the compiler considers this error. The program cannot run.

 

The number in parentheses can be omitted to allow the compiler to automatically match the array size and the number of items in the initialization list.

1 # include <stdio. h>
2 int main (void)
3 {
4 const int days [] = {, 30, 31, 30 };
5/* const:
6. Sometimes you need to use a read-only array, that is, the program reads values from the array, but the program does not want to write data into the array. In this case, you can use the keyword const. In this way, the program treats each element in the array as a constant. After being declared with const, you cannot assign values to it */
7 int index;
8/* The operator sizeof gives the size of the object or type (in bytes )*/
9 for (index = 0; index <sizeof days/sizeof days [0]; index ++)
10 printf ("mon…… % 2d has % d days. \ n", index + 1, days [index]);
11 return 0;
12}

 

 

Multi-dimensional array

A multi-dimensional array is an array. For example, the monthly precipitation in five years is represented by float rain [5] [12. It can be interpreted that rain has five elements, and each element is an array containing 12 float values.

Using two-dimensional views to represent Arrays can intuitively imagine arrays with two indexes. In fact, arrays are stored sequentially. After the first 12 elements, the tiller is the second array containing 23 elements.

 

Notes for multi-dimensional array initialization:

1. For example:

const float rain[5][12]={
{4.3,4.3,1.2,3.0},
{5.2,3.0,1.6,3.5},
......
}

If there are only four values in the first list, only the first four elements in the first row are assigned a value, and the last eight elements are initialized to 0 by default. If there are more than 12 values in the list, an error is reported and the program cannot run,These values do not affect the value assignment of the next row.

2. During initialization, you can also omit the internal curly braces and keep only the outermost pair of curly braces. As long as the number of values is correct, the initialization effect is the same. If the number of values is not enough, values are assigned row by row during array initialization. The first element is assigned a value until no value is assigned.

 

Pointers and Arrays

The array name is also the address of the first element of the array.

1 # include <stdio. h>
2 # define SIZE 4
3 int main (void)
4 {
5 short dates [SIZE];
6 short * pti;
7. short index;
8 double bills [SIZE];
9 double * ptf;
10
11 pti = dates;/* assign the first address of the array to the pointer */
12 ptf = bills;
13 printf ("% 23 s % 10s \ n", "short", "double ");
14 for (index = 0; index <SIZE; index ++)
15 printf ("pointers + % d: % 10 p % 10p \ n", index, pti + index, ptf + index );
16 return 0;
17}

Output:

                  short     double
pointers + 0: 0028FF0C 0028FEE8
pointers + 1: 0028FF0E 0028FEF0
pointers + 2: 0028FF10 0028FEF8
pointers + 3: 0028FF12 0028FF00

In the preceding example, adding 1 to A pointer adds a storage unit to the pointer.

Because in C language: the result of adding 1 to A pointer is to add a storage unit to the pointer (that is, adding 1 to the pointer, equivalent to adding the byte size of the object to which the Pointer Points ). For an array, the address is added to the address of the next element, instead of the next byte.

For example, * (days + index) and days [index] are equivalent.

 

// Unfinished

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.