Dynamic memory allocation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitbyte89
    New Member
    • Nov 2008
    • 26

    Dynamic memory allocation

    ifstream& operator>>(ifst ream & 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(c har *pol)////////////pointer to a pointer pol of string
    {
    int size=strlen(*po l);
    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,cou nt=1,count1=0,c ount2=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;
    ..............c ontinue......

    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 ????
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Code:
    int array[10];
    int* darray = new int[10];
    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.

    Comment

    Working...