0

Infix to Prefix- First transforming reverse of infix to postfix and reversing the result to get prefix.

However the problem lies in reversing of the postfix expression. I suspect the problem lies with using pointers. I tried reversing using other way and it worked.

But I can't seem to understand why the problem is arising.

Output:

c*b+a //Reverse of given infix expression

cb*a+ //Postfix of the reversed infix expression

+a*a+ //Prefix: Problem

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Method declarations
....
*/
void main()
{
    char infix[] = "a+b*c";
    char* reverse = rev(exp);
    printf("%s\n", reverse);
    char* postfix = inToPost(reverse);
    printf("%s\n", postfix);
    char* prefix= rev(postfix);
    printf("%s", prefix);
}

char* rev(char* ptr1)
{
    char rev[strlen(ptr1)+1];
    char *temp = ptr1;
    int i =0;
    while(*ptr1!='\0')
    {
        ptr1++;
    }
    do
    {
        rev[i++] = *--ptr1;
    }while(ptr1!=temp);
    rev[i] = '\0';
    ptr1 = rev;
    return ptr1;
}
char* inToPost(char *ptr)
{
    char post[strlen(ptr)+1];
    int i =0;
    while(*ptr!='\0')
    {
        char ch = *ptr;
        if(isOperand(ch))
        {
            post[i++]=ch;
            //printf("%c",ch);
        }
        else if(isOperator(ch))
        {
            while(!isEmpty() && !isOpenP(peek()) && getPrec(peek())>=getPrec(ch))
            {
                post[i++]=peek();
                //printf("%c", peek());
                pop();
            }
            push(ch);
        }
        else if(isOpenP(ch))
        {
            push(ch);
        }
        else if(isCloseP(ch))
        {
            while(!isEmpty() && !isOpenP(peek()))
            {
                post[i++]=peek();
                //printf("%c", peek());
                pop();
            }
            pop();
        }
        ptr++;
    }
    while(!isEmpty())
    {
        post[i++]=peek();
        //printf("%c", peek());
        pop();
    }
    post[i] = '\0';
    ptr = post;
    return ptr;
}

/*
Method definitions
*/
  • 2
    It's not your immediate problem, but note that `void main()` is only acceptable on Windows (among widely used platforms). See [What should `main()` return in C and C++](https://stackoverflow.com/questions/204476/) for the sordid details. – Jonathan Leffler Jul 07 '18 at 18:25
  • 1
    terrible code, 3 times too long, this is Ctrc/CtrlV of few attempts? Error in answer is not the only. Coments in code suggest OOP, tens of uneeded assigments ... UB ... – Jacek Cz Jul 07 '18 at 18:55
  • what're you even trying to say?! I'm a beginner and I'm not trying to get a job or anything with this code, to write it neat and professional. It's pretty understandable if you have solved with pointers before. Don't be rude for no reason at all. – Priyanker Rao Jul 07 '18 at 19:03
  • 1
    suggestion to beginer. Make work in short clear steps. Do & test reversing function (few lines), AND NOTHING MORE, if this will be good tested go next. Impossible in one big attemp Building next layers on unchecked (de facto erroneous) code is way to nothing – Jacek Cz Jul 07 '18 at 19:10

1 Answers1

5

You make a capital mistake in your rev function, one that leads to undefined behavior: You return a pointer to a local variable.

The pointer you return from the rev function points to the first element of the rev array. This array will go out of scope and cease to exist once the function ends. All pointers to any element in it will become invalid.

Either pass in an array (or rather a pointer) to the function as arguments, or use dynamic allocation for the array.


On an unrelated note, try to avoid using the same name for the function for local variables in it. It makes the code harder to read and maintain.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Not just in `rev`, but also in `inToPost` (`ptr = post; return ptr;` where `post` is on the local stack). – cdarke Jul 07 '18 at 18:25
  • 1
    You can also reverse the array in situ without using a local array at all, or you could copy the reversed array in the local variable to the input array. I'd go with 'reverse in situ'. – Jonathan Leffler Jul 07 '18 at 18:26
  • @Someprogrammerdude: understandable! – cdarke Jul 07 '18 at 18:30
  • > Either pass in an array (or rather a pointer) I'm sorry. I didn't get this. Isn't that what I did? – Priyanker Rao Jul 07 '18 at 18:31
  • @PriyankerRao To the *destination* array. – Some programmer dude Jul 07 '18 at 18:38
  • how do i do that?! The only way I can think of using pointers to go to the end of the string and traverse to beginning is this. If i make changes to the original array, it'll stop midway while traversing with a temporary pointer I used to traverse from the end. – Priyanker Rao Jul 07 '18 at 18:53
  • many more problems in this code, in reality impossible to smooth improvements. Is really detailed analyse needed here? – Jacek Cz Jul 07 '18 at 18:57
  • Keep it simple then, use `malloc` or `calloc` to allocate memory on the *heap*, that does not become invalid on return like the local arrays (but you must remember to `free` them). – cdarke Jul 07 '18 at 19:23