Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

maybe this question is too simple for you guys, but.. 1

Status
Not open for further replies.

zwwz98

Technical User
May 6, 2003
2
CA
I just started c and have lots of trouble with simple statements.

I got "Declaration is not allowed here in function main"

for the following code:

int main() {
int a;
int *b;
*b = 5;

int c; //error occured here, if I get rid of this line,
//code is fine
return 0;
}
 
Make all of your declarations before any other statements. If your declaration of the integer 'c' came before the assignment of '5' to your wild pointer, it would compile. Try this instead:

int main() {
int a;
int *b;
int c;

*b = 5;

return 0;
}

This will fix your compile error, but you still have a problem with your pointer, 'b'.

I am (as well as other forum members, I suspect) tempted to blurt out the answer for the pointer problem. However, I think you'll benefit more by trying to work through it. If you are truly stumped, come on back to Tek-Tips. We'll be happy to help you out :)
 
Thank you Jason
I will check my c book again about pointer.
 
hey zwwz98,
just make all ur variable declarations b4 any statements in a given scope if u r using *.c extension for ur file. if u r using *.cpp extension then it will work no matter where ur variable deslarations appear in a given block of code.
 
C99, the new C standard, doesn't require declarations to occur at the beginning of blocks. However, it is still widely unimplemented.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top