c++结构体中数组的使用_C中的数组

c++结构体中数组的使用

In C language, arrays are reffered to as structured data types. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.

在C语言中, arrays引用为结构化数据类型。 数组定义为存储在连续内存位置中的同质数据的有限有序集合

Here the words,

这里的话,

  • finite means data range must be defined.

    有限 意味着必须定义数据范围。

  • ordered means data must be stored in continuous memory addresses.

    有序 意味着数据必须存储在连续的存储器地址中。

  • homogenous means data must be of similar data type.

    同质 意味着数据必须具有相似的数据类型。

使用数组的示例 (Example where arrays are used,)

  • to store list of Employee or Student names,

    存储员工或学生姓名列表,

  • to store marks of students,

    储存学生的分数,

  • or to store list of numbers or characters etc.

    或存储数字或字符等的列表。

Since arrays provide an easy way to represent data, it is classified amongst the data structures in C. Other data structures in c are structure, lists, queues, trees etc. Array can be used to represent not only simple list of data but also table of data in two or three dimensions.

由于数组提供了一种表示数据的简便方法,因此将其分类为C中的数据结构。c中的其他数据结构是structurelistqueuestree等。Array不仅可以用来表示简单的数据列表,还可以用来表示表。二维或三个维度的数据。

声明一个数组 (Declaring an Array)

Like any other variable, arrays must be declared before they are used. General form of array declaration is,

像任何其他变量一样,必须在使用数组之前声明它们。 数组声明的一般形式是

data-type variable-name[size];

/* Example of array declaration */

int arr[10];
array declaraction in c

Here int is the data type, arr is the name of the array and 10 is the size of array. It means array arr can only contain 10 elements of int type.

这里的int是数据类型, arr是数组的名称,10是数组的大小。 这意味着数组arr只能包含10个int类型的元素。

Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0] address and the last element will occupy arr[9].

数组的索引0开始到size-1,arr数组的第一个元素将存储在arr[0]地址,最后一个元素将占据arr[9]

数组初始化 (Initialization of an Array)

After an array is declared it must be initialized. Otherwise, it will contain garbage value(any random value). An array can be initialized at either compile time or at runtime.

声明数组后,必须对其进行初始化。 否则,它将包含垃圾值(任何随机值)。 数组可以在编译时或在运行时初始化。

编译时数组初始化 (Compile time Array initialization)

Compile time initialization of array elements is same as ordinary variable initialization. The general form of initialization of array is,

数组元素的编译时初始化与普通变量初始化相同。 数组初始化的一般形式是:

data-type array-name[size] = { list of values };

/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 };    // integer array initialization

float area[5]={ 23.4, 6.8, 5.5 };   // float array initialization

int marks[4]={ 67, 87, 56, 77, 59 };    // Compile time error

One important thing to remember is that when you will give more initializer(array elements) than the declared array size than the compiler will give an error.

要记住的一件事是,当您给初始化器(数组元素)提供的值比声明的数组大小多时, 编译器将给出一个错误。

#include<stdio.h>

void main()
{
    int i;
    int arr[] = {2, 3, 4};      // Compile time array initialization
    for(i = 0 ; i < 3 ; i++) 
    {
        printf("%d\t",arr[i]);
    }
}

2 3 4

2 3 4

运行时数组初始化 (Runtime Array initialization)

An array can also be initialized at runtime using scanf() function. This approach is usually used for initializing large arrays, or to initialize arrays with user specified values. Example,

数组也可以在运行时使用scanf()函数初始化。 此方法通常用于初始化大型数组,或使用用户指定的值初始化数组。 例,

#include<stdio.h>

void main()
{
    int arr[4];
    int i, j;
    printf("Enter array element");
    for(i = 0; i < 4; i++)
    {
        scanf("%d", &arr[i]);    //Run time array initialization
    }
    for(j = 0; j < 4; j++)
    {
        printf("%d\n", arr[j]);
    }
}

二维数组 (Two dimensional Arrays)

C language supports multidimensional arrays also. The simplest form of a multidimensional array is the two-dimensional array. Both the row's and column's index begins from 0.

C语言也支持多维数组。 多维数组的最简单形式是二维数组。 行索引和列索引都从0开始。

Two-dimensional arrays are declared as follows,

二维数组声明如下:

data-type array-name[row-size][column-size] 

/* Example */
int a[3][4];
two dimensional array in c

An array can also be declared and initialized together. For example,

数组也可以一起声明和初始化。 例如,

int arr[][3] = {
    {0,0,0},
    {1,1,1}
};

Note: We have not assigned any row value to our array in the above example. It means we can initialize any number of rows. But, we must always specify number of columns, else it will give a compile time error. Here, a 2*3 multi-dimensional matrix is created.

注意:在上面的示例中,我们没有为数组分配任何行值。 这意味着我们可以初始化任意数量的行。 但是,我们必须始终指定列数,否则会产生编译时错误。 在这里,创建了一个2*3多维矩阵。

二维数组的运行时初始化 (Runtime initialization of a two dimensional Array)
#include<stdio.h>

void main()
{
    int arr[3][4];
    int i, j, k;
    printf("Enter array element");
    for(i = 0; i < 3;i++)
    {
        for(j = 0; j < 4; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 4; j++)
        {
            printf("%d", arr[i][j]);
        }
    }
}

翻译自: https://www.studytonight.com/c/arrays-in-c.php

c++结构体中数组的使用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值