【问题标题】:Compiler errors in my program [closed]我的程序中的编译器错误[关闭]
【发布时间】:2017-01-19 01:43:41
【问题描述】:
#include <stdio.h>
#include<stdlib.h>
char push();
char pop();
char display();
char peek();
char reverse();
# define MAX 50
struct stack
    {
        char string[MAX];
        int top= -1;
    };
void main()
    {
        int a;
        printf("Choose an option\n");
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Display\n");
        printf("4.Peek\n");
        printf("5.Reverse\n");
        scanf("%d", &a);
            switch(a)
                {
                    {
                        case 1: push();
                        break;
                    }
                    {
                        case 2: pop();
                    break;
                    }
                    {
                    case 3: display();
                    break;
                    }
                    {
                    case 4: peek();
                    break;
                    }
                    {
                        case 5: reverse();
                    break;
                    }
                    default : printf("Invalid Input");
                }


    }
char push(char a)
    (
        char a;
        printf("Enter the string");
        scanf("%c",&a);
        if(top=MAX-1)
         {
            printf("Stack Overflow");
         }

        else
         (
         top=top+1;
         string[top]=a;
         )

    )

char pop()
    (
        char c;
        (
            if(top=-1)
            {
                printf("Stack Underflow");
            }
            else(
                c=string[top];
                top=top-1;
                printf("The character removed is %c", c);
                )
           )
     )


char display()
    {
        int i;
        for(i=0,i<=MAX-1,i++)
            {
                printf("%c", string[i]);
            }
    }


char peek()
    (
     printf("The top element is %c", string[top]);
    )


char reverse()
    (
     int i;
     printf("The reverse of string is");
     for(i=MAX-1,i>=0,i--)

        (
         printf("%c",string[i])
         )

     )

这是我的程序,大约一个月前才开始 C 编程。 以下是我的错误。请帮忙。我无法从其他答案中弄清楚,如果有人能纠正我的错误,我会学得更好。谢谢

prog.c:12:16: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
         int top= -1;
                ^

prog.c:14:6: warning: return type of 'main' is not 'int' [-Wmain]
 void main()
      ^

prog.c:54:9: error: expected declaration specifiers or '...' before 'printf'
         printf("Enter the string");
         ^

prog.c:55:9: error: expected declaration specifiers or '...' before 'scanf'
         scanf("%c",&a);
         ^

prog.c:56:9: error: expected declaration specifiers or '...' before 'if'
         if(top=MAX-1)
         ^

prog.c:73:13: error: expected identifier or '(' before 'if'
             if(top=-1)
             ^

prog.c:98:6: error: expected declaration specifiers or '...' before 'printf'
      printf("The top element is %c", string[top]);
      ^

prog.c:96:6: error: 'peek' declared as function returning a function
 char peek()
      ^

prog.c:96:6: error: conflicting types for 'peek'

prog.c:6:6: note: previous declaration of 'peek' was here
 char peek();
      ^

prog.c: In function 'peek':

prog.c:105:6: error: expected declaration specifiers or '...' before 'printf'
      printf("The reverse of string is");
      ^

prog.c:106:6: error: expected declaration specifiers or '...' before 'for'
      for(i=MAX-1,i>=0,i--)
      ^

prog.c:112:6: error: expected '{' at end of input
      )
      ^

prog.c:112:6: warning: control reaches end of non-void function [-Wreturn-type]
      )
      ^

【问题讨论】:

  • 你不能像在 c 中那样初始化你的结构
  • 另外if(top=-1) 可能是个错误。
  • void main 更改为 int main。如果您正在使用建议 void main 的资源,请将它们扔到很远的地方,并获得过去 20 年写的东西。如果您在印度的教育系统中并且仍在使用 TurboC 和这些过时的想法,那么您有我的同情,但我们无法为您做作业。一个月后您必须知道,您需要通过一个一个地修复错误并重新编译以查看每次更改后仍然存在哪些错误来处理您的错误。如果你还不清楚,那么从现在开始做吧。
  • 在函数体周围使用{ … },而不是( … )。在其他人中。学习阅读错误信息;它们通常非常精确(尽管“预期的声明说明符”需要一点时间来适应——这意味着你遇到了一个严重的问题并且你混淆了编译器)。

标签: c compiler-errors stack


【解决方案1】:

struct stack {...}; 定义了一个类型,即struct stack

您不能“初始化”一个类型,也不能为 struct 类型的成员定义默认值,或者您在执行 ... top = -1 时的意图是什么。

你可以做的是定义一个该类型的变量并初始化它:

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


#define STRING_MAX (42)

struct Stack {
  char string[STRING_MAX];
  int top;
}


int main(void) {
  struct Stack stack = {
    "top entry",
    1
  };

  printf("string = '%s', top = %d\n", stack.string, stack.top);

  return EXIT_SUCCESS;
}

上面的例子打印:

string = 'top entry', top = 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 2012-08-06
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多