Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usable case of pointer to array with unspecified bounds in C++ (not in C)

Consider following code:

int main() {     int (*p)[]; // pointer to array with unspecified bounds      int a[] = {1};     int b[] = {1,2};      p = &a; // works in C but not in C++     p = &b; // works in C but not in C++      return 0; } 

In pure C you can assign a pointer to this type of address of an array of any dimension. But in C++ you can't. I found one case when compiler allows assign value to such pointer:

struct C {     static int v[]; };  int main()  {     int (*p)[] = &C::v; // works in C++ if 'v' isn't defined (only declared)     return 0; } 

But could not find any useful case of this code.

Can anyone give an useful example (in C++) of pointer to array with unspecified bounds? Or is it only vestige remaining from C?

like image 693
αλεχολυτ Avatar asked Jun 29 '14 18:06

αλεχολυτ


People also ask

Can we use pointer as array in C?

Pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory.

How do you store a pointer address in an array?

Consider this example: int *ptr; int arr[5]; // store the address of the first // element of arr in ptr ptr = arr; Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of the first element of the array in variable ptr .

How array can be traversed with the pointers?

This is done as follows. int *ptr = &arr[0]; After this, a for loop is used to dereference the pointer and print all the elements in the array. The pointer is incremented in each iteration of the loop i.e at each loop iteration, the pointer points to the next element of the array.

Can you have an array of pointers?

In computer programming, an array of pointers is an indexed set of variables, where the variables are pointers (referencing a location in memory). Pointers are an important tool in computer science for creating, using, and destroying all types of data structures.


1 Answers

Such a pointer cannot participate in pointer arithmetic, potentially useful things that still can be done are to get its type with decltype or reinterpret_cast it to another pointer type or intptr_t. This is because section 3.9p6 says:

A class type (such as "class X") might be incomplete at one point in a translation unit and complete later on; the type "class X" is the same type at both points. The declared type of an array object might be an array of incomplete class type and therefore incomplete; if the class type is completed later on in the translation unit, the array type becomes complete; the array type at those two points is the same type. The declared type of an array object might be an array of unknown size and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points ("array of unknown bound of T" and "array of N T") are different types. The type of a pointer to array of unknown size, or of a type defined by a typedef declaration to be an array of unknown size, cannot be completed.

5.3.1 says:

Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1.

Since array-to-pointer decay can be performed on array lvalues without prior conversion to rvalue, the code dyp left in a comment is correct:

(*p)[i] 

Relevant rule, from 4.2:

An lvalue or rvalue of type 'array of N T" or "array of unknown bound of T" can be converted to a prvalue of type "pointer to T". The result is a pointer to the first element of the array.

like image 56
Ben Voigt Avatar answered Oct 09 '22 03:10

Ben Voigt