简体   繁体   English

指针有大小吗?

[英]Do pointers have a size?

In C++, when using a pointer to multidimensional array like, 在C ++中,当使用指向多维数组的指针时,

int arr[2][5];
int (*p)[5] = arr;

How does a int* is different from the one with size ie int (*)[5] ? int*与大小有什么不同,即int (*)[5]

Pointers are always the same size for any particular machine (virtual, or otherwise). 对于任何特定的机器(虚拟或其他),指针总是相同的大小。 On a 32-bit machine, pointers are 32-bits wide. 在32位机器上,指针是32位宽。 On a 64-bit machine, they are 64-bits wide. 在64位机器上,它们是64位宽。 Similar rules apply for more exotic (by today's standards) architectures. 类似的规则适用于更具异国情调(按今天的标准)架构。

The difference is that they're of different types. 不同之处在于它们的类型不同。

int* is a pointer to int; int*是指向int的指针; int (*)[5] is a pointer to an array of 5 ints. int (*)[5]是指向5个int的数组的指针。 (The cdecl program is useful for interpreting declarations like these.) cdecl程序对于解释这些声明非常有用。)

It's very likely (but by no means guaranteed) that they'll both have the same size and representation. 它很可能(但绝不保证)它们都具有相同的大小和表示。 The difference, as between any two types, is in the operations that can be applied to objects of those types, and the meanings of those operations. 任何两种类型之间的差异在于可以应用于这些类型的对象的操作以及这些操作的含义。

In response to the title, "Do pointers have a size?", certainly they do; 在回答标题时,“指针有大小吗?”,当然它们确实如此; the sizeof operator tells you what it is. sizeof运算符会告诉您它是什么。 But the 5 in int (*)[5] isn't the size of the pointer; 5int (*)[5]不是指针的大小; it's the number of elements in the array to which the pointer points. 它是指针指向的数组中的元素数。

If you have 如果你有

int *p = /* something */;
int (*q)[5] = /* something */;

Then *p is an int, but *q is an array of five ints, so (*q)[0] is an int. 那么* p是一个int,但* q是一个五个整数的数组,所以(* q)[0]是一个int。

你的'指针大小'是一个指针数组

During runtime there is no difference. 在运行时期间没有区别。 During compilation time it is remembered, whether pointer in question is an array or not, as well as its size, and compiler guarantees that no inappropriate conversions will be made. 在编译期间,它会被记住,无论指针是否是数组,以及它的大小,编译器保证不会进行不适当的转换。 If I'm not mistaken, inappropriate conversion in this case is conversion of common pointer to array pointer. 如果我没有弄错的话,在这种情况下不适当的转换是将公共指针转换为数组指针。

Also, as others have stated, during runtime pointer sizes are platform-dependent. 此外,正如其他人所说,在运行时指针大小与平台有关。 On PC they have the same size as int : 4 bytes on 32-bit platforms, 8 bytes on 64-bit platforms. 在PC上,它们具有与int相同的大小:32位平台上的4个字节,64位平台上的8个字节。

Pointers have always same size for particular type machine. 对于特定类型的机器,指针总是具有相同的尺寸。 Pointers have size of 4 bytes on 32 bit machine. 指针在32位机器上的大小为4个字节。 It does not matter it is pointing to any data type or array of any data type. 它指向任何数据类型或任何数据类型的数组并不重要。 Pointer are variable which holds the address of any object. 指针是变量,它保存任何对象的地址。

For example: 例如:

int main()
{
    int x = 0;
    int * p = &x;

    int arr[2][5];
    int (*pt)[5] = arr;

    cout<<sizeof(p)<<endl;
    cout<<sizeof(pt)<<endl;

    return 0;
}

You will get 4 for both the pointers. 两个指针都会得到4。

Yes, a poiner usually have a size of int. 是的,一个poiner通常有一个int的大小。 you can check the size using the sizeof operator. 您可以使用sizeof运算符检查大小。 For example: 例如:

int* p = NULL;
int size = sizeof(p);
printf("size is %d\n",size);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM