Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi I am trying to send an pointer to a function as following, but there is a problem with my code, can anybody help please:

void fun(int* p1, int* p2, int&s)
{
int size;
cin>>size;
s=size;
p1 = new int[size];
p2 = new int[size];
for(int i =0 ; i<size; ++i)
{
p1[i] = i;
p2[i] = i * i;
}
}
int main()
{
int* p1;
int* p2;
fun(p1,p2,size);
for(int i =0; i< size;++i)
cout<<p1[i]<<" "<<p2[i];
}
Posted
Comments
Richard MacCutchan 11-Jul-11 5:11am    
I'm not sure why this question was down voted so I have 5'ed it to compensate. This is a perfectly reasonable question from someone who is trying to learn features of C++.

You should also realise that on return from fun you will be back to your original p1 and p2 which are uninitialised. To achieve what you are trying to do you should send p1 and p2 as references, or add another level of indirection thus:
void fun(int*& p1, int*& p2, int&s)
{
int size;
cin>>size;
s=size; // oops - restored this line
p1 = new int[size];
p2 = new int[size];

... // or

void fun(int** p1, int** p2, int&s)
{
int size;
cin>>size
s=size;
*p1 = new int[size];
*p2 = new int[size];
...
 
Share this answer
 
v2
Comments
Espen Harlinn 10-Jul-11 14:57pm    
Good points, my 5
Sergey Alexandrovich Kryukov 10-Jul-11 17:35pm    
Yes, a 5.
--SA
Richard MacCutchan 11-Jul-11 4:05am    
Thanks (and to Espen), but you missed my mistake which I have now corrected.
CPallini 11-Jul-11 4:26am    
Simply the best.
The second form would be possibly: void fun(int ** pp1, int ** pp2, int * ps)
Richard MacCutchan 11-Jul-11 4:35am    
Correct, I missed that part.
consider using this declaration:

oid fun(int* &p1, int* &p2, int&s)

because you need to derefernce the pointers
 
Share this answer
 
Comments
Richard MacCutchan 11-Jul-11 11:12am    
Another good suggestion, have a 5.
you didn't declare variable size in main() and variable s in fun()
C#
void fun(int* p1, int* p2, int&s)
{
   int size;
   cin>>s;     // replace size by s

   //s=size;         //comment or remove this line

   p1 = new int[size];
   p2 = new int[size];
   for(int i =0 ; i<size; ++i)
   {
      p1[i] = i;
      p2[i] = i * i;
   }
}

int main()
{

   int size = 0;             // Add this line 

   int* p1;
   int* p2;
   fun(p1,p2,size);
   for(int i =0; i< size;++i)
   cout<<p1[i]<<" "<<p2[i];
}


Copy and compile.
 
Share this answer
 
v6
Comments
CPallini 11-Jul-11 4:28am    
size looks uninitialised to me.
Ashish Tyagi 40 11-Jul-11 4:32am    
I am really sorry, Sir, let me modify it. And thinks for pointing me out..
Richard MacCutchan 11-Jul-11 4:36am    
See also my answer, particularly my comments about p1 and p2. I guess we fixed half each! :)
Ashish Tyagi 40 11-Jul-11 4:48am    
Ha ha ha you are right sir. +5 from me.. Ha ha ha :-)
You cant change the pointers p1, p2 to point to Heap as so, however, in the main function you have to intialize the size variable (need declare before using) before send it to the fun function. I think this help for you.
 
Share this answer
 
v2
size variable's scope is in function func only. In fact you are suppose to get a compilation error.
you need to declare size in main function too
 
Share this answer
 
In addition to the above I'd like to add that the name you chose for your function could be misleading - it may not be funny at all! ;p
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900