Solved

VS2008 compilation error

Posted on 2010-08-22
16
564 Views
Last Modified: 2013-12-14
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
Comment
Question by:Quantster
  • 5
  • 3
  • 3
  • +2
16 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33496642
What line does the error refer to ?

Could you post the entire file ? (it seems to be cut short)
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33496654
There are no errors in what you have posted.
0
 

Author Comment

by:Quantster
ID: 33496673
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
 
LVL 32

Expert Comment

by:phoffric
ID: 33496674
I did compile and there were no errors.
0
 

Author Comment

by:Quantster
ID: 33496675
Hi Phoffirc,

 I am seeing the complie error. What machine and IDE are you using?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33496681
As I asked before : what line does the error message refer to ?
0
 
LVL 2

Expert Comment

by:ghemstrom
ID: 33496698
The line you refer to must have a type after it
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 150 total points
ID: 33496699
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Accepted Solution

by:
Anthony2000 earned 350 total points
ID: 33496740
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33496841
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
 

Author Comment

by:Quantster
ID: 33496851
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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33496864
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
 
LVL 32

Expert Comment

by:phoffric
ID: 33496869
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33496908
>> 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
 
LVL 32

Expert Comment

by:phoffric
ID: 33496921
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
 
LVL 2

Expert Comment

by:ghemstrom
ID: 33499744
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now