如何在 c 语言中获取数组的大小-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > c语言 >

如何在 c 语言中获取数组的大小

作者:迹忆客 最近更新:2023/05/07 浏览次数:

本教程介绍了如何在 c 语言中确定一个数组的长度,sizeof() 运算符用于获取一个数组的大小/长度。


sizeof() 运算符在 c 语言中确定一个数组的大小

sizeof() 运算符是一个编译时的一元运算符。它用于计算操作数的大小。它返回变量的大小。sizeof() 运算符以字节为单位给出大小。

sizeof() 运算符用于任何数据类型,如 intfloatchar 等基元,也用于 arraystruct 等非基元数据类型。它返回分配给该数据类型的内存。

the output may be different on different machines like a 32-bit system can show different output and a 64-bit system can show different of the same data types.

sizeof() 的语法

sizeof(operand)
  • operand 是一个数据类型或任何操作数。

sizeof() 在 c 语言中用于原始数据类型的操作数

该程序使用 intfloat 作为原始数据类型。

#include  
int main(void) 
{ 
    printf("size of char data type: %u\n",sizeof(char)); 
    printf("size of int data type: %u\n",sizeof(int)); 
    printf("size of float data type: %u\n",sizeof(float)); 
    printf("size of double data type: %u\n",sizeof(double)); 
    return 0; 
}

输出:

size of char data type: 1
size of int data type: 4
size of float data type: 4
size of double data type: 8

用 c 语言获取数组的长度

如果我们用数组的总大小除以数组元素的大小,就可以得到数组中的元素数。程序如下。

#include
int main(void) 
{
    int number[16];
    size_t n = sizeof(number)/sizeof(number[0]);
    printf("total elements the array can hold is: %d\n",n);
    return 0;
}

输出:

total elements the array can hold is: 16

当一个数组作为参数被传送到函数中时,它将被视为一个指针。sizeof() 运算符返回的是指针大小而不是数组大小。所以在函数内部,这个方法不能用。相反,传递一个额外的参数 size_t size 来表示数组中的元素数量。

#include#include
void printsizeofintarray(int intarray[]);
void printlengthintarray(int intarray[]);
int main(int argc, char* argv[])
{
    int integerarray[] = { 0, 1, 2, 3, 4, 5, 6 };
    printf("sizeof of the array is: %d\n", (int) sizeof(integerarray));
    printsizeofintarray(integerarray);
    printf("length of the array is: %d\n", (int)( sizeof(integerarray) / sizeof(integerarray[0]) ));
    printlengthintarray(integerarray);
}
void printsizeofintarray(int intarray[])
{
    printf("sizeof of the parameter is: %d\n", (int) sizeof(intarray));
}
void printlengthintarray(int intarray[])
{
    printf("length of the parameter is: %d\n", (int)( sizeof(intarray) / sizeof(intarray[0]) ));
}

输出(在 64 位的 linux 操作系统中):

sizeof of the array is: 28
sizeof of the parameter is: 8
length of the array is: 7
length of the parameter is: 2

输出(在 32 位 windows 操作系统中):

sizeof of the array is: 28
sizeof of the parameter is: 4
length of the array is: 7
length of the parameter is: 1

上一篇:在 c 语言中将数组初始化为 0

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在c中将整数转换为字符

发布时间:2024/01/03 浏览次数:131 分类:c语言

本教程介绍了在c中将整数转换为字符的不同方法。在c编程语言中,将整数转换为字符在各种情况下都很重要。在c中,字符是以ascii值表示的,因此转换过程相对简单。

mongodb 中查询数组大小大于1的文档

发布时间:2023/05/10 浏览次数:464 分类:mongodb

在处理需要验证数组大小或查找大小大于或小于特定长度的元素的项目时,可能会使用 $size、$where、$exists 等 mongodb 运算符。下面讨论的方法可以帮助您解决数组长度或数组大小问题。

发布时间:2023/05/07 浏览次数:364 分类:c语言

本文介绍了如何在 c 语言中使用 typedef enum。使用 enum 在 c 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。

c 语言中的静态变量

发布时间:2023/05/07 浏览次数:167 分类:c语言

本文介绍了如何在 c 语言中使用静态变量。在 c 语言中使用 static 变量在函数调用之间保存变量值

c 语言中生成随机数

发布时间:2023/05/07 浏览次数:158 分类:c语言

本文演示了如何在 c 语言中生成随机数。使用 rand 和 srand 函数在 c 语言中生成随机数

发布时间:2023/05/07 浏览次数:129 分类:c语言

本文演示了如何在 c 语言中使用前缀增量与后缀增量运算符。c 语言中 i 和 i 记号的主要区别

发布时间:2023/05/07 浏览次数:275 分类:c语言

本文演示了如何在 c 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法

发布时间:2023/05/07 浏览次数:177 分类:c语言

本文介绍了如何在 c 语言中使用位掩码。使用 struct 关键字在 c 语言中定义位掩码数据

发布时间:2023/05/07 浏览次数:212 分类:c语言

本文演示了如何在 c 语言中使用标准库排序函数。使用 qsort 函数对 c 语言中的整数数组进行排序

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图