C指针和数组/'的sizeof“运算符 [英] C pointers and arrays/ 'sizeof' operator

查看:169
本文介绍了C指针和数组/'的sizeof“运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  对于字符指针和阵列堆栈指针的区别

要说明我的问题:

int main(void){
    int myary[20];
    int *myaryPtr;
    myaryPtr = myary;

    sizeof(myary); // Will it return 80? Correct?
    sizeof(myaryPtr); // Will it return 4? Correct?
    return 0;
}

首先,是我的假设是正确的?

First off, is my assumption correct?

再假设我的假设是正确的,什么是详细的解释?我明白我的20个元素的数组是80个字节,而不是名称 myary 只是一个指向数组的第一个元素?所以,不应该也为4?

And then assuming my assumption is correct, what is the detailed explanation? I understand that my 20 element array is 80 bytes, but isn't the name myary merely a pointer to the first element of the array? So shouldn't it also be 4?

推荐答案

是的,你的假设是正确的,假设一个 INT 和指针都是4个字节长在你的机器。

Yes, your assumption is correct, assuming an int and a pointer are both 4 bytes long on your machine.

和没有,数组不是指针。数组名有时会衰变成在某些情况下的指针,但它们不是一回事。有专门为混乱的公共点的comp.lang.c常见问题的整节。

And no, arrays aren't pointers. The array name sometimes decays into a pointer in certain contexts, but they aren't the same thing. There is a whole section of the comp.lang.c FAQ dedicated to this common point of confusion.

这篇关于C指针和数组/'的sizeof“运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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