Solved

VS2008 compilation error

Posted on 2010-08-22
16
565 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Local PHP Interpreter for Phpstorm 2 192
change colour of repeater control in asp.net c# 7 73
Acrinis True image 2 61
Resolve Dependency Issues 4 47
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 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

12 Experts available now in Live!

Get 1:1 Help Now