functions with pointers

Hi guys I'm reading a c++ book at the moment and I noticed that in one of the examples the return type is a pointer now what really confuses me is why would you want to return a memory address in the first place I just can't see where or why this would be useful? could someone try explain to me why and where you would use a pointer as a return type with an example would help even more,

thanks in advance


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>

using namespace std;

int *pint(int* number){

   return number;

}

int main()
{
    int num = 55;
    cout << pint(&num);

}
For example, to allocate memory and make copy of the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int	*make_copy_of_array(int nD[],int qn)
{
  int	*nDI;
  int	jn;

  nDI=new int[qn];
  for(jn=0;jn<qn;jn++)
    nDI[jn]=nD[jn];

  return nDI;
}
void	main()
{
  int	nD[]={0,1,2,3,4},*nD2;
  nD2=make_copy_of_array(nD,5);
  delete[] nD2;
}
thanks skaa for the reply,why would you have to use a pointer to make a copy of an array?
Because I need an address in memory of the new array. This address can be returned in return operator like in my previous example, or as a parameter value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void	make_copy_of_array(int nD[],int qn,int *nDI)
{
  int	jn;

  nDI=new int[qn];
  for(jn=0;jn<qn;jn++)
    nDI[jn]=nD[jn];
}
void	t_1()
{
  int	nD[]={0,1,2,3,4},*nD2;
  make_copy_of_array(nD,5,nD2);
  delete[] nD2;
}
Topic archived. No new replies allowed.