What do each of these arrays mean in C++? - c++

I received lecture slides for C++ that merely mention these without explaining what they mean and what are their differences:
int (*arr)[]={...};
int *(arr[])={...};
int (&arr)[]={...};
int &(arr[])={...}; // not allowed?
What do each of these mean? I tried running a program with some of these, but I'm getting errors because I don't know what to put in the initialization list.

int (*arr)[] = { ... };
This is not an array at all. This is a pointer to an array of unknown size. Note that this a scalar type. It is just a data pointer, no different in nature from any other data pointer. Which means that the only way to use { ... } initializer with it is to specify a single value of proper type inside the { ... }. E.g.
int (*arr)[] = { nullptr };
which is the same as
int (*arr)[] = nullptr;
int *(arr[]) = { ... };
This is indeed an array. The same thing can be expressed as
int *arr[] = { ... };
This is an array of int * pointers. It size will depend on how many initializers are supplied in { ... }. E.g.
int *arr[] = { nullptr, &some_int, &some_other_int };
declares arr as an array of 3 pointers of type int *.
int (&arr)[] = { ... };
This is not an array per se. This is a reference to an array of unknown size. Again, the only legal { ... } initializer in this case would be just one lvalue of type int [] inside the { ... }. If the array was declared const, you'd be able to attach it to a temporary array of ints using this syntax, e.g.
const int (&arr)[] = { 1, 2, 3 };
But without const it is not possible. (I have my doubts about the legality of this even with const though. GCC accepts it.)
int &(arr[]) = { ... };
This is an attempt to declare an array of references. Not allowed.

You need to use the Clockwise/Spiral Rule.
int (*arr)[]={...}; // pointer to array of int
int *(arr[])={...}; // array of int pointers
int (&arr)[]={...}; // a reference to an array of int
int &(arr[])={...}; // an array of int references
The last one though is illegal: Why arrays of references are illegal?

Related

Random 0's appearing in C++ array [duplicate]

This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
Closed 4 years ago.
In the program below the length of the array ar is correct in main but in temp it shows the length of the pointer to ar which on my computer is 2 (in units of sizeof(int)).
#include <stdio.h>
void temp(int ar[]) // this could also be declared as `int *ar`
{
printf("%d\n", (int) sizeof(ar)/sizeof(int));
}
int main(void)
{
int ar[]={1,2,3};
printf("%d\n", (int) sizeof(ar)/sizeof(int));
temp(ar);
return 0;
}
I wanted to know how I should define the function so the length of the array is read correctly in the function.
There is no 'built-in' way to determine the length inside the function. However you pass arr, sizeof(arr) will always return the pointer size. So the best way is to pass the number of elements as a seperate argument. Alternatively you could have a special value like 0 or -1 that indicates the end (like it is \0 in strings, which are just char []).
But then of course the 'logical' array size was sizeof(arr)/sizeof(int) - 1
Don't use a function, use a macro for this:
//Adapted from K&R, p.135 of edition 2.
#define arrayLength(array) (sizeof((array))/sizeof((array)[0]))
int main(void)
{
int ar[]={1,2,3};
printf("%d\n", arrayLength(ar));
return 0;
}
You still cannot use this macro inside a function like your temp where the array is passed as a parameter for the reasons others have mentioned.
Alternative if you want to pass one data type around is to define a type that has both an array and capacity:
typedef struct
{
int *values;
int capacity;
} intArray;
void temp(intArray array)
{
printf("%d\n", array.capacity);
}
int main(void)
{
int ar[]= {1, 2, 3};
intArray arr;
arr.values = ar;
arr.capacity = arrayLength(ar);
temp(arr);
return 0;
}
This takes longer to set up, but is useful if you find your self passing it around many many functions.
As others have said the obvious solution is to pass the length of array as parameter, also you can store this value at the begin of array
#include <stdio.h>
void temp(int *ar)
{
printf("%d\n", ar[-1]);
}
int main(void)
{
int ar[]= {0, 1, 2, 3};
ar[0] = sizeof(ar) / sizeof(ar[0]) - 1;
printf("%d\n", ar[0]);
temp(ar + 1);
return 0;
}
When you write size(ar) then you're passing a pointer and not an array.
The size of a pointer and an int is 4 or 8 - depending on ABI (Or, as #H2CO3 mentioned - something completely different), so you're getting sizeof(int *)/sizeof int (4/4=1 for 32-bit machines and 8/4=2 for 64-bit machines), which is 1 or 2 (Or.. something different).
Remember, in C when pass an array as an argument to a function, you're passing a pointer to an array.If you want to pass the size of the array, you should pass it as a separated argument.
I don't think you could do this using a function. It will always return length of the pointer rather than the length of the whole array.
You need to wrap the array up into a struct:
#include<stdio.h>
struct foo {int arr[5];};
struct bar {double arr[10];};
void temp(struct foo f, struct bar g)
{
printf("%d\n",(sizeof f.arr)/(sizeof f.arr[0]));
printf("%d\n",(sizeof g.arr)/(sizeof g.arr[0]));
}
void main(void)
{
struct foo tmp1 = {{1,2,3,4,5}};
struct bar tmp2;
temp(tmp1,tmp2);
return;
}
Inside the function ar is a pointer so the sizeof operator will return the length of a pointer. The only way to compute it is to make ar global and or change its name. The easiest way to determine the length is size(array_name)/(size_of(int). The other thing you can do is pass this computation into the function.

How to read the function pointer?

How to interpret the following c++ declaration?
int (*(*x[2])())[3];
This is from the example present at Type-cppreference.
The interpretation of the sample declaration in the question is present in the Type-cppreference page linked.
int (*(*x[2])())[3]; // declaration of an array of 2 pointers to functions
// returning pointer to array of 3 int
So the actual question is not about that case in particular; but about how any C++ declaration shall be read.
You can infer all those details starting at the declared name x and moving clockwise respecting parenthesis. You will get to the description above:
x is a an array of dimension 2 of pointers to functions that return a pointer to an array of dimension 3 of ints.
Better explained here as the Clockwise/Spiral Rule: http://c-faq.com/decl/spiral.anderson.html
It is an array of two pointers to functions that return pointer to array of type int[3] and do not have parameters.
Here is a demonstrative program
int ( *f() )[3] { return new int[2][3]; }
int ( *g() )[3] { return new int[4][3]; }
int main()
{
int (*(*x[2])())[3] = { f, g };
for ( auto func : x ) delete []func();
return 0;
}

Returning an array ... rather a reference or pointer to an array

I am a bit confused. There are two ways to return an array from a method. The first suggests the following:
typedef int arrT[10];
arrT *func(int i);
However, how do I capture the return which is an int (*)[]?
Another way is through a reference or pointer:
int (*func(int i)[10];
or
int (&func(int i)[10];
The return types are either int (*)[] or int (&)[].
The trouble I am having is how I can assign a variable to accept the point and I continue to get errors such as:
can't convert int* to int (*)[]
Any idea what I am doing wrong or what is lacking in my knowledge?
If you want to return an array by value, put it in a structure.
The Standard committee already did that, and thus you can use std::array<int,10>.
std::array<int,10> func(int i);
std::array<int,10> x = func(77);
This makes it very straightforward to return by reference also:
std::array<int,10>& func2(int i);
std::array<int,10>& y = func2(5);
First, the information you give is incorrect.
You write,
“There are two ways to return an array from a method”
and then you give as examples of the ways
typedef int arrT[10];
arrT *func(int i);
and
int (*func(int i))[10];
(I’ve added the missing right parenthesis), where you say that this latter way, in contrast to the first, is an example of
“through a reference or pointer”
Well, these two declarations mean exactly the same, to wit:
typedef int A[10];
A* fp1( int i ) { return 0; }
int (*fp2( int i ))[10] { return 0; }
int main()
{
int (*p1)[10] = fp1( 100 );
int (*p2)[10] = fp2( 200 );
}
In both cases a pointer to the array is returned, and this pointer is typed as "pointer to array". Dereferencing that pointer yields the array itself, which decays to a pointer to itself again, but now typed as "pointer to item". It’s a pointer to the first item of the array. At the machine code level these two pointers are, in practice, exactly the same. Coming from a Pascal background that confused me for a long time, but the upshot is, since it’s generally impractical to carry the array size along in the type (which precludes dealing with arrays of different runtime sizes), most array handling code deals with the pointer-to-first-item instead of the pointer-to-the-whole-array.
I.e., normally such a low level C language like function would be declared as just
int* func()
return a pointer to the first item of an array of size established at run time.
Now, if you want to return an array by value then you have two choices:
Returning a fixed size array by value: put it in a struct.
The standard already provides a templated class that does this, std::array.
Returning a variable size array by value: use a class that deals with copying.
The standard already provides a templated class that does this, std::vector.
For example,
#include <vector>
using namespace std;
vector<int> foo() { return vector<int>( 10 ); }
int main()
{
vector<int> const v = foo();
// ...
}
This is the most general. Using std::array is more of an optimization for special cases. As a beginner, keep in mind Donald Knuth’s advice: “Premature optimization is the root of all evil.” I.e., just use std::vector unless there is a really really good reason to use std::array.
using arrT10 = int[10]; // Or you can use typedef if you want
arrT10 * func(int i)
{
arrT10 a10;
return &a10;
// int a[10];
// return a; // ERROR: can't convert int* to int (*)[]
}
This will give you a warning because func returns an address of a local variable so we should NEVER code like this but I'm sure this code can help you.

What is T (& var)[N]?

In boost/utility/swap.hpp I have found this piece of code:
template<class T, std::size_t N>
void swap_impl(T (& left)[N], T (& right)[N])
{
for (std::size_t i = 0; i < N; ++i)
{
::boost_swap_impl::swap_impl(left[i], right[i]);
}
}
What are left and right? Are they references to arrays? Is this code allowed by C++ ISO standard 2003 or later?
A reference to an array of type T and length N.
This is a natural extension of C's pointer-to-array syntax, and is supported by C++03.
You could use cdecl.org to try to parse these complex type declarations.
What are left and right? Are they references to arrays? Is this code allowed by C++ ISO standard 2003 or later?
Yes. They're references to arrays.
That means, you can call swap_impl as:
int a[10]; //array
int b[10];
//...
swap_impl(a,b); //correct
But you cannot call swap_impl as:
int *a = new int[10]; //pointer
int *b = new int[10];
//...
swap_impl(a,b); //compilation error
Also note that you cannot do even this:
int a[10];
int b[11];
//...
swap_impl(a,b); //compilation error - a and b are arrays of different size!
Important point:
- Not only arguments must be arrays, but the arrays must be of same size!
This is the way to declare a reference to an array of T (of size N) named left and right. The code is legal C++.
This allows you to pass in:
int ones[5] = { 1,1,1,1,1 };
int twos[5] = { 2,2,2,2,2 };
swap_impl(ones, twos);
Then template type inference will know that you have T = int and N = 5 and do the in-place swap. If you mismatch the types or the size, you get a handy compilation failure.
Yes this is standard C++ allowed from very early on (its basically C with the addition of a reference).
Using typedefs makes it easier to read:
int main()
{
typedef int (&MyArray)[4];
int data[4];
MyArray dataRef = data;
}
It sort of mirrors the function typedef
typedef int (*MyFunc)();

Array initialization [c/c++]

Why this is not allowed?
#include <cstdio>
struct Foo {
int fooId;
char arr[ ];
} fooList[] =
{ {1, {'a', 'b'}},
{2, {'c', 'd'}}
};
int main()
{
for (int i = 0; i < 2; i++)
printf("%d %c\n", fooList[i].fooId, fooList[i].arr[0]);
}
whereas, this is allowed:
struct Foo {
int fooId;
char arr[2]; // this line modified
} fooList[] =
{ {1, {'a', 'b'}},
{2, {'c', 'd'}}
};
Only the last member of a C struct can be flexible as in arr[].
Shamelessly copying from paragraph 6.7.2.1, sub-paragraph 16 of the ISO C99 standard:
16 As a special case, the last element
of a structure with more than one
named member may have an incomplete
array type; this is called a flexible
array member. With two exceptions,
the flexible array member is ignored.
First, the size of the structure shall
be equal to the offset of the last
element of an otherwise identical
structure that replaces the flexible
array member with an array of
unspecified length.106)...
EDIT:
As for C++, see this. Bottom-line: flexible array members are not allowed in C++ at all - at least for the time being.
In C++ all members of an user defined type must have complete types, and the member arr does not have a complete type unless you give it a size.
In C, the struct definition would compile, but you might not get what you want. The problem is that an array without size is allowed at the end of a struct to be used as a proxy to access the contiguous block of memory after the instance. This allows a dumb vector implementation as:
typedef struct vector {
int size;
char buffer[];
} vector;
vector* create_vector( int size ) {
vector* p = (vector*) malloc( sizeof *p + size ); // manually allocate "size" extra
p->size = size;
};
int main() {
vector* v = create_vector(10);
for ( int i = 0; i < v->size; ++i )
printf("%d\n", v->buffer[i] );
free(v);
}
But the language does not allow you to initialize with the curly braces as the compiler does not know how much memory has to be held (in general, in some circumstances it can know). The size-less member of the struct is only a way of accessing beyond the end of the object, it does not hold memory in itself:
printf( "sizeof(vector)=%d\n", sizeof(vector) ); // == sizeof(int)
In C++03, this is not allowed in struct or class!
Comeau C++ compiler gives this error:
"ComeauTest.c", line 3: error:
incomplete type is not allowed
char arr[ ];
^
Exactly simlar question yesterday : Difference between int* and int[] in C++