Arrays and pointers-arrays

Array


What is an array?

An array consists of a series of elements of the same data type. When you need to use an array, you can tell the compiler how many elements and the types of these elements are contained in the array by declaring the array. The compiler correctly creates the array based on this information. The types that can be used for ordinary variables and array elements can be used. Consider the following array declaration:

/* 一些数组声明 */
int main(void){
    
    
    float one[365];       //内含365个float类型元素的数组
    char code[12];        //内含12个char类型的数组
    int two[50];          //内含50个int类型的数组
    double three[30];     //内含30个double类型元素的数组
    /*..........*/
}

To access the elements in the array, use the array subscript number (also called index) to represent each element in the array

Initialize the array

1. Bracket initialization method

#include "stdio.h"
#define months 12
int main(){
    
    
   int days[months]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
   int index;
   for(index=0;index<months;index++)
        printf("\n month %2d has %2d days.",index+1,days[index]);
   return 520;
}

Of course, this program is not perfect enough. What it shows is only the number of days per month in a normal year. As for the perfect program, I hope readers can use this initialization method to practice.

for loop initialization

#include "stdio.h"
#define months 12
int main(){
    
    
   int days[months];
   int index;
   for(index=0;index<months;index++){
    
    
        printf("\n please input the %dth number(int):",index+1);
        scanf("%d",&days[index]);
   }
   //初始化完成
   for(index=0;index<months;index++)
        printf("\n month %2d has %2d days.",index+1,days[index]);
   return 520;
}

Of course, this program will also have many small flaws, such as what to do if the content entered by the user is not an integer type. At this time, please read another article of mine on how to optimize scanf. https://blog.csdn.net/qq_42392049/article/details/112604480

Designated initializer (C99)

C99 adds a new feature: designated initializer (designated initializer). Use this feature to initialize the specified array element. For example, only initialize the last element in the array. For the traditional C initialization syntax, all elements before the last element must be initialized before it can be initialized:

     int arr[6] = {
    
    0,0,0,0,0,212}; // 传统的语法

C99 stipulates that you can use square bracketed subscripts in the initialization list to indicate the elements to be initialized:

int arr[6] = {
    
    [5] = 212}; //把arr[5]初始化为212

For general initialization, after initializing an element, uninitialized elements will be set to 0.

Code example:

#include <stdio.h>
#define months 12
int main(){
    
    
   int days[months]={
    
    31,28,[4]=31,30};
   int i;
   for(i=0;i<months;i++)
       printf("\n %2d   %d",i+1,days[i]);
   return 0;
}

The output of the program in the C99 compiler is as follows:
1 31
2 28
3 30
4 0
5 31
6 0
7 0
8 0
9 0
10 0
11 0
12 0

Assign values ​​to array elements

Here we focus on a few common wrong assignments

#define size 5
int main(){
    
    
    int oxen[size] ={
    
    5,3,2,9};     /*初始化没问题*/
    int yaks[size];
    yaks=oxen;        /*不允许*/
    yaks[size]=oxen[size];     /*数组下标越界*/
    yaks[size]={
    
    5,3,2,8};     /*不起作用*/
}

These errors are because C does not allow an array to be assigned to another array as a unit, nor does it allow assignment in the form of a curly brace list except initialization.

Specify the size of the array

int n=5;
int m=8;
float al[5];                    //可以
float a2[5*2+1]                 //可以
float a3[sizeof(int)+1];        //可以
float a4[-4]                    //不可以 数组大小必须大于0
float a5[0];                    //不可以
float a6[2.5];                  //不可以
float a7[(int)2.5]              //可以
float a8[n];                    //C99之前不允许;

Update the knowledge summary of the multidimensional array in two days.

Guess you like

Origin blog.csdn.net/qq_42392049/article/details/112604218