怎样的sizeof(数组)在运行时的工作原理? [英] How sizeof(array) works at runtime?

查看:174
本文介绍了怎样的sizeof(数组)在运行时的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读,在C sizeof操作符是在编译时PTED间$ P $,并且因为在编译时编译器知道数组大小和类型,sizeof的是体健计算由array.But占用的是如何的字节数sizeof的以下code工作:

I have read that sizeof operator in C is interpreted at compile time and since at compile time compiler knows the array size and its type,sizeof is abled to compute the number of bytes occupied by array.But how is sizeof working for the following code :

 #include<stdio.h>
 #include<string.h>
 int main()
 {
    int n;
    scanf("%d",&n);
    int a[n];
    int s=sizeof(a);
    printf("%d",s);
    return 0;
 }

下面数组大小是不是在编译时已知,那么它是如何工作正常?

Here array size is not known at compile time,then how is it working properly ?

推荐答案

的sizeof 总是在C89编译时进行计算。由于C99和可变长度阵列,它是在计算时,可变长度数组是在的sizeof 操作数的前pression的一部分运行时间。

sizeof is always computed at compile time in C89. Since C99 and variable length arrays, it is computed at run time when a variable length array is part of the expression in the sizeof operand.

同为的sizeof 的评价操作:它不是在C89而是C99进行评估,如果操作数是变长数组类型赋值。例如:

Same for the evaluation of the sizeof operand: it is not evaluated in C89 but in C99 if the operand is of variable length array type it is evaluated. For example:

int n = 5;
sizeof (int [n++]); 

// n is now 6

这篇关于怎样的sizeof(数组)在运行时的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆