用C的sizeof行为 [英] Behaviour of Sizeof in C

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

问题描述

据我了解,当我们通过数组名称为sizeof,数组的名称不衰减到指针基址。下面的code。通过给答案10验证了这一事实。

 的#include<&stdio.h中GT;诠释主(){
    INT ARR [10];
    的printf(数组的大小为%d,sizeof的(ARR)/的sizeof(INT));
    返回0;
}

然而,当我运行下面的code,答案来得1.不论尺寸是否是用原型与否,答案是1。为什么会这样呢?

 的#include<&stdio.h中GT;无效尺寸(INT ARR []){
    的printf(Sizof数组为%d,sizeof的(ARR)/的sizeof(INT));
}
诠释主(){
    INT ARR [10];
    尺寸(ARR);
    返回0;
}


解决方案

此签名

 无效尺寸(INT ARR [])

绝对等同于

 无效尺寸为(int * ARR)

另请参阅问题6.4

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

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

解决方案

This signature

void dimension(int arr[])

is absolutely equivalent to

void dimension(int *arr)

See also Question 6.4

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

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