+ 1

why char *s="hello" is able to save more then one correctors?

#include <stdio.h> #include <stdlib.h> struct stack { int size; int top; char *arr; }; int isEmpty(struct stack *ptr) { if (ptr->top == -1) { return 1; } else { return 0; } } int isFull(struct stack *ptr) { if (ptr->top == ptr->size - 1) ///*** IMP ***/// { return 1; } else { return 0; } } void push(struct stack* ptr, char val){ if(isFull(ptr)){ printf("Stack Overflow! Cannot push %d to the stack\n", val); } else{ ptr->top++; ptr->arr[ptr->top] = val; } } char pop(struct stack* ptr){ if(isEmpty(ptr)){ printf("Stack Underflow! Cannot pop from the stack\n"); return -1; } else{ char val = ptr->arr[ptr->top]; ptr->top--; return val; } } int parenthesisMatch(char * exp){ // Create and initialize the stack struct stack* sp=(struct stack*)malloc(sizeof(struct stack)); sp->size = 100; sp->top = -1; sp->arr = (char *)malloc(sp->size * sizeof(char)); for (int i = 0; exp[i]!='\0'; i++) { if(exp[i]=='(') { push(sp, '('); } else if(exp[i]==')') { if(isEmpty(sp)) { return 0; } pop(sp); } } if(isEmpty(sp)) { return 1; } else { return 0; } } int main() { char * exp = "((8)(*--$9))"; // Check if stack is empty if(parenthesisMatch(exp)){ printf("The parenthesis is matching"); } else{ printf("The parenthesis is not matching"); } return 0; }

18th Dec 2020, 9:54 AM
Dershil Jadav
Dershil Jadav - avatar
1 Odpowiedź
+ 2
It not. It's not even saving one character, insteat it is a pointer variable holding address of first character of world "hello" that is 'h' and as arrays are stored continuously in memory so rest all can be accessed easily by moving the pointer accordingly
18th Dec 2020, 9:57 AM
Arsenic
Arsenic - avatar