3

In C++, when using a pointer to multidimensional array like,

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

How does a int* is different from the one with size i.e. int (*)[5]?

iammilind
  • 68,093
  • 33
  • 169
  • 336
user103214
  • 3,478
  • 6
  • 26
  • 37

7 Answers7

5

Pointers are always the same size for any particular machine (virtual, or otherwise). On a 32-bit machine, pointers are 32-bits wide. On a 64-bit machine, they are 64-bits wide. Similar rules apply for more exotic (by today's standards) architectures.

David Alber
  • 17,624
  • 6
  • 65
  • 71
  • 4
    *Most* pointers are the same size, in particular, the ones relevant here. Pointers to member functions, in particular, tend to have a strange size. This is also not really what the OP is asking. – Thanatos Oct 17 '11 at 06:12
  • 3
    @Thanatos `static_assert(!std::is_pointer::value, "As strange as it is, pointers to member functions are not pointers.");`. – R. Martinho Fernandes Oct 17 '11 at 06:16
  • 3
    In fact, pointers to member functions [might actually be structures](http://blogs.msdn.com/b/oldnewthing/archive/2004/02/09/70002.aspx). – In silico Oct 17 '11 at 06:22
  • 1
    @Thanatos: regarding pointer to member functions... it depends :) on MVSC, they might have from 1 to 4 times the size of a regular pointer, an alternative, if I remember right, is that they could always have the size of a regular pointer but may not point directly to the function, and instead point to a thunk responsible for the various adjustements. – Matthieu M. Oct 17 '11 at 06:27
  • 2
    Pointers to member functions aside, pointers are the same size on most modern systems, but it's entirely possible for different pointer types to have different sizes. One plausible possibility: on a word-address machine, `int*` might be one word, but `char*` might consist of a word pointer plus an offset. – Keith Thompson Oct 17 '11 at 07:00
3

The difference is that they're of different types.

int* is a pointer to int; int (*)[5] is a pointer to an array of 5 ints. (The cdecl program is useful for interpreting declarations like these.)

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. But the 5 in int (*)[5] isn't the size of the pointer; it's the number of elements in the array to which the pointer points.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

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.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
0

Your 'pointer with size' is an array of pointers

Oscar
  • 65
  • 6
0

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.

Septagram
  • 9,425
  • 13
  • 50
  • 81
  • @Martinho, I was under the impression, that size of `int` still depends on the architecture on PC, as it changed from 16-bit to 32-bit many years ago :\ – Septagram Oct 17 '11 at 06:52
  • Okay, seems it might be either 64-bit and 32-bit: http://stackoverflow.com/questions/589575/size-of-int-long-etc/589586#589586 Unlucky me, I happen to be running i686 OS, otherwise I'd just check right now for GCC :\ – Septagram Oct 17 '11 at 07:01
0

Pointers have always same size for particular type machine. Pointers have size of 4 bytes on 32 bit machine. 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.

Arvind
  • 138
  • 2
  • 6
-1

Yes, a poiner usually have a size of int. you can check the size using the sizeof operator. For example:

int* p = NULL;
int size = sizeof(p);
printf("size is %d\n",size);
Belgi
  • 14,542
  • 22
  • 58
  • 68
  • `sizeof(int *)` has nothing to do with `sizeof int`. – Roland Illig Oct 17 '11 at 06:12
  • @Roland Illig he wanted to know the size of pointers, I gave him the code to check it on the machine he is working on. – Belgi Oct 17 '11 at 06:13
  • Generally you should assign sizes only to variables of type `size_t`, not `int`. In this case it doesn't matter though. Still (and I only saw it right now) the variable `p` is incompatible to the format `%d`. – Roland Illig Oct 17 '11 at 19:10