Bucky Ďroid Bucky Ďroid - 4 months ago 22
C Question

How to fix the below unknown type error from this simple c stack program?

I am not so much experienced with C language.

So I was trying to write a simple c program for stack but it's showing a bunch of error.

So may be i made something wrong in data elements.

Error Log:

stack.c:31:11: error: unknown type name ‘stack’
void push(stack[],top){
^
stack.c:31:19: error: unknown type name ‘top’
void push(stack[],top){
^
stack.c:45:6: warning: conflicting types for ‘pop’ [enabled by default]
void pop(stack,top){
^
stack.c:19:1: note: previous implicit declaration of ‘pop’ was here
pop();
^
stack.c:54:6: warning: conflicting types for ‘traverse’ [enabled by default]
void traverse(stack,top){
^
stack.c:22:1: note: previous implicit declaration of ‘traverse’ was here
traverse();
^
stack.c: In function ‘traverse’:
stack.c:62:20: error: subscripted value is neither array nor pointer nor vector
printf("%d\n",stack[i]);


Program:

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

void main(){
int stack[10];
int i;
int choice;
printf("Enter the elementz\n");
for ( i = 0; i < 10; i++){
scanf("%d",&stack[i]);
printf("++++++++ MENU ++++++++\n\n\n");
printf("Enter 1 to push \n Enter 2 to pop\n Enter 3 to display \n\n\n");
printf("Enter you choice \n\n\n");
scanf("%d",&choice);
switch(choice){
case 1 :
push();
break;
case 2:
pop();
break;
case 3 :
traverse();
break;
default:
printf("Enter the correct choice\n");


}
}
}

void push(stack[],top){
int item;
int max = 10;
printf("Enter the number you want to input\n");
scanf("%d",&item);
if(top == stack[max]-1){
printf("It's full\n");
}
else{
top = top+1;
stack[top] = item;

}
}

void pop(stack,top){

if(top == -1){
printf("STack is empty\n");
}
else{
top = top-1;
}
}

void traverse(stack,top){
int i;
if(top == -1){
printf("WHy r u giving m empty stack to print");
}
else{
for (i = 0; i <10; i++){
printf("Your stack is : \n");
printf("%d\n",stack[i]);
}
}
}


Thank you in advance.

Answer
  • You must write type names for each function argument. (the cause of error you posted)
  • You must declare or define functions to use before using them. (the cause of error you posted)
  • You must pass arguments that functions to use requires to functions. (the cause of error you posted)
  • Modifications to passed arguments won't affect caller's local variables, so you must use pointers to have callee modify them.
  • The condition to check if the stack is full is wrong.
  • The loop condition in traverse() looks strange.
  • Reading elements of the stack and doing stack manipulation based on chosen menus in the same loop looks strange because reading elements will corrupt the stack dealt with the menu.
  • You should use standard int main(void) in hosted environment instead of void main(), which is illegal in C89 and implementation-defined in C99 or later, unless you have some special reason to use non-standard signature.
  • You should format your code properly with indent.

Try this:

#include <stdio.h>
#include <stdlib.h>
void push(int stack[],int* top);
void pop(int stack[],int* top);
void traverse(int stack[],int top);
int main(void){
    int stack[10];
    int top = 9;
    int i;
    int choice;
    printf("Enter the elementz\n");
    for ( i = 0; i < 10; i++){
        scanf("%d",&stack[i]);
    }
    for ( i = 0; i < 10; i++){
        printf("++++++++ MENU ++++++++\n\n\n");
        printf("Enter 1 to push \n Enter 2 to pop\n Enter 3 to display \n\n\n");
        printf("Enter you choice \n\n\n");
        scanf("%d",&choice);
        switch(choice){
            case 1 :
                push(stack,&top);
                break;
            case 2:
                pop(stack,&top);
                break;
            case 3 :
                traverse(stack,top);
                break;
            default:
                printf("Enter the correct choice\n");


        }
    }
}
void push(int stack[],int* top){
    int item;
    int max = 10;
    printf("Enter the number you want to input\n");
    scanf("%d",&item);
    if(*top == max-1){
        printf("It's full\n");
    }
    else{
        *top = *top+1;
        stack[*top] = item;

    }
}
void pop(int stack[],int* top){

    if(*top == -1){
        printf("STack is empty\n");
    }
    else{
        *top = *top-1;
    }
}
void traverse(int stack[],int top){
    int i;
    if(top == -1){
        printf("WHy r u giving m empty stack to print");
    }
    else{
        for (i = 0; i <= top; i++){
            printf("Your stack is : \n");
            printf("%d\n",stack[i]);
        }
    }
}
Comments