Link to home
Start Free TrialLog in
Avatar of Quantster
Quantster

asked on

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

What line does the error refer to ?

Could you post the entire file ? (it seems to be cut short)
Avatar of phoffric
phoffric

There are no errors in what you have posted.
Avatar of Quantster

ASKER

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.
I did compile and there were no errors.
Hi Phoffirc,

 I am seeing the complie error. What machine and IDE are you using?
As I asked before : what line does the error message refer to ?
The line you refer to must have a type after it
SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.
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.


>> 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
>> 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.
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