By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,475 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,475 IT Pros & Developers. It's quick & easy.

Dynamic memory allocation

P: 26
ifstream& operator>>(ifstream & input , Poly & a)
{


char *pol;
pol=new char[];
if(input==cin)
cout<<" Please Enter the Polynomial in a manner +1x^2-1x^1+10x^0 "<<"\n";
input>>pol;///////inputing a polynomial as a string
a.setPoly(pol);


return input;
}



Poly& Poly::setPoly(char *pol)////////////pointer to a pointer pol of string
{
int size=strlen(*pol);
cout<<*pol;

double *temp1=new double[degree+1],*temp=new double[degree+1],*deg=new double[degree+1];
long i=0,j=0,k=0,count=1,count1=0,count2=0,count3=0,cnt =1,power=0,pow=0,p=1,pp=1;

for( i=0 ; i<size ; i++)
{
if((int)pol[i]==43)
{
count1=0;
..............continue......

i want to ask that how should i pass a dynamically created string in a setPoly...........through pointer to a pointer or simple pointer or array ????
Nov 18 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
It doesn't matter. Theya re both passed the same way.

When you pass an array to a function, all you pass is the address of element 0.

Expand|Select|Wrap|Line Numbers
  1. int array[10];
  2. int* darray = new int[10];
  3.  
darray is a pointer to an int. Specifically, it is the address of darray[0].

Likewise, array is the address of array[0].

Both darray[0] and array[0] are int. So darray and array are addresses of int. So array and darray are int*.

You pass these arrays to a function that has an int* argument. You will also need a second argument for the number of element in the array. Again, this is due to only the address of the array being passed. Unless you have a number of elements argument, there is no way the function can determine the number of elements.

Read this: http://bytes.com/forum/thread772412.html.
Nov 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.