AKSHAT KANT THAKUR
AKSHAT KANT THAKUR

Reputation: 125

Why am I getting errors in my stack data structure?

I decided to make a stack using C. I just wrote the code for a push function and now I'm getting errors (it says something is wrong with my maxsize).

I f you ask me everything seems just right, and I don't have any idea on what would the error might be.

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

#define maxsize 100;

int top=-1, item;
int s[maxsize];

void push()
{
    if (top == maxsize-1)
    {
        printf("Stack overflow\n");
        return;
    }

    top = top + 1;
    s[top] = item;
}

The error that I am getting is:-

stack.c:3:20: error: expected ']' before ';' token
#define maxsize 100;
               ^
stack.c:5:7: note: in expansion of macro 'maxsize'
int s[maxsize];
  ^~~~~~~
stack.c: In function 'push':
stack.c:3:20: error: expected ')' before ';' token
#define maxsize 100;
               ^
stack.c:9:16: note: in expansion of macro 'maxsize'
if (top == maxsize-1)
           ^~~~~~~
stack.c:9:8: note: to match this '('
if (top == maxsize-1)
   ^
stack.c:16:5: error: 's' undeclared (first use in this function)
s[top]=item;
^
stack.c:16:5: note: each undeclared identifier is reported only once
for each function it appears in

Upvotes: 0

Views: 118

Answers (2)

Harshil Khamar
Harshil Khamar

Reputation: 216

Use this code:

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

#define maxsize 100

int top=-1,item;
int s[maxsize];

void push()
{
    if (top == maxsize-1)
    {
        printf("Stack overflow\n");
        return;
    }

    top = top + 1;
    s[top] = item;
}

Here you used ; after the #define statement which caused the problem.

Upvotes: 0

purple
purple

Reputation: 212

#define directives do not end with a ;. Change to #define maxsize 100

Upvotes: 1

Related Questions