Array Easy Production

来源:互联网 发布:国泰君安csmar数据库 编辑:程序博客网 时间:2024/05/21 07:57
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Array is a collection of the same type variable, by the name of a common reference these variables.A particular element in the array can be accessed through a subscript,.In C/C + + array is composed of continuous storage unit.The lowest address corresponding to the first unit of the array, the highest address corresponding to the last unit of the array.
A one-dimensional array:
General form type var_name [size];Can access the elements in the array by means of an array name list.The total length: the total number of bytes = sizeof (basic types) * (array length).C/C + + no array bounds checking.Can override an array of each side, and write some other variable array or even write the code of the program.

#include<stdio.h>
int main()
{
       int arr[100];
       int i, j;
       /*load x with value 0 throught 99*/
       for (i = 0; i < 100; i++)
       {
              arr[i] = i;
       }
       /*display content of x*/
       for (j = 0; j < 100; j++)
       {
              printf("%d ", arr[j]);//存0~99
       }
       return 0;
}

Generated pointer to an array, by specifying the array name, rather than any subscript, can generate a pointer to the first element of array 

#include<stdio.h>
int main()
{
       int count[100], i;
       for (i = 0; i <= 100; i++)
       {
              count[i] = 1;
       }
       return 0;
}
Generate a pointer to the array:
By specifying the array name, rather than any subscript, said to have a pointer to the first element of array.

Can use the & operator to specify the address of the first array element.
By the end of a null string:
When declaring a deposit will end with a null string array of characters, need to declare it for longer than its greatest string to store one character at a time.
C language function of ends with a null string.
strcpy ()    strcat ()    strcmp ()   strlen ()   strchr ()  strstr ()
write a easy example to achieve memorize array

#include<stdio.h>
int main()
{
       int a[4][5];
       int i, j;
       for (i = 0; i < 4; i++)
       {
              for (j = 0; j < 5; j++)
              {
                     a[i][j] = i * 5 + j;
              }
       }
       for (i = 0; i < 4; i++)
       {
              for (j = 0; j < 5; j++)
              {
                     printf("%d ", a[i][j]);
              }
       }
       return 0;
}
An array of strings
To create a ends with a null string array, using an array of two characters.On the left side of the subscript decide the size of the string, and on the right side of the subscript size specifies the maximum length of each string.
Practice:
Use a string array as the basis of a simple text editor.
Function: accept multiline input, know to accept a blank line, the end of the input, the input all the lines printed on the screen

#include<stdio.h>
#define MAX 100
#define LEN  80

char text[MAX][LEN]

int main()
{
     int t, i, j;
     printf("Enter an empty to quit!\n");
     for (t = 0; t < MAX; t++)
     {
          printf("%d: ", t);
          gets(text[t]);
          if (!*text[t])
               break;
     }
     for (i = 0; i < t; i++)
     {
          for (j = 0; text[i][j]; j++)
          {
          putchar(text[i][j]);
          }
          putchar('\n');
          puts(text[i]);
     }
     return 0;
}

Initialization of the array
C/C + + allows initialization in the array.
An array of applications: a board game
0 0
原创粉丝点击