当前位置:网站首页>[C language] one dimensional array

[C language] one dimensional array

2022-06-25 07:18:00 InfoQ

​ The concept and use of arrays

We want to put data into memory space , First, you must allocate memory space . So we put in ⑤ individual
int
  The value of the memory space of type , for example :
int
 
arr [5];
This allocates... In memory ⑤ individual  
int
  Type of memory space , common  4×5=20  Bytes , And gave them a name , It's called
arr.

  • Each element in the array has a sequence number , This number comes from  
    0
      Start , Not from the familiar  
    1
      Start , It's called subscript  
    (Index)
     . When using array elements , Indicate the subscript , for example :

  • arrname
     
    [Index]
  • arraName  Is the array name ,index  Subscript . for example :a[0]  It means the first one 1 Elements ,a[3]  It means the first one 2 Elements .

  • When using arrays , We often use the elements in the array , however   Then print out the values in turn  ( That is to assign initial values to the elements in the array one by one ). Then use the loop structure to output ( That is, read the values of array elements in turn ) As shown in the following code :

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i;
 int arr[10] = { 0 };
 for (i = 0; i < 10; i++)// take 1~10 Put it in the array
 {
 arr[i]=(i+1);
 }
 for (i = 0; i < 10; i++)
 {
 printf(&quot;arr[%d] = %d\n&quot;, i, arr[i]);// Output the array in turn
 }
 return 0;
}

 Click and drag to move
Running results : as follows

null
 Click and drag to move
​​

Variable  
i
  Is an array subscript , It's also --> The loop condition ; Use array subscripts as loop conditions , When the last element is reached, the loop ends . Array  
arr
  The largest subscript of is  
9
, That is, no more than  
10
, So we specify that the condition of the cycle is  
i<10
, once  
i
  achieve  
10
  You have to end the cycle .

  • So let's change the code above : Let the user enter ten numbers ! The code example is as follows :

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i;
 int number[10] = {0};
 printf(&quot;| Please enter ten numbers :|\n&quot;);
 for (i = 0; i < 10; i++)
 scanf(&quot;%d&quot;, &number[i]);
 for (i = 0; i < 10; i++)
 printf(&quot;%d &quot;, number[i]);
 return 0;
}

 Click and drag to move
Running results : as follows :

null
 Click and drag to move
​​


Array name

  • The array name is
    First element
    The address of . But there are
    Exceptions :
  • sizeof( Array name ) ——>&nbsp;  The array name represents the entire array  —  It calculates the size of the entire array , Unit is byte .
  • Address fetch (&)  Array name  ——>&nbsp;  The array name represents the entire array  —  It takes out the address of the entire array .


The definition of one-dimensional array

A one-dimensional array is used to store a set of data in a bit sequence . Its general form is as follows :

Type specifier &nbsp; &nbsp;  Array identifier  [ Constant expression ];
  • The type specifier represents : The type of all elements in the array , Is any basic data type or construction type .
  • The array identifier represents : The name of the array type variable , The naming rules are consistent with the identifier of the variable name . This is also called , Array identifier .
  • A constant expression represents : Defines the number of data elements stored in the array , That is, the length of the array .
  • int a[10];
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; explain   integer   Array  a, Yes  10  Elements .
  • float b[10],c[20];
    &nbsp;  explain   Single precision floating point   Array  b, Yes  10  Elements , Real array  c, Yes  20 Elements .
  • char ch[20];
      Description character array  ch, Yes  20  Elements


A reference to a one-dimensional array

  • After the array definition is completed , Use this array . You can use the elements of an array by referencing them . Be careful : The index to access the array is from
    0
    At the beginning .
The general form of an array is defined as follows :
  • Array identifier  
    [ Subscript ]
for example : Reference an array variable  
arr
  pass the civil examinations
A variable .
  • arr 
    [2];
Find out the total size of the array elements is :
int sz = sizeof(arr) / sizeof(arr[0]);
Examples of related topics , as follows :

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i = 0;
 int arr[10] = { 0 };
 int sz = sizeof(arr) / sizeof(arr[0]);// Calculate the total element size of the array
 arr[5] = 5;
 arr[6] = 7;
 for (i = 0; i < sz; i++)
 {
 printf(&quot;%d &quot;, arr[i]);
 }
 return 0;
}

 Click and drag to move
The following example shows the results of running :

null
 Click and drag to move
​​


🥏 There are a few things to note about arrays :

  • The array is being created ,【】 Give a constant in parentheses , You can't use variables ! Be careful : Out-of-service  
    const
      Keyword decoration is given to it , Its essence is still
    Variable .
  • In the array , for example :arr[5]  Can only be used in  arr[0]、arr[1]、arr[2]、arr[3]、arr[4], They can't be used  arr[5]. If you use it  arr[5], Will lead to
    The subscript crossing the line
    The situation of .
  • The subscript can be  
    Integer constant
      Or is it  
    Shaping expression
    .
  • The type of array actually refers to the value type of array elements . For the same array , The data types of all its elements are
    same
    .
  • String words , Array subscript is the character that only knows your array subscript , barring  
    '\0'
     .


Initialization of one dimensional array

  • In addition to using assignment statements to assign values to array elements one by one , Initialization assignment and dynamic assignment methods can also be used .
  • Array initialization assignment refers to giving initial values to array elements when defining an array . Array initialization is done in the compile phase . This will reduce running time , Increase of efficiency .

  • int a = 10;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //  initialization
  • int arr[5] = {1,2,3,4,5};
    &nbsp; //  Fully initialized
  • int arr1[5] = {1,2,3};
     &nbsp; &nbsp; &nbsp; //  Incomplete initialization , Incomplete initialization , The remaining uninitialized elements  
    The default is 0

null
 Click and drag to move
​​

  • If your subscript does not specify a subscript , It will see how many elements you have by default, so as to help you determine the subscript of the array .
  • int arr[] = {1,2,3,4,5};
  • int arr[5] = {1,2,3,4,5};
  • above ② Sentence codes are completely equivalent !!! It will be initialized according to the following data , To determine the subscript of the array !


One dimensional array memory storage mode

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i = 0;
 int arr[10] = { 0 };
 int sz = sizeof(arr) / sizeof(arr[0]);
 for (i = 0; i < sz; i++)
 {
 //%p -  Print in address format , In hexadecimal !
 printf(&quot;arr[%d] = %p\n&quot;, i, &arr[i]);
 }
 return 0;
}

 Click and drag to move
Running results : as follows

null
 Click and drag to move
​​

  • One dimensional arrays are stored continuously in memory !
  • As the array subscript grows , The address changes from ground to height !


🥏 practice : Use a one-dimensional array to save student names !

This code uses --->  Pointer array operation !

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i; // Control loop variable
 char *Arrname[5] = { 0 }; // Character pointer array
 // Indirect addressing through dereference operation
 Arrname[0] = &quot; name : Zhang San &quot;;
 Arrname[1] = &quot; Gender : male &quot;;
 Arrname[2] = &quot; Telephone :123456789&quot;;
 Arrname[3] = &quot; Student number :0&quot;;
 Arrname[4] = &quot; Address : The earth &quot;;

 for (i = 0; i < 5; i++)
 {
 printf(&quot;%s\n&quot;, Arrname[i]);
 printf(&quot;\n&quot;);
 }
 return 0;
}

 Click and drag to move
Running results : as follows

null
 Click and drag to move

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201236120194.html