使用switch case实现栈操作的C程序出现未定义引用错误求助
栈操作C程序链接错误排查与解决
问题背景
编写了一段通过switch case实现栈(Push、Pop、Peep、Display)操作的C语言程序,期望正常执行所有栈操作,但编译时出现链接错误,相关代码及报错信息如下:
原代码
#include <stdio.h> #define max 20 int stk[max]; int top=-1; void push(int); int pop(); void peep(); void display(); int isFull(); int isEmpty(); int main() { int ch,item; do { printf("....Stack Operations.... "); printf("Press 1 for Push "); printf("Press 2 for pop "); printf("Press 3 for peep "); printf("Press 4 for display "); printf("Enter your choice "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the item :- "); scanf("%d",&item); push(item); break; case 2: int k=pop(); printf("The poped element is %d ",k); break; case 3: peep(); break; case 4: display(); break; default: printf("Invalid Choice "); } }while(ch>=1 && ch<=5); void push(int item) { if(isFull()) printf("Stack Overflow "); else { top=top+1; stk[top]=item; } } int pop() { int s; if(isEmpty()) printf("Stack Underflow "); else { s=stk[top]; top=top-1; } return s; } void peep() { if(isEmpty()) printf("Stack Underflow "); else { printf("Topmost Element of the stack is %d",stk[top]); } } void display() { if(isEmpty()) printf("Stack Underflow "); else { for(int i=top;i>=0;i--) printf("%d ",stk[i]); } } int isFull() { if(top==max-1) return 1; else return 0; } int isEmpty() { if(top==-1) return 1; else return 0; } }
报错信息
/usr/bin/ld: /tmp/ccHJ1ZkB.o: in function `main': main.c:(.text+0xf7): undefined reference to `push' /usr/bin/ld: main.c:(.text+0x103): undefined reference to `pop' /usr/bin/ld: main.c:(.text+0x12b): undefined reference to `peep' /usr/bin/ld: main.c:(.text+0x137): undefined reference to `display' collect2: error: ld returned 1 exit status
错误原因
- 函数嵌套定义:C语言标准(C89及更早)不允许在函数内部定义另一个函数,你将
push、pop等所有栈操作函数都定义在了main函数内部,导致编译器无法正确识别这些函数的实现,链接阶段找不到对应函数的定义,从而抛出"undefined reference"错误。 - 循环条件不合理:原循环条件
while(ch>=1 && ch<=5)与菜单选项(仅1-4)不匹配,输入5时会持续触发default分支并循环。
修正方案
将所有栈操作函数移至main函数外部,同时修正循环条件,优化输出格式:
修正后的代码
#include <stdio.h> #define max 20 int stk[max]; int top=-1; void push(int); int pop(); void peep(); void display(); int isFull(); int isEmpty(); // 栈操作函数定义移至main外部 void push(int item) { if(isFull()) printf("Stack Overflow\n"); else { top = top + 1; stk[top] = item; } } int pop() { int s = -1; // 初始化默认返回值,避免栈空时返回未定义值 if(isEmpty()) printf("Stack Underflow\n"); else { s = stk[top]; top = top - 1; } return s; } void peep() { if(isEmpty()) printf("Stack Underflow\n"); else { printf("Topmost Element of the stack is %d\n", stk[top]); } } void display() { if(isEmpty()) printf("Stack Underflow\n"); else { printf("Stack elements: "); for(int i = top; i >= 0; i--) printf("%d ", stk[i]); printf("\n"); } } int isFull() { return (top == max - 1) ? 1 : 0; } int isEmpty() { return (top == -1) ? 1 : 0; } int main() { int ch, item; do { printf("....Stack Operations....\n"); printf("Press 1 for Push\n"); printf("Press 2 for Pop\n"); printf("Press 3 for Peep\n"); printf("Press 4 for Display\n"); printf("Enter your choice: "); scanf("%d", &ch); switch(ch) { case 1: printf("Enter the item: "); scanf("%d", &item); push(item); break; case 2: int k = pop(); if(k != -1) // 仅当弹出有效元素时打印 printf("The popped element is %d\n", k); break; case 3: peep(); break; case 4: display(); break; default: printf("Invalid Choice\n"); } } while(ch >= 1 && ch <= 4); // 修正循环条件,匹配菜单选项 return 0; }
额外优化说明
- 为
pop函数初始化默认返回值(-1),避免栈空时返回未初始化变量导致的未定义行为。 - 给所有
printf添加\n换行符,优化输出格式,避免内容堆积。 - 简化
isFull和isEmpty函数的写法,提升代码可读性。
内容的提问来源于stack exchange,提问作者Rahul Sharma
相关产品推荐
相关产品推荐

