I find the concepts in pointers to an array very confusing in the context of using them in functions.
Here in this case I am trying to get a whole array from a function. The whole array is generated within the function. Obviously my code is hopelessly nonsensical.

#include <iostream>
using namespace std;

void func(int &array);

int main()
{
      func(&array);
      cout << array[1];
      cin.get();
      return 0;
}

void func(int &array)
{
     int array[] = {10,20,30,40};
}

>>int &array

That does not create an array. All it is is a reference to an integer. If you want an array that you have one of two choices void foo(int array[]) or void foo(int *array) or just return the array

int* foo()
{
    int *ay = new int[5];
    for(int i = 0; i < 5; i++)
        ay[i] = i;
    return ay;
}

int main()
{
    int* ay = foo();
    delete[] ay;
}

Understanding pointers is CRITICAL to mastering C and C++. You should really go out onto the internet and read about pointers. You should work through some tutorials and examples. There is a lot of core concepts in pointer management. I don't think it would be appropriate to discuss it all here.

That being said, here is a concept that will help you. When you declare an array:

int myArray[10];

the variable myArray is actually a pointer. It is a pointer to the fist int in the array.

Please read up on pointers. It will be well worth your time. These will get you started:
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html
http://home.netcom.com/~tjensen/ptr/pointers.htm

>>int &array

or just return the array

int* foo()

I don't want to return an array via a function.
I know I can get it through referenced argument of a function. I want it that way.

EDIT, nvm. I need to check something first.

Understanding pointers is CRITICAL to mastering C and C++. You should really go out onto the internet and read about pointers. You should work through some tutorials and examples. There is a lot of core concepts in pointer management. I don't think it would be appropriate to discuss it all here.

I don't have much problem in pointers per se, but with pointer to arrays.....not even that.
How shall I say, may sound stupid, but its more of getting the concept of C++ syntax of pointers right.
I have gone through all the links you have mentioned before, but I don't find examples (specially passing them to and fro through arguments in functions) much.

Here's a slight tweak of AD's code to show what you asked about (with a little added for proof of concept). Notice how ugly/confusing all the extra syntax is compared to simply returning the pointer, like AD did.

#include <iostream>

void foo(int** ay)		//pointer to pointer to int
{
    *ay = new int[5];	//must dereference parameter to get to argument
    for(int i = 0; i < 5; i++)
        (*ay)[i] = i;	//must dereference parameter to get to argument
}

int main()
{
    int *ay = 0;
    foo(&ay);			//must send reference to pointer
	for(int i = 0; i < 5; i++)
		std::cout << ay[i];
    delete[] ay;
}

A second version using references instead, still not very nice, but you can see a difference from the first one.

#include <iostream>

void foo(int*& ay)		//reference to pointer to int
{
    ay = new int[5];
    for(int i = 0; i < 5; i++)
        ay[i] = i;
}

int main()
{
    int *ay = 0;
    foo(ay);
	for(int i = 0; i < 5; i++)
		std::cout << ay[i];
    delete[] ay;
}

Notice how ugly/confusing all the extra syntax is compared to simply returning the pointer, like AD did.

On the contrary I find the syntax in your code to be easier to comprehend even though AD's method is more elegant.
Thank you.
I have seen somewhere one more syntactical variation to get the whole array (close to the first version of yours) I shall post it maybe on Monday or so and seek your comments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.