In the first case, the foo function allocates an array and returns a pointer to it. You can then subscript this returned pointer to read the elements of the array.

In the second case, the pointer a in main has a value of 0. It doesn't point to any memory you can access, so you get an error when you try to read the memory to which it points.

The calls to foo(a) make a copy of the (zero) value of a in a temporary location for the use of the function. The function stores a pointer to the newly allocated memory in this temporary location, but this has no effect on the a variable in main. The a in foo is not the a in main, it is a copy of the value of a.

your foo returns a pointer to an int. in foo, you have created an array of ints, and you return a pointer to that array. as said above, foo only gets a copy of a from main, not a itself...so, what you put into a in foo, isn't getting into the a in main. this seems counter intuitive though, from what we learn about pointers.

but, what we did learn is that if we want to pass a variable and have any changes made to it from a function ACTUALLY occur to what we passed it, we pass it by reference. great thing is, we can do that here.

simply adding one character to the parameter list of foo keeps your program from erroring:
int* foo(int* &a)

note the added "&"

*shrug* sometimes things don't make sense...but hey...that's the fun in it all, lol

You can also remove the parameter from the function altogether, and write: 'int * a = new int[10] ;' in the foo () function, and call it in main () as: 'a = foo () ;' and then print the values usig only your second 'for' loop.

There is a difference between the two (passing a pointer by reference and passing a pointer by address). In the first case, its still a pointer variable which is formal argument and no text needs to be changed in the function. Also, the modifications made will be permanent and reflected in main (). In the second case, you are declaring another pointer in the foo () function, which is a pointer-to-a-pointer. Means that, it will hold the address of a pointer variable itself. You'll have to call it as: 'foo (&a) ;' and make the function definition like:

void foo ( int ** p )
{
* p = new int[10] ;

for ( int i = 0 ; i < 10 ; i ++ )
(*p)[i] = i ; // end for

} // end of foo ()

// Notice the parenthesis around *p. It is necessary otherwise *p[i] will be interpreted as *(p[i]) which is not what we want.

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…

The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.