C 语言中 Sizeof 的行为 [英] Behaviour of Sizeof in C

查看:27
本文介绍了C 语言中 Sizeof 的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,当我们将数组名称传递给 sizeof 时,数组名称不会衰减到指向基地址的指针.下面的代码通过给出答案 10 来验证这一事实.

I have learnt that when we pass the array name to sizeof, the name of the array does not decay to the pointer to base address. The code below verifies this fact by giving answer 10.

#include <stdio.h> 

int main(){  
    int arr[10];  
    printf("Size of array is %d" , sizeof(arr)/sizeof(int));  
    return 0;  
}

然而,当我运行下面的代码时,答案是 1.无论维度是否写在原型中,答案都是 1.为什么会这样?

However when I run the code below, the answer comes 1. Irrespective of whether a dimension is written in prototype or not , the answer is 1. Why is it so ?

#include <stdio.h>

void dimension(int arr[]){  
    printf("Sizof array is %d" , sizeof(arr)/sizeof(int));  
}


int main(){  
    int arr[10];  
    dimension(arr);  
    return 0;  
}  

推荐答案

此签名

void dimension(int arr[])

绝对等价于

void dimension(int *arr)

另见问题 6.4

这篇关于C 语言中 Sizeof 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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