?? fun()
{
int a[3]={3,3,4};
return &a;
}
what could be the compatible return type. Here the pointer is pointing to the array of 3 integers not just the pointer which points to integer array.
Aim is to return a pointer to array of 3 integers.
Everyone else has already told you why you shouldn’t do this as it is written, but here are the types you are interested in.
Given a declaration
int a[3], the type of the expression&aisint (*)[3](notint **), or “pointer to 3-element array of int”, such asand the signature for a function returning that type would be
int (*fun())[3] {...}.One other option nos didn’t show is this:
although this isn’t terribly useful; you don’t normally see allocations of single, fixed-size arrays like this. Somewhat more useful is allocating an array of those arrays:
Even so, if somebody needs to allocate a fixed-size array type, they usually hide it behind a typedef:
Abstraction is a good thing.
I’ve put up several iterations of this table before; you might find it handy.
Declaration: T a[N]; Expression Type Decays To Value ---------- ---- --------- ----- a T [N] T * Address of first element in a &a T (*)[N] n/a Address of a (same value as above, but different type) *a T n/a Same as a[0] a[i] T n/a Value at index i &a[i] T * n/a Address of value at index i sizeof a size_t Total number of bytes in a (N * sizeof T) sizeof a / sizeof *a size_t n/a Number of elements in a (N) Declaration: T a[N][M]; Expression Type Decays To Value ---------- ---- --------- ----- a T [N][M] T (*)[M] Address of first element in a[0] &a T (*)[N][M] n/a Address of a (same value as above, but different type) *a T [M] T * Same as a[0] a[i] T [M] T * Address of first element in array at index i &a[i] T (*)[M] n/a Address of array at index i (same value as above, but different type) *a[i] T n/a Same as a[i][0] a[i][j] T n/a Value at a[i][j] &a[i][j] T * n/a Address of value at index i,j sizeof a size_t n/a Total number of bytes in a (N * M * sizeof T) sizeof a / sizeof *a size_t n/a Number of subarrays in a (N) sizeof a[i] size_t n/a Total number of bytes in a[i] (M * sizeof T) sizeof a[i] / sizeof *a[i] size_t n/a Number of elements in a[i] (M)