3

I'm writing a postfix calculator using a generic stack library I wrote. I will preface this by saying that it is a homework assignment, and I normally wouldn't seek help online, but the tutoring center / help room is closed today.

I have a function "calc" that takes character inputs from stdin, tokenizes it, and determines if it is a number, +, -, *, or ^. The tokenizer works, as I have tested it before with all cases.

There seem to be issues when I implement the stack library.

This is the code in question:

char num[MAX_NUM];
float n;
while (1) {
            switch (token = get_tok(num, sizeof(num))) {
            case TOK_NUM:
                    //printf("TOK_NUM: %s (%lf)\n", num, strtod(num, NULL));
                    n = strtod(num, NULL);
                    push(stk, (void *)n);        
                    printf("TOK_NUM_STK: %lf\n", (float)peek(stk));
                    pop(stk);
                    break;

There are other switch statements to deal with the other characters (+, -, *, and ^) but I haven't moved on to them yet.

The idea is to convert the character array num to a floating point.

The push function is a part of my stack library. This is the code for that:

struct stack_t {
    int count;
    struct node_t {
            void *data;
            struct node_t *next;
    } *head;
};

void push(stack stk, void *data) {

    struct node_t *tmp = malloc(sizeof(struct node_t));

    tmp -> data = data;

    tmp -> next = stk -> head;

    stk -> head = tmp;

    (stk -> count)++;
}

The stack library works as I expect it to, as I have used it in other programs before, so I'm not worried about that.

My problem is that when I compile my postfix calculator, I get the error "cannot convert to a pointer type" and it references this line:

push(stk, (void *)n); 

All I'm trying to do right now is take the user's input from stdin, push it onto the stack, read it from the stack, and then pop it off the stack. I'm not sure why I'm getting this error right now, and I'm not sure how to fix it. Any help or tips to move me in the right direction would be greatly appreciated.

  • What is the type of `n`? You may have to allocate buffer to store data in `n`, copy the data to the buffer and pass the pointer to the buffer instead of passing `n` directly. – MikeCAT May 02 '16 at 00:56
  • Apologies. I forgot to include that line. This line appears before the `while(1)` line. `float n;` –  May 02 '16 at 00:59
  • Please don't put spaces around the dot (`.`) or arrow (`->`) operators. They bind very tightly indeed; there should not be a space on either size of them in orthodox C. Yes, the compiler doesn't mind. But people reading your code will, in general, mind. – Jonathan Leffler May 02 '16 at 03:23
  • We're given a styleguide at the beginning of the semester that indicates how our code will be graded... for some reason it had arrows written in that fashion. When I write code for myself, I don't use spaces, but for class I do. –  May 05 '16 at 22:45

1 Answers1

3

float and double are not compatible with integer types, you cannot convert them to pointer, what you want is either use float on the stack, or a pointer to the floats:

float *f = malloc(sizeof *f);
*f = n;
push(stk, f);

To use the elements on the stack, convert it back:

float *f = pop(stk);  // I assume your pop function returns a void *
float n = *f;  // n is the number your pushed
fluter
  • 13,238
  • 8
  • 62
  • 100
  • I will definitely give this a try, and thank you for your answer... my question now is, how can I do arithmetic with what is on the stack? Will I have to cast it back to a float before performing any operations? Or will I have to deference it? –  May 02 '16 at 01:10
  • @GarrettMassey yes, you just pop and dereference it to get the original number, see my updated post. – fluter May 02 '16 at 01:13
  • This worked. Thank you. I had to change some variables around so as to not conflict with variables I was using elsewhere, but the problem was solved. Thank you so much! –  May 02 '16 at 01:21
  • Glad to know it worked – fluter May 02 '16 at 01:31
  • Now I just have to make sure I don't mess up on the calculation cases... –  May 02 '16 at 02:02
  • quick question... I'm working on the multiplication operator, and I'm getting another error I don't understand. "pointer value used where a floating point value was expected" for this line: `printf("answer = %f\n", ((float)pop(stk) * (float)pop(stk)));` –  May 02 '16 at 02:31
  • `pop(stk)` returns a `void*`, right? so to get the actual float number, do `*(float*)pop(stk)`, it first convert the `void*` to `float*`, then dereference it. – fluter May 02 '16 at 02:34
  • That solved the compiler error... but it seems to be doing something wrong because every answer I get is 0.0000. I've run several tests to see what the values of each element in the stack is and I keep, for some reason, getting back 0, despite the fact that I'm pushing other numbers on the stack earlier. –  May 02 '16 at 02:38
  • make sure you are multiplying the numbers, not the pointers? – fluter May 02 '16 at 02:42
  • it seems to be something else... tried some code and it's telling me that all values in my stack are 0. I'll work on figuring that out. Thanks for your help. –  May 02 '16 at 02:53