不同的sizeof结果 [英] Different sizeof results

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

问题描述

为什么 N 不等于 8 在下面的函数?

 无效美孚(字符cvalue [8])
{
  INT N = sizeof的(cvalue);
}

N 确实的平等 8 在这个版本的功能:

 空白栏()
{
  炭cvalue [8];
  INT N = sizeof的(cvalue);
}


解决方案

由于无法通过在C.功能参数你实际上一个指针传递给它整个阵列;括号是语法糖。有没有保证你指向数组的大小为8,因为你可以通过这个功能,任何你想要的字符指针。

  //这些都做同样的事情
无效美孚(CHAR cvalue [8])
无效美孚(CHAR cvalue [])
无效美孚(字符* cvalue)

Why does n not equal 8 in the following function?

void foo(char cvalue[8])
{
  int n = sizeof (cvalue);
}

But n does equal 8 in this version of the function:

void bar()
{
  char cvalue[8];
  int n = sizeof (cvalue);
}

解决方案

Because you can't pass entire arrays as function parameters in C. You're actually passing a pointer to it; the brackets are syntactic sugar. There are no guarantees the array you're pointing to has size 8, since you could pass this function any character pointer you want.

// These all do the same thing
void foo(char cvalue[8])
void foo(char cvalue[])
void foo(char *cvalue)

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

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