sizeof(array)/ sizeof(int) [英] sizeof(array) / sizeof(int)

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

问题描述

我在一个函数中声明了一个数组:

Within a function I have declared an array:

int char_count_array[118] = {0};

稍后,我将此数组传递给函数并计算以下内容:

Later on, I pass this array to a function and calculate the following:

int xx = sizeof(char_count_array); 
int xy = sizeof(char_count_array)/sizeof(int);  

但是,我得到的结果是: xx = 4 xy = 1

However, the result I get is: xx = 4 xy = 1

我想我会得到: xx = 472(118 * 4)xy = 118(472/4)

有人会知道我在这里做错了吗?

Would anyone know what I am doing wrong here?

推荐答案

如果您通过将其添加到函数中,最有可能以 int * 而不是 int [118] 的形式出现。因此,您的 sizeof 返回的是指针的大小。当您将C数组传递给函数时,通常也要传递元素数。

If you are passing it to a function, it's most likely going in as int* instead of int[118]. So your sizeof is returning the size of a pointer. When you pass C arrays to functions, it's conventional to also pass the number of elements.

my_func( arr, sizeof(arr)/sizeof(arr[0]) );

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

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