0

I have some doubt regarding this program,Here we are creating integer to array and storing char inside that

THINK IN CASE WHEN WE WRITE THE INFIX TO POSTFIX PROGRAM, SAME STACK OPERATION FUNCTION WILL BE USED THERE

And we will store parenthesis and operand in the stack, i want to ask whether the ASCII value stored in the stack->array And why is there no need of typecasting And since integer require 4 bytes of memory then how 1 byte char are stored in that array not only storing but how we can access all char variables using that integer array because according to pointer arithmatic *(array+i)will be 4 byte ahead if array is pointer to integer

#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

// Stack type
struct Stack
{
    int top;
    unsigned capacity;
    int* array;
};

// Stack Operations
struct Stack* createStack( unsigned capacity )
{
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

    if (!stack) return NULL;

    stack->top = -1;
    stack->capacity = capacity;
    stack->array = (int*) malloc(stack->capacity * sizeof(int));

    if (!stack->array) return NULL;

    return stack;
}

int isEmpty(struct Stack* stack)
{
     return stack->top == -1 ;
}

char peek(struct Stack* stack)
{
    return stack->array[stack->top];
}

char pop(struct Stack* stack)
{
    if (!isEmpty(stack))
        return stack->array[stack->top--] ;
    return '$';
}

void push(struct Stack* stack, char op)
{
    stack->array[++stack->top] = op;
}


// The main function that returns value of a given postfix expression
int evaluatePostfix(char* exp)
{
    // Create a stack of capacity equal to expression size
    struct Stack* stack = createStack(strlen(exp));
    int i;

    // See if stack was created successfully
    if (!stack) return -1;

    // Scan all characters one by one
    for (i = 0; exp[i]; ++i)
    {
        // If the scanned character is an operand (number here),
        // push it to the stack.
        if (isdigit(exp[i]))
            push(stack, exp[i] - '0');

        // If the scanned character is an operator, pop two
        // elements from stack apply the operator
        else
        {
            int val1 = pop(stack);
            int val2 = pop(stack);
            switch (exp[i])
            {
                case '+': push(stack, val2 + val1); break;
                case '-': push(stack, val2 - val1); break;
                case '*': push(stack, val2 * val1); break;
                case '/': push(stack, val2/val1); break;
            }
        }
    }
    return pop(stack);
}

// Driver program to test above functions
int main()
{
    char exp[] = "231*+9-";
    printf ("Value of %s is %d", exp, evaluatePostfix(exp));
    return 0;
}
hellow
  • 12,430
  • 7
  • 56
  • 79
david12345
  • 32
  • 10
  • Please write `struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));` as `struct Stack *stack = malloc(sizeof *stack);`, it's shorter and more DRY. As to your question, characters in C are small integers, which is why there's no problem in storing `char` values in `int` variables, they're both integers and you can be sure any `char` fits in an `int` (there's some complexity regarding signedness, but let's ignore that). – unwind Sep 04 '18 at 08:52
  • a `char` is just a number, the only special thing about it is that often it is interpreted as a character. `sizeof(int) > sizeof(char)`, so there there is no problem with assigning a `char` to an `int` – 463035818_is_not_an_ai Sep 04 '18 at 08:53
  • There is no need to cast a void* to any other poitner while assigning. Only in C++ you need to do that (but there you don't use malloc ofc ;) ) – hellow Sep 04 '18 at 08:53
  • [Don't cast the result of `malloc` in C](https://stackoverflow.com/q/605845/995714) – phuclv Sep 04 '18 at 10:23

1 Answers1

1

char is the smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned. It contains CHAR_BIT bits.

It size is almost always less than the size of an int. So a char can be easily stored in an int.

how we can access all char variables using that integer array because according to pointer arithmatic *(array+i)will be 4 byte ahead if array is pointer to integer

This is possible because when you are storing a char into an int, you are storing them at intervals the size of int. Since top is an int, the pointer arithmetic on it (++) will increase its address value by the size of an int.

stack->array[++stack->top] = op;

And that's what happens when you are retrieving the char also. The pointer arithmetic on top(--) will decrease its address value by the size of an int.

return stack->array[stack->top--] ;

So it works without any problem.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • When I am printing `sizeof('b')` it is printing 4 which is the size of int but it should print 1 because i am writing b in quotes 'b' and i am assuming that is a char – david12345 Sep 04 '18 at 12:13
  • In C, the type of a character constant like 'a' is actually an `int`, with size of 4 (or some other implementation-dependent value). If you print `sizeof(char)`, you will get `1`. – P.W Sep 04 '18 at 12:20
  • Does this answer your question? – P.W Sep 10 '18 at 05:03