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

2023-05-16

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];

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];

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++结构体中数组的使用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c++结构体中数组的使用_C中的数组 的相关文章

  • Linux深入浅出PyTorch(一)安装及基础知识

    目录 PyTorch安装工具使用1 开发工具建议使用pycharm2 安装包管理工具建议使用Anaconda3 安装结果检查 Pytorch安装2 配置pytorch 虚拟环境3 在PyCharm中配置PyTorch虚拟环境 丰富的PyTo
  • AttributeError: ‘NoneType‘ object has no attribute +++ 错误

    AttributeError 39 NoneType 39 object has no attribute 43 43 43 错误 解决办法 xff1a if self pool is not None self pool reset nu
  • Win11系统联想小新Air14Plus笔记本Anaconda环境下安装PyTorch

    PyTorch 创建虚拟环境 conda create name 名称 python 61 3 6 xff08 python版本 xff09 eg conda create name pytorch python 61 3 6 进入虚拟环境
  • 恒源云配置环境过程

    1 下载oss 软件 2 打开oss 上传数据 输入命令login 登录恒源云账号 输入命令 mkdir oss datasets 在个人数据中建立文件夹 名为 34 datasets 34 输入命令 cp 个人数据 zip oss dat
  • ubuntu 无法进入开机界面

    1 按电源键强制关机 xff0c 然后按电源键重新启动电脑 xff0c 然后在光标选择自己使用的系统 不要按 Enter 回车键 xff0c 然后按 e 键 xff0c 进入grub编辑页面 将光标移动到倒数第二行 xff0c 然后在qui
  • iOS警报– UIAlertController

    In this tutorial we ll be discussing the UIAlertController class and how it is handy to create different types of Alerts
  • jquery 读取XML 文件 并按时间进行排序

    在开发中遇到了一个很烦躁的问题 xff0c 就说在jdk 1 4 的情况下 xff0c 读取远程的XML文件不能自动断开 xff0c 于是就想到了 xff0c 用jQuery来写 在测试的时候使用的是本地的文件 xff0c 可到最后发现 x
  • clickHouse相关知识详解

    clickHouse相关知识详解 clickHouse介绍大数据技术背景什么是clickHouseclickHouse核心特性clickHouse适用场景clickHouse不适用的场景使用clickHouse的大厂 clickHouse安
  • 抖音数据库解析总结

    目前在抖音打出的包里面 xff1a 在database文件夹下面存在存着许多数据库 xff0c 这个大概挨个梳理了一下 xff0c 有用目前就两个数据库 xff1a 抖音id im db eg 95034530671 im db xff1a
  • openfeign实现原理

    1 openfeign简介 OpenFeign 提供了一种声明式的远程调用接口 xff0c 它可以大幅简化远程调用的编程体验 调用其他服务接口像调用本地服务service方法一样丝滑顺畅 使用示例如下 xff1a 引入依赖 span cla
  • MySQL高阶知识点(一):SQL语句执行流程

    1 一条 SQL查询语句是如何被执行的 MySQL 的基本架构示意图如下所示 xff1a 大体来说 xff0c MySQL 可以分为 Server 层和存储引擎层两部分 Server 层包括连接器 查询缓存 分析器 优化器 执行器等 xff
  • Spring Bean生命周期

    1 概述 之前我们在总结Spring扩展点 xff1a 后置处理器时谈到了Spring Bean的生命周期和其对Spring框架原理理解的重要性 xff0c 所以接下来我们就来分析一下Bean生命周期的整体流程 首先Bean就是一些Java
  • MySQL高阶知识点(二):索引概述

    1 深入浅出索引 索引是一种用于快速查询和检索数据的数据结构 常见的索引结构有 B 树 xff0c B 43 树和 Hash 通常来讲 xff0c 索引就像一本中华字典的目录 xff0c 通过目录可以快速定位查找某个汉字在哪一页 xff0c
  • Spring Boot如何优雅提高接口数据安全性

    1 背景 最近我司业务上需要对接第三方各大银行平台 xff0c 调用第三方接口和提供接口供第三方调用 xff0c 这时候的对外open接口安全性就得重视了 xff0c 再有就是之前我在知乎上发布一篇 Spring Security实现后端接
  • python错误解决:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd1 in position 0: invalid continuat

    当处理文本文档时出现错误 xff1a UnicodeDecodeError utf 8 codec can t decode byte 0xd1 in position 0 invalid continuation byte 可试一下另存为
  • Centos7编译Golang报错:/usr/bin/ld: /tmp/go-link-xx0.o: unrecognized relocation (0x2a) in section `.text‘

    在低版本centos7 xff08 我的是7 4 xff09 中运行goland编译时报错 xff1a usr bin ld tmp go link 038275771 000010 o unrecognized relocation 0x
  • 解决 Macbook 连接蓝牙鼠标卡顿、飘的现象

    原因 xff1a 1 鼠标蓝牙缺陷 xff0c 蓝牙版本低 xff0c 电池电量低 xff1b 2 电脑性能卡顿和功能优先度 xff1b 3 2 4Ghz的蓝牙与2 4Ghz的Wi Fi起冲突 xff0c 环境干扰大 这里以解决2和3的问题
  • 使用Kotlin的Android TextView –全面教程

    Kotlin is the official programming language for Android apps development In this tutorial we ll be discussing TextViews
  • macOS Ventura 13解决某app软件“已损坏,无法打开”

    从12 4升级到13系统的安全选项被重置了 xff0c 时间长不记得怎么回事 xff0c 再次记录一下 如图报错 xff1a 方法一 xff1a 前往 设置 隐私与安全性 安全性 xff08 在设置最下面 xff09 强制打开已拦截的xxx
  • 解决MacOS Dock栏不能自动隐藏

    已经在设置里面打开了了自动隐藏MacOS Dock栏功能 xff0c 但是实际上不能自动隐藏 分别依次运行下面三行终端命令 xff1a defaults write com apple dock autohide delay int 0 d

随机推荐