tags:

views:

170

answers:

3
char *str = NULL;
str = Operation(argv[1]);
cout << Cal(str);

Operation function return a pointer, but I really don't know why str in Cal is null. in line 2, it still has content. What am I missing?

+1  A: 

str is a pointer to a pointer. As you say, Operation returns a pointer (to what?). You ought to get a diagnostic (type mismatch).

dirkgently
but still its not possible to store void* in char** ?
Xinus
No. You are correct.
dirkgently
A: 
char* Operation(char* name)
{
    fstream file(name,ios::in);
    char c;
    char stack[256]; int count_stack = -1; 
    char result[256]; int count_result = -1;
    while ( !file.eof() )
    {
     c = file.get();
     if ( c=='(')
     {
      count_stack++;
      stack[count_stack] = c ;
     }
     else if ( isOperand(c) )
     {
      count_result++;
      result[count_result] = c;
     }
     else if ( isOperator(c) )
      if ( isOperator( (stack[count_stack]) )>0 )
       if ( isOperator(c) > isOperator( stack[count_stack] ) )// dua ra so sanh khi trong stack co n>=1 ptu
       {
        count_result++;
        result[count_result] = c;
       }
       else
       {
        count_result++;
        result[count_result] = (stack[count_stack]);
        stack[count_stack]= c;
       }
      else
      {
       count_stack++;
       stack[count_stack] = c ;
      }
     else if ( c==')') // Neu gap dau ngoac dong se lay het phan tu ra den khi gap ngoac mo
     {
      while( stack[count_stack] != '(')
      {
       count_result++; 
       result[count_result] = stack[count_stack];
       count_stack--;
      }
      count_stack--;
     }
    }
    while ( count_stack >=0 )
    {
     count_result++; 
     result[count_result] = stack[count_stack];
     count_stack--;
    }
    return &result[0];
}

This is the Operation Function . I'm really bad in pointer :P

nXqd
Editing your question would be much preferred to answers that are not answers. (Can new users edit their own questions?)
Justin Love
Thanks , it's really my bad :|
nXqd
@Justin: Yes, they can edit their questions.
sth
+2  A: 

Well, firstly, in your Operation function you're returning a pointer to a temporary stack array. In Operation you declare char result[256] as a temporary stack variable. The 256 bytes you allocate here are ONLY valid for the duration of the Operation function. So using the pointer to result after Operation returns will cause undefined behavior.

To fix this, you have a few options:

1) You can allocate result on the heap, (using the new operator)

2) Even better, instead of returning a char* pointer, simply return an std::string object so you don't need to worry about allocating/deallocating memory.

3) Finally, if your program is single-threaded, you can also simply make char result[256] a static variable by declaring static char result[256]. This way, the pointer will be valid even after you return from Operation.

I would recommend option 2.

Charles Salvia
In Operation function, I return the address of the first element in result so I can use result array :|"you're returning a pointer to a temporary stack array" I don't see it . Can you point it out :D ( I'm noob )
nXqd
My teacher ask me to do math ( operand, operator ) with no container in stl . So I don't use string .Thanks so much . Stackoverflow is great :)
nXqd
Okay, well since you can't use a string, you can use option 3 (declare the array as static), as long as you are sure your program is not multi-threaded. (Static variables are not thread-safe)
Charles Salvia
Thanks salvia so much. It's solved . I don't know much about thread-safe . I'll research :)
nXqd