How to fix Segmentation Fault while trying to do RPN in C - c

I am trying to make a program that will use RPN, which will calculate a series of integers. I have some standard functions (pop, push) that I can't change. But when I run the below code, I get a segmentation fault.
Since I can't change the inside core of those two functions, so I tried to change the way that I am calling them (I made stack[] a dynamic array and the top an int pointer) but nothing changes.
#include <stdio.h>
#include <stdlib.h>
#define N 1000
void push(int stack[],int *t,int obj)
{
if((*t)==(N-1))
{
printf("Stack overflow...\n");
getchar(); //getc
abort();
}
else
stack[++(*t)]=obj;
}
int pop(int stack[],int *t)
{
int r;
if((*t<0))
{
printf("Stack empty...\n");
printf("Error in expresion.\n");
getchar(); //getc
abort();
}
else
r=stack[(*t)--];
return(r);
}
int isdigit(char in)
{
int flag;
if (in>=48 && in<=57) //if its terminus
flag=1;
else if(in==42 || in==43 || in==45 || in==47) //if its operator
flag=2;
else
flag=0;
return flag;
}
int main(int argc, char** argv) {
int *stack=(int*) malloc(N*sizeof(int));
char input;
int i=0,*top=0,flag;
int num1,num2;
float result;
if (stack == NULL) {
printf("Out of memory!\n");
return (1);
}
printf("Give the Formula: ");
while(input=getchar())
{
flag=isdigit(input);
if(flag==1) //if its terminus
{
push(&stack[i],top,input);
}
if(flag==2) //if its enforcer
{
num1=pop(&stack[i],top);
num2=pop(&stack[i+1],top);
if (input==42) //case of +
result=num1+num2;
if (input==43) //case of *
result=num1*num2;
if (input==45) //case of -
result=num2-num1;
if (input==47) //case of /
if(num2!=0)
result=num2/num1;
else
printf("Can't do the operation");
push(&stack[i],top,result);
}
if(flag==0) //case of everything else
{
printf("Error");
exit(1);
}
printf("Operand: %c\n",input);
// for(int j=0;j<strlen(stack);j++)
// printf("stack[%d]=%d\n",i,stack[j]);
//printf current status of stack
i++;
}
return (EXIT_SUCCESS);
}
If you have a series of integers like 234+*, the stack should be firstly 2,3,4,+,*, secondly 5,4,*, thirdly 20.

In main you defined top as pointer and initialized it to point to null.
After that, in push, you try to dereference the null location via *t. Here you get segmentation fault.

Related

Error 'is_empty' was not declared in this scope.why does it give error?

output=[Error] 'is_empty' was not declared in this scope
must be:
Example
Input string : abbccbaabccbba message will be The string is valid
aaabbcbbcbaab message will be The string is invalid
aadbxcy*ycxbdaa message will be Wrong character!!!
what should i do?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct
{
char home[35];
int top;
} My_stack;
void push(My_stack * s, char c) // push (insert) operation
{ // assume there is enough space for pushing next element!
s -> top ++;
s -> home[s -> top] = c;
}
int pop(My_stack * s) // pop (remove) operation
{
if(is_empty (*s)) {
printf("ERROR: Nothing to pop - program terminates\n");
exit(1);
}
return (s ->home[s ->top --]);
}
int is_empty(My_stack * s) // checking whether stack is empty or not
{
return(s -> top < 0 ? 1 : 0);
}
int main(){
char ch[25];
int i,l;
My_stack stack;
printf("give the string");
scanf("%s",ch);
l=strlen(ch);
i=0;
while(ch[i]!='\0') {
if(ch[i]!='A'&&ch[i]!='B'&& ch[i]!='*') {
printf("the string is not accepted allowed caracters are A,B and * ");
exit(0);
}
i++;
}
i=0;
while(ch[i] != '*') {
push(&stack, ch[i]);
i++;
}
i++; // one step forward to pass '*
while(ch[i] != '\0') {
if(ch[i] != pop(&stack)) {
printf("the string is not valid");
exit(0);
}
i++;
}
printf("the string is valid");
return 0;
}
First of all, you need to pass the pointer to the function, not the actual value, so your functioncall must be changed to:
is_empty (s)
instead of
is_empty (*s)
Then, the reason is because you define your function is_empty after your first use. To prevent this error, you should declare your function first. This tells the compiler "hey, there exists this function with this signature, the definition is given later though" so then you can use it in your program before having implemented it explicitly.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct
{
char home[35];
int top;
} My_stack;
int is_empty(My_stack * s); // <----- DECLARATION OF FUNCTION
int pop(My_stack * s) // pop (remove) operation
{
if(is_empty (s)) {
printf("ERROR: Nothing to pop - program terminates\n");
exit(1);
}
return (s ->home[s ->top --]);
}
int is_empty(My_stack * s) // checking whether stack is empty or not
{
return(s -> top < 0 ? 1 : 0);
}
Or, you put the entire definition before your first use so then there's no need for a declaration:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct
{
char home[35];
int top;
} My_stack;
int is_empty(My_stack * s) // checking whether stack is empty or not
{
return(s -> top < 0 ? 1 : 0);
}
int pop(My_stack * s) // pop (remove) operation
{
if(is_empty (s)) {
printf("ERROR: Nothing to pop - program terminates\n");
exit(1);
}
return (s ->home[s ->top --]);
}

Segmentation Error in C while trying to input

I am trying to make a C program to take in a list of movies and add to it with memory alloction and able to retrieve movies from the list as well using a txt file.
movies.txt
5
Mission Impossible
Action
4
2008
Up
Action
3
2012
I keep running into an error after running in the command line and when the menu comes up, whenever I input something it runs a seg fault. I don't have access to a debugger right now and I'm not exactly sure what's wrong although I assume its a problem with my pointers or memory alloc.
Can someone point me in the right direction?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// limit definition
#define LIMIT 999
//movie structure
struct movie
{
char name[100];
char type[30];
int rating;
int releaseDate;
};
//reads file
void readFile(FILE *fp,struct movie* movieList[],int *noOfReviews)
{
char buffer[100];
int counter = 0;
struct movie* newNode;
fgets(buffer,LIMIT,fp);
*noOfReviews = atoi(buffer); // number of reviews in buffer
printf("%d",*noOfReviews); //prints reviews
while((fgets(buffer,LIMIT,fp)!=NULL) || (*noOfReviews > 0)) //if null or reviews greater than zero
{
if(counter % 4 == 0)
{
struct movie* tmpNode = (struct movie*)malloc(sizeof(struct movie)); //allocates memory
movieList[counter] = tmpNode;
newNode = tmpNode;
*noOfReviews--; // --#ofreviews
}
//copys struc into buffer
switch(counter % 4 )
{
case 0:
strcpy(newNode->name,buffer);
break;
case 1:
strcpy(newNode->type,buffer);
break;
case 2:
newNode->rating = atoi(buffer);
break;
case 3:
newNode->releaseDate = atoi(buffer);
break;
default:
printf("Exception\n");
break;
}
counter++;
}
}
//searches list
int searchList(struct movie* movielist[],char movieName[],int noOfMovies)
{
int counter = 0;
while(noOfMovies--)
{
if(strcmp(movielist[counter]->name,movieName) == 0) // if string compares to name
{
return counter;
}
counter++;
}
return -1;
}
//compares strings of name
int nameStrCmp(const void *a, const void *b)
{
return (strcmp(((struct movie*)a)->name,((struct movie*)b)->name));
}
// compares rating strings
int ratingStrCmp(const void * a, const void * b)
{
return (((struct movie*)a)->rating - ((struct movie*)b)->rating);
}
//displays the structure
void display(struct movie* movieList[],int n)
{
int i;
struct movie* searchRslt;
for(i = 0; i < n; i++)
{
searchRslt = movieList[i];// search result index of movies list
//prints struct information
printf("name:%s\n type:%s\n rating:%d\n releaseDate:%d\n",searchRslt->name,searchRslt->type,searchRslt->rating,searchRslt->releaseDate);
}
}
//main function
int main(int argc, char *argv[])
{
char buffer[100];
int noOfReviews;
struct movie* movieList[1000];
struct movie *searchRslt;
char mName[100];
if(argc <= 1)
{
printf("invalid");
return 0;
}
FILE *fp = fopen(argv[1],"r");
readFile(fp,movieList,&noOfReviews);
while(1)
{
//case selection menu
int input;
printf("Enter 1 to search for a movie.\n");
printf("Enter 2 to display the list of movies by name.\n");
printf("Enter 3 to display the list of movies by rating.\n");
scanf("%d",&input);
switch(input)
{
case 1:
printf("Enter movie name to search:");
scanf("%s",mName);
int index = searchList(movieList,mName,noOfReviews);
if(index < 0)
printf("Not found!!\n"); // if movie not found
else // gets movies
{
searchRslt = movieList[index];
printf("name:%s\n type:%s\n rating:%d\n releaseDate:%d\n",searchRslt->name,searchRslt->type,searchRslt->rating,searchRslt->releaseDate);
}
break;
case 2:
qsort(movieList,noOfReviews,sizeof(struct movie),nameStrCmp);
display(movieList,noOfReviews);
break;
case 3:
qsort(movieList,noOfReviews,sizeof(struct movie),ratingStrCmp);
display(movieList,noOfReviews);
break;
default:
break;
}
}
}
A few formatting/readability/coding suggestions: (Some are just suggestions, others actually eliminate warnings.)
format. (mainly indention)
replace 'struct movie' with MOVIE everywhere. (see struct definition below)
initialize variables before use.
pay attention to buffer size in declaration and usage (fgets errors)
Compile with warnings on. (eg, with gcc use -Wall)
The following is limited to addressing each of these items, including new struct definition. The following compiles and builds without warnings or errors. It does not include debugging your code. (If can do a Beyond Compare (its free), between your original post and the edited version below, you can easily find where the edits are.)
(At the time of this post, there was no information provided about your input file, so further efforts were not made.)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// limit definition
#define LIMIT 999
//movie structure
typedef struct {
char name[100];
char type[30];
int rating;
int releaseDate;
}MOVIE;
//reads file
void readFile(FILE *fp,MOVIE* movieList[],int *noOfReviews)
{
char buffer[LIMIT];
int counter = 0;
MOVIE* newNode = {0};
fgets(buffer,LIMIT,fp);
*noOfReviews = atoi(buffer); // number of reviews in buffer
printf("%d",*noOfReviews); //prints reviews
while((fgets(buffer,LIMIT,fp)!=NULL) || (*noOfReviews > 0)) //if null or reviews greater than zero
{
if(counter % 4 == 0)
{
MOVIE* tmpNode = (MOVIE *)malloc(sizeof(MOVIE)); //allocates memory
movieList[counter] = tmpNode;
newNode = tmpNode;
*noOfReviews--; // --#ofreviews
}
//copys struc into buffer
switch(counter % 4 ) {
case 0:
strcpy(newNode->name,buffer);
break;
case 1:
strcpy(newNode->type,buffer);
break;
case 2:
newNode->rating = atoi(buffer);
break;
case 3:
newNode->releaseDate = atoi(buffer);
break;
default:
printf("Exception\n");
break;
}
counter++;
}
}
//searches list
int searchList(MOVIE* movielist[],char movieName[],int noOfMovies)
{
int counter = 0;
while(noOfMovies--)
{
if(strcmp(movielist[counter]->name,movieName) == 0) // if string compares to name
{
return counter;
}
counter++;
}
return -1;
}
//compares strings of name
int nameStrCmp(const void *a, const void *b)
{
return (strcmp(((MOVIE*)a)->name,((MOVIE*)b)->name));
}
// compares rating strings
int ratingStrCmp(const void * a, const void * b)
{
return (((MOVIE*)a)->rating - ((MOVIE*)b)->rating);
}
//displays the structure
void display(MOVIE* movieList[],int n)
{
int i;
MOVIE* searchRslt;
for(i = 0; i < n; i++)
{
searchRslt = movieList[i];// search result index of movies list
//prints struct information
printf("name:%s\n type:%s\n rating:%d\n releaseDate:%d\n",searchRslt->name,searchRslt->type,searchRslt->rating,searchRslt->releaseDate);
}
}
//main function
int main(int argc, char *argv[])
{
int noOfReviews;
MOVIE* movieList[1000];
MOVIE *searchRslt;
char mName[100];
if(argc <= 1)
{
printf("invalid");
return 0;
}
FILE *fp = fopen(argv[1],"r");
readFile(fp,movieList,&noOfReviews);
while(1)
{
//case selection menu
int input;
printf("Enter 1 to search for a movie.\n");
printf("Enter 2 to display the list of movies by name.\n");
printf("Enter 3 to display the list of movies by rating.\n");
scanf("%d",&input);
switch(input)
{
case 1:
printf("Enter movie name to search:");
scanf("%s",mName);
int index = searchList(movieList,mName,noOfReviews);
if(index < 0)
printf("Not found!!\n"); // if movie not found
else // gets movies
{
searchRslt = movieList[index];
printf("name:%s\n type:%s\n rating:%d\n releaseDate:%d\n",searchRslt->name,searchRslt->type,searchRslt->rating,searchRslt->releaseDate);
}
break;
case 2:
qsort(movieList,noOfReviews,sizeof(MOVIE),nameStrCmp);
display(movieList,noOfReviews);
break;
case 3:
qsort(movieList,noOfReviews,sizeof(MOVIE),ratingStrCmp);
display(movieList,noOfReviews);
break;
default:
break;
}
}
}

Why queue using two stacks not functioning?

I'm trying to implement a queue using 2 stacks but my code isn't functioning.
Can you spot the error?
#include <stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct stack {
int top;
int arr[MAX];
} stack;
void enque(stack*s1, int ele) {
printf("entering");
push(&s1, ele);
printf("what a pain");
}
void push(stack*s, int ele) {
if (s->top == MAX - 1) {
printf("OVERFLOW");
} else {
s->arr[++s->top] = ele;
}
}
int deq(stack*s1, stack*s2) {
int x;
if (s1->top == -1 && s2->top == -1) {
printf("empty");
} else {
if (s2->top == -1) {
while (s1->top != -1) {
push(&s2, pop(&s1));
}
}
x = pop(&s2);
return x;
}
}
int pop(stack *s) {
if (s->top == -1) {
printf("UNDERFLOW");
} else {
return s->arr[s->top--];
}
}
void display(stack*s) {
printf("entered display");
int i;
for (i = 0; i <= s->top; i++) {
printf(" %d", s->arr[i]);
}
}
int main() {
int ch, ele, c;
stack s1, s2;
s1.top = -1, s2.top = -1;
do {
printf("1 - Enqueue2-deq3-display4-exit\n");
printf("Enter choice");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("enter ele of ur choice");
scanf("%d", &ele);
enque(&s1, ele);
break;
case 2:
c = deq(&s1, &s2);
printf("%d", c);
break;
case 3:
display(&s1);
break;
case 4:
exit(0);
default:
printf("Wrong choice");
}
} while (ch != 5);
}
Can you spot the error?
The compiler can spot the error faster than Stack-overflow.
OP is not compiling with all warnings enabled or is using a weak compiler
Enable all warnings to save time.
int deq(stack*s1, stack*s2) and int pop(stack *s) both have the same problem.
A common waning in such cases is warning: control reaches end of non-void function [-Wreturn-type]
Make certain each function path returns a value.
int pop(stack *s) {
if (s->top == -1) {
printf("UNDERFLOW"); // Notice, no return
} else {
return s->arr[s->top--];
}
}
Also push(&s1, ele); used before declaration. This make for conflicting function signature. Declare or define push() before using. Likely undefined behavior (UB) and this makes the code unreliable.
I recommend to print an '\n' more often.
// printf("entering");
printf("entering\n");
// or
puts("entering"); // \n automatically added.
You are passing pointer address in deque and enque functions.
push(&s2, pop(&s1)); --> push(s2, pop(s1));
x = pop(&s2); --> x = pop(s2);
push(&s1, ele);-->push(s1, ele);
since s1 and s2 are received as pointers to deque and enque functions
Also consider changes suggested by #Mr.Chux by considering compiler warnings you could have resolved all the issues by yourself.

Why is my Infix to postfix program giving the wrong output (C, Stacks)

Introduction:
I am coding an infix to postfix program in the C programming language using static char stack (using struct and char array).
My Methadology: I usually start out with working code and make is cleaner and smaller later on (when I know the algorithm works).
Done: I have only finished my basic rough code of the program and it's giving me the wrong output so I went over it a few times myself and showed it to my friend, who also couldn't quite figure it out.
Ask: It would really help me out and possibly make me a better programmer if someone could please give this a look and tell me why I am getting the wrong output and if possible, point me in the right direction.
Insight: The problem I am getting is that instead of adding a +, -, * or / after the alphanumeric part (like so A+B would be AB+), it gives random characters in their place (like so AB| or AB├ etc). These characters change on every execution of the program. I believe they are garbage values but am not sure how they are being used.
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define MAX 20
struct stackObj{
char stack[MAX];
int topOfStack;
};
void stackPush(struct stackObj *s, char value);
char stackPop(struct stackObj *s);
void stackPeekPrint(struct stackObj *s);
char stackPeek(struct stackObj *s);
int stackIsEmpty(struct stackObj *s);
int stackIsFull(struct stackObj *s);
int comparePrecedence(struct stackObj *s, char value);
int main(){
struct stackObj s;
s.topOfStack=0;
char result[MAX];
int resultFlag=0;
char exp[MAX];
gets(exp);
printf("\n\n");
printf("Expression:\t");
printf(exp);
printf("\n\n");
char _new='.';
char _old='.';
int i=0;
while (exp[i]!= '\0'){
_new = exp[i];
i++;
if (_new=='+' || _new=='-' || _new=='*' || _new=='/'){
if (comparePrecedence(&s,_new)==1){
stackPush(&s,_new);
} else if (comparePrecedence(&s,_new)==0){
_old = stackPop(&s);
result[resultFlag]=_old;
resultFlag++;
stackPush(&s,_new);
}
} else if (_new=='('){
stackPush(&s,_new);
}
else if (_new==')'){
_old = stackPop(&s);
while (_old != '('){
result[resultFlag]=_old;
resultFlag++;
_old = stackPop(&s);
}
stackPop(&s);
} else if (isalnum(_new) ) {
result[resultFlag]=_new;
resultFlag++;
}
}
while (s.topOfStack > 0) {
_old = stackPop(&s);
result[resultFlag]=_old;
resultFlag++;
}
printf("\n\n\n");
printf(result);
printf("\n\n\n");
return 0;
}
//----------------- Precedence Comparison CODE -----------------------------
int comparePrecedence(struct stackObj *s, char value){
//START - Variable Declarations and Definitions
int stackPrecedence = 0;
int operatorPrecedence = 0;
char stackChar = stackPeek(s);
char operChar = value;
//END - Variable Declarations and Definitions
//START - Setting Precedences
if (stackChar== '+'){ stackPrecedence=1; }
else if (stackChar== '-'){ stackPrecedence=1; }
else if (stackChar== '*'){ stackPrecedence=2; }
else if (stackChar== '/'){ stackPrecedence=2; }
if (operChar== '+'){ operatorPrecedence=1; }
else if (operChar== '-'){ operatorPrecedence=1; }
else if (operChar== '*'){ operatorPrecedence=2; }
else if (operChar== '/'){ operatorPrecedence=2; }
//END - Setting Precedences
//START - Comparing Precedences
if (operatorPrecedence > stackPrecedence) { return 1;
} else { return 0; }
}
//----------------- STACK CODE ------------------------------------------
void stackPush(struct stackObj *s, char value){
if ( (stackIsFull(s) != 1) && (s->topOfStack != MAX) ){
s->stack[s->topOfStack] = value;
s->topOfStack++;
} else {
printf("\nSorry, stack is full. No new data can be entered");
}
}
char stackPop(struct stackObj *s){
s->topOfStack = s->topOfStack-1;
return s->stack[s->topOfStack-1];
}
void stackPeekPrint(struct stackObj *s){
if (s->topOfStack != 0){
printf("\nTOS:\t%c", s->stack[(s->topOfStack)-1]);
} else {
printf("\nSorry stack is empty");
}
}
char stackPeek(struct stackObj *s){
if (s->topOfStack != 0){
return (s->stack[(s->topOfStack)-1]);
} else {
return '`';
}
}
int stackIsEmpty(struct stackObj *s){
if (s->topOfStack==0) {
return 1;
} else {
return 0;
}
}
int stackIsFull(struct stackObj *s){
if (s->topOfStack==MAX) {
return 1;
} else {
return 0;
}
}
In your stackPop() Function you are doing
s->topOfStack = s->topOfStack-1;
return s->stack[s->topOfStack-1];
It should be
s->topOfStack = s->topOfStack-1;
return s->stack[s->topOfStack];
you are actually returning s->stack[s->topOfStack-2].

Trying to implement an array based stack in C for an assignment

I am trying to implement an array based stack in C. My stack is supposed to have two parameters, top (the number of the top element in the array), and array (the array itself). My implementation follows.
typedef struct
{
char array[20];
int top;
}
stack;
stack mystack;
int Push(char,stack);
char Pop(stack);
char Top(stack);
int isFull(stack);
char input;
char save;
void main()
{
mystack.top = -1;
printf("Please input the characters you would like in your stack \n
while(input != '^')
{
Push( (scanf("%c",&input)) , mystack );
if (isFull(mystack) == 1)
printf("Your Stack is full, please input '^'\n");
}
char junk;
scanf("enter any character to continue %c",&junk);
while(mystack.top != -1)
{
printf("%c \n",Pop(mystack));
}
scanf("enter any character to terminate the program",&junk);
}
int Push(char charpush,stack stackpush)
{
if(stackpush.top >=20 )
return -1;
else
{
stackpush.array[stackpush.top + 1] = charpush;
stackpush.top = stackpush.top +1;
return 0;
}
}
char Pop(stack stackpop)
{
if (stackpop.top != -1)
{
save = stackpop.array[stackpop.top];
stackpop.top = stackpop.top-1;
return save;
}
}
char Top(stack stacktop)
{
if (stacktop.top != -1)
return stacktop.array[stacktop.top];
}
int isFull(stack stackisfull)
{
if (stackisfull.top = -1)
return 0;
else if (stackisfull.top >= 20)
return 1;
else return -1;
}
Currently my program accepts characters from the user, but the program automatically terminates when '^' is entered. It doesn't display the stack, and it doesn't do anything if characters come through the input and the stack is already full.
Please let me know if i need to be more specific or any further information is needed.
You've got a whole lot of problems to correct...
You have misunderstood scanf greatly
It doesn't return what it read
It doesn't accept a prompt
When reading from the terminal, it doesn't "see" anything until return is pressed
What does your method Pop() return when the stack is empty?
What does your method Top() return when the stack is empty?
Why did you write while(mystack.top != -1)? Would it make more sense to write while (!isEmpty(mystack)) and then write an isEmpty method?
You didn't initialize input - do yo know what is in it at the start?
Your indention "scheme" makes my head hurt. :)
In addition to the points #John Hascall has mentioned, C is a pass by value language. Meaning for every function call the arguments you provide are local in scope see this other stackoverflow post.
Having your global mystack variable (not the best practice either) will work but not how you are currently using it. By passing mystack to each function the changes are only visible on the argument used thereby defeating the purpose of having that global.
I've made the minor edits to indentation and logical errors but the main change was editing your functions to not take a "stack" argument and use your global:
#include <stdio.h>
typedef struct
{
char array[20];
int top;
} stack;
stack mystack; // your global
int Push(char); // remove "stack" arg for each stack-utility function
char Pop(void);
char Top(void);
int isFull(void);
char input;
char save;
// main as returning int and excepting argc/*argv[]
int main(int argc, char *argv[])
{
mystack.top = -1;
printf("Please input the characters you would like in your stack \n");
while(input != '^')
{
// by including scanf inside the function call return is passed
scanf("%c", &input);
Push( input );
if (isFull() == 1)
printf("Your Stack is full, please input '^'\n");
}
char junk;
// scanf will not print
printf("enter any character to continue\n");
scanf("%c",&junk);
while(mystack.top != -1)
{
printf("%c \n",Pop());
}
// same as last comment
printf("enter any character to terminate the program\n");
scanf("%c",&junk);
}
int Push(char charpush)
{
if(mystack.top >=20 )
return -1;
else
{
mystack.array[mystack.top + 1] = charpush;
mystack.top = mystack.top +1;
return 0;
}
}
char Pop(void)
{
if (mystack.top != -1)
{
save = mystack.array[mystack.top];
mystack.top = mystack.top-1;
return save;
}
// return has to match declaration type
return 0;
}
char Top(void)
{
if (mystack.top != -1)
return mystack.array[mystack.top];
// same as last comment
return 0;
}
int isFull(void)
{
// you were assigning not comparing
if (mystack.top == -1)
return 0;
else if (mystack.top >= 20)
return 1;
else return -1;
}

Resources