• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

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
#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;

Open in new window

#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;

Open in new window

0
Quantster
Asked:
Quantster
  • 5
  • 3
  • 3
  • +2
2 Solutions
 
Infinity08Commented:
What line does the error refer to ?

Could you post the entire file ? (it seems to be cut short)
0
 
phoffricCommented:
There are no errors in what you have posted.
0
 
QuantsterAuthor Commented:
I didnt post the entire file. you should be able to complie this one and get the error that i saw. Just add a return 0 statement and a closing brace and the program will be complete.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
phoffricCommented:
I did compile and there were no errors.
0
 
QuantsterAuthor Commented:
Hi Phoffirc,

 I am seeing the complie error. What machine and IDE are you using?
0
 
Infinity08Commented:
As I asked before : what line does the error message refer to ?
0
 
ghemstromCommented:
The line you refer to must have a type after it
0
 
phoffricCommented:
I used VS 2008 Express.

The C2143 error is described here:
   http://msdn.microsoft.com/en-us/library/0afb82ta.aspx

As far as your particular case, it compiles fine here.

Perhaps in your original source, there is a hidden spurious character that is causing an error. Why don't you copy what you posted and overwrite what you have and see if that fixes the problem.
0
 
Anthony2000Commented:
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

	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;
}

Open in new window

0
 
Anthony2000Commented:
Here is the whole example. Do you understand why you were seeing the C2143 compile error?

#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;
	char outputFilename[] = "out.txt";
	int i;

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

    return 0;

}

Open in new window

0
 
QuantsterAuthor Commented:
The problem was I was using a non-decration statement before finishing all my declarations. THe msd link points that out.

Thanks for all your help.
0
 
Anthony2000Commented:
I found this link:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=215&rll=1

and here is a portion of that link:

Dispersed Statements and Declarations
------------------------------------------------------------------
In earlier versions of C, declarations could appear only at a block's beginning, before any other statements. Now it's possible to place declarations freely. The following function demonstrates this. It's C99-compliant. However, pre-C99 compilers will refuse to compile it because the declaration of rank appears after a statement (and because it uses // comments):
 void compensations(Employee *emp)
 {
     double bonus=155.25;
     double salary;
     salary=employee->salary + bonus;
     int rank; //allowed only in C99
     //.. rest of the code
}

I would have thought that VS2008 Express would be C99 compliant with regard to placement of variables.


0
 
phoffricCommented:
>> I would have thought that VS2008 Express would be C99 compliant with regard to placement of variables.

"Now, the Visual C++ compiler team receives the occasionally question as to why we haven’t implemented C99.  It’s really based on interest from our users.  Where we’ve received many requests for certain C99 features, we’ve tried to implement them (or analogues).  A couple examples are variadic macros, long long, __pragma, __FUNCTION__, and __restrict.  If there are other C99 features that you’d find useful in your work, let us know!  We don’t hear much from our C users, so speak up and make yourselves heard."
   http://blogs.msdn.com/b/vcblog/archive/2007/11/05/iso-c-standard-update.aspx
0
 
Infinity08Commented:
>> Where we’ve received many requests for certain C99 features, we’ve tried to implement them (or analogues).

lol. Now that single phrase describes Microsoft's attitude towards standards very well : they don't care about standards - they do whatever they like, and try to impose their own standards.

Looking at the code for this question, I noted the C++ style comments (//), so assumed the author compiled it either with a C++ or C99 compiler (making me miss the cause of the error - well spotted, Anthony2000). Turns out it was neither - it was compiled with a "Visual C" compiler - Microsoft's own brand of C.
0
 
phoffricCommented:
Sorry it took so long to finalize the answer. :(

Yes, the link did explain about the error if you have a C program.

Had we really read your first sentence more carefully, we would have come up with an answer immediately. Nice catch Anthony2000. It turns out it was an unfair bet since you would have won 100% of the time. :)
>> I am writing a C program using VS2008
0
 
ghemstromCommented:
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now