Question : VS2008 compilation error

Dear Experts,

 I am writing a C program using VS2008 to evalute expression using a stack. I have the algorithm worked out but i am running into some compilation error.

 code is attached. As soon as I put in the line

charstack.top = -1;

I start getting a compliation error.

: error C2143: syntax error : missing ';' before 'type'
 error C2143: syntax error : missing ';' before 'type'
 error C2143: syntax error : missing ';' before 'type'

I just cant figure out what the porblem is. Please provide me some insights.

Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define TRUE 1
#define FALSE 0
#define EMPTYSTACK -1
#define STACKSIZE 30

int empty(struct stack*);
int pop(struct stack*);
int push(struct stack*, char);

typedef struct stack{
	int top;
	char stackelement[STACKSIZE];
} CHSTACK;

int main(void)
{
	FILE *ifp, *ofp; //input and output file pointers
	char *mode = "r";
	CHSTACK charstack, *stack_ptr;
	//charstack.top = -1;
	//stack_ptr = &charstack;

	char outputFilename[] = "out.txt";
	int i;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define TRUE 1
#define FALSE 0
#define EMPTYSTACK -1
#define STACKSIZE 30

int empty(struct stack*);
int pop(struct stack*);
int push(struct stack*, char);

typedef struct stack{
	int top;
	char stackelement[STACKSIZE];
} CHSTACK;

int main(void)
{
	FILE *ifp, *ofp; //input and output file pointers
	char *mode = "r";
	CHSTACK charstack, *stack_ptr;
	//charstack.top = -1;
	//stack_ptr = &charstack;

	char outputFilename[] = "out.txt";
	int i;

Answer : VS2008 compilation error

try the modification that I made:

In C all variable declarations in a block have to occur before your statements. This is not the case in C++. It compiles fine in C++, and not in C. I will bet that your file extention is .c
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
	FILE *ifp, *ofp; //input and output file pointers
	char *mode = "r";
	CHSTACK charstack, *stack_ptr;
	char outputFilename[] = "out.txt";
	int i;

	charstack.top = -1;
	//stack_ptr = &charstack;

        return 0;
}
Random Solutions  
 
programming4us programming4us