2014-11-06 48 views
10

我想写一个实现Pop和Push函数的程序。问题是,我试图传递的指向整数顶部的功能,使这个整数不断变化的指针,但是当我尝试编译我总是得到这一行:C编程,错误:被调用的对象不是函数或函数指针

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

#define MAX 10 
int push(int stac[], int *v, int *t) 
{ 
    if((*t) == MAX-1) 
    { 
     return(0); 
    } 
    else 
    { 
     (*t)++; 
     stac[*t] = *v; 
     return *v; 
    } 
} 

int pop(int stac[], int *t) 
{ 
int popped; 
if((*t) == -1) 
{ 
     return(0); 
} 
else 
{ 
    popped = stac[*t] 
    (*t)--; 
    return popped; 
} 
} 
int main() 
{ 
int stack[MAX]; 
int value; 
int choice; 
int decision; 
int top; 
top = -1; 
do{ 
    printf("Enter 1 to push the value\n"); 
    printf("Enter 2 to pop the value\n"); 
    printf("Enter 3 to exit\n"); 
    scanf("%d", &choice); 
    if(choice == 1) 
    { 
     printf("Enter the value to be pushed\n"); 
     scanf("%d", &value); 
     decision = push(stack, &value, &top); 
     if(decision == 0) 
     { 
      printf("Sorry, but the stack is full\n"); 
     } 
     else 
     { 
      printf("The value which is pushed is: %d\n", decision); 
     } 
    } 
    else if(choice == 2) 
    { 
     decision = pop(stack, &top); 
     if(decision == 0) 
      { 
       printf("The stack is empty\n"); 
      } 
     else 
      { 
       printf("The value which is popped is: %d\n", decision); 
      } 

    } 
}while(choice != 3); 
printf("Top is %d\n", top); 

} 
+5

+1为异国情调的_missed分号_情况下,欢迎来到stackoverflow :) – Rerito 2014-11-06 13:09:58

+0

这个评论让我微笑很多次... – Frederick 2015-04-09 21:26:36

回答

17

你错过了一个分号只是错误在该行之前:

poped = stac[*t] <----- here 
(*t)--; 

这样做的原因奇怪的错误是编译器锯某事像那:

poped = stac[*t](*t)--; 

它可以解释为对来自表的函数指针的调用,但这显然没有意义,因为stac是一个int数组,而不是一个函数指针数组。

+0

谢谢,我正在寻找的错误,我不能相信我没有' t看错过的分号。 – 2014-11-06 14:38:42

+1

@AbylIkhsanov - 这就是这些编译器的生活方式(这是一个很好的教训 - 您经常需要查看消息中提到的行之前的错误。 – 2014-11-06 14:54:24

相关问题