Chang-Gyun Nam
Chang-Gyun Nam

Reputation: 23

Comparison between pointers and integers in C

What I'd like to program is having a user input a series of parentheses/braces and evaluate them whether they are nested properly or not.

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define STACK_SIZE 100

char contents[STACK_SIZE];
int top = 0;

void make_empty(void)
{
    top = 0;
}

    bool is_empty(void)
{
    return top == 0;
}

bool is_full(void)
{
    return top == STACK_SIZE;
}

void push(char i)
{
    if(is_full())
        stack_overflow();
    else
        contents[top++] = i;
}

char pop(void)
{
    if(is_empty())
        stack_underflow();
    else
        return contents[--top]; 
}

void stack_overflow(void)
{
  printf("Stack overflow\n");
  exit(EXIT_FAILURE);
}

void stack_underflow(void)
{
  printf("Stack underflow\n");
  exit(EXIT_FAILURE);
}

int main(void)
{
    char ch;
    printf("Enter parentheses and/or braces: ");
    while((ch = getchar()) != '\n')
    {   
        if(ch == '(' || ch == '{')
            push(ch);
        else if(ch == ')')
            {
                if(pop != '(') /// compiler says it is a comparison between pointer and integer.
                {
                    printf("not nested properly!");
                    exit(EXIT_FAILURE);
                }
            }
        else if(ch == '}'){
            if(pop != '{')  /// compiler says it is a comparison between pointer and integer.
            {
                printf("not nested properly!");
                exit(EXIT_FAILURE);
            }
        }
    }
    if(is_empty())
        printf("Parentheses/braces are nested properly");
    else
        printf("not nested properly!!");
    /* if the stack is empty, it is nested properly, otherwise not.*/

    return 0;
}

the compiler says that the comparison between pop and '(' or '{' is comparison between pointer and integer, although I set the return type of the function 'pop' as int. Because of this when the program processed with right parenthesis or brace it always prints "not nested properly." How can I improve this?

Upvotes: 2

Views: 72

Answers (1)

Yunnosch
Yunnosch

Reputation: 26753

This is just "mentioning" the function, not calling it.
The compiler sees a function pointer instead of the return value and its type.

pop != '{'

Use

pop() != '{'

in order to call the function and do the comparison of return value of type char and '{'.

Upvotes: 4

Related Questions