为什么在 C 中指向数组的指针的大小总是 8? [英] Why the size of pointer to array is always 8 in C?

查看:23
本文介绍了为什么在 C 中指向数组的指针的大小总是 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是比较数组本身大小和指向数组指针大小的简单代码.

#include int main(){int kkarray[100]= {1,0,};int (*kkpointer) = kkarray;printf("使用数组本身的数组大小:%ld \n" , sizeof(kkarray));printf("使用指向数组的指针的数组大小:%ld \n" , sizeof(kkpointer));}

但结果是,

4008

我可以理解第一个值是 400,但第二个值,我认为它也应该是 400,因为当我分配一个指向数组的指针 kkpointer 时,它有点像数组的名称,它是 const地址值.例如,我可以使用 kkpointer[0] 获取数组的第一个值.我想知道为什么我得到的是8"而不是 400.我不认为8"是地址本身的大小.请让我知道.谢谢

解决方案

任何指针的大小总是8 在你的平台上,所以它是平台相关的.>

sizeof 操作符不关心指针指向哪里,它给出指针的大小,在第一种情况下它只给出数组的大小,而这不是一样.

这是§的引述.6.5.3.4¶2 N1570 草案

<块引用>

sizeof 运算符产生其操作数的大小(以字节为单位),这可能是一个表达式或类型的括号名称.大小由类型决定操作数.结果是一个整数.如果操作数的类型是变长数组类型,操作数被评估;否则,不计算操作数并且结果是整数常量

§6.5.3.4¶4

<块引用>

当 sizeof 应用于具有 char、unsigned char 或带符号的字符(或其限定版本)结果为 1.当应用于具有数组类型的操作数,结果为数组中的总字节数.103) 当应用于具有结构或联合类型的操作数,结果是总数此类对象中的字节数,包括内部和尾随填充

如你所见,当应用于数组时,它给出了总字节数.但是如果你传递一个指针,那么结果将是它的类型的大小.

Belows are simple code that compares size of array itself and size of pointer to array.

#include <stdio.h>    
int main(){   
        int kkarray[100]= {1,0,};
        int (*kkpointer) = kkarray;

        printf("size of array using array itself : %ld \n" , sizeof(kkarray));        
        printf("size of array using pointer to array : %ld \n" , sizeof(kkpointer));   
}

but the result is,

400
8

I could understand that the first value is 400, but second one,, i think that it also should be 400 because as i assigned a pointer that points to array, kkpointer, it kinda acts like name of array, which is const address value. For example, i can get the first value of array using kkpointer[0]. I want to know why i got '8' instead of 400. I don't think the '8' is the size of address itself. please let me know. Thanks

解决方案

The size of any pointer is always 8 on your platform, so it's platform dependent.

The sizeof operator doesn't care where the pointer is pointing to, it gives the size of the pointer, in the first case it just gives the size of the array, and that is not the same.

This is a quote from § 6.5.3.4, ¶ 2 N1570 draft

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant

and § 6.5.3.4, ¶ 4

When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array.103) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding

so as you can see, when applied to the array, it gives the total bytes. But if you pass a pointer, then the result will be the size of it's type.

这篇关于为什么在 C 中指向数组的指针的大小总是 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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