C语言变长数组如何使用,能否详细解析其原理与操作步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1041个文字,预计阅读时间需要5分钟。
当代代码示例:pythondef greet(name): return Hello, + name + !
print(greet(Alice))
看如下代码:
#include<stdio.h> typedef struct { int len; int array[]; }SoftArray; int main() { int len = 10; printf("The struct's size is %d\n",sizeof(SoftArray)); return 0; }
运行结果:
[root@VM-0-7-centos mydoc]# ./a.out
The struct's size is 4
我们可以看出,_SoftArray结构体的大小是4,显然,在32位操作系统下一个int型变量大小刚好为4,也就说结构体中的数组没有占用内存。为什么会没有占用内
存,我们平时用数组时不时都要明确指明数组大小的吗?但这里却可以编译通过呢?这就是我们常说的动态数组,也就是变长数组。
本文共计1041个文字,预计阅读时间需要5分钟。
当代代码示例:pythondef greet(name): return Hello, + name + !
print(greet(Alice))
看如下代码:
#include<stdio.h> typedef struct { int len; int array[]; }SoftArray; int main() { int len = 10; printf("The struct's size is %d\n",sizeof(SoftArray)); return 0; }
运行结果:
[root@VM-0-7-centos mydoc]# ./a.out
The struct's size is 4
我们可以看出,_SoftArray结构体的大小是4,显然,在32位操作系统下一个int型变量大小刚好为4,也就说结构体中的数组没有占用内存。为什么会没有占用内
存,我们平时用数组时不时都要明确指明数组大小的吗?但这里却可以编译通过呢?这就是我们常说的动态数组,也就是变长数组。

