Array Easy Production

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值