Fail to compile anything in VC++ express 2010
-
dimanche 26 février 2012 23:22
I don't quite understand how, but Visual C++ has decided to go blind. I have a program that I have made and tested in Code::Blocks, there are no linking errors, it just can't see any declared variable, it just gives an error saying 'y' is an undeclared identifier. Here is the code
/* ///////////////////// // dAmnPacket.h // */////////////////////// #include <string.h> #include <stdlib.h> typedef struct _dAmnPacketArg dAmnPacketArg; struct _dAmnPacketArg { char *key; char *value; }; typedef struct _Packet dAmnPacket; struct _Packet { char *cmd; char *param; char *body; unsigned char argc; dAmnPacketArg *args; }; dAmnPacket *parsePacket( char* ); char *getValue( dAmnPacket*, char* ); void destroyPacket( dAmnPacket* ); /* ///////////////////// // dAmnPacket.c // */////////////////////// #include "dAmnPacket.h" /* This parser assume that there is atleast a command */ dAmnPacket *parsePacket( char *data ) { dAmnPacket *p = (dAmnPacket*)malloc( sizeof(dAmnPacket) ); unsigned int fspace = strchr( data, ' ' ) != NULL ? strchr( data, ' ' ) - data : (int)NULL; // find space, if any. unsigned int fflb = strchr( data, '\n') - data; // find first line break. /* Initialize everything in the packet */ p->cmd = (char*)NULL; p->param = (char*)NULL; p->args = (dAmnPacketArg*)NULL; p->argc = 0; p->body = (char*)NULL; if( fspace != (int)NULL && fspace < fflb ) // if space is found before first line break. { p->cmd = (char*)malloc( fspace ); p->param = (char*)malloc( fflb-fspace ); strncpy(p->cmd, data, fspace); p->cmd[fspace] = '\0'; strncpy( p->param, data+fspace+1, fflb-fspace-1); // just after space and before line break. p->param[fflb-fspace] = '\0'; }else if( fspace > fflb || fspace == (int)NULL ) // no space? no param. { p->cmd = (char*)malloc( fflb ); strncpy(p->cmd, data, fflb); p->cmd[fflb]= '\0'; } unsigned int end = strrchr( data, '\0' ) - data; if( end == fflb + 1 ); // Skip to the end. else{ int bbody = strstr( data, "\n\n" ) - data; if(bbody == fflb){ p->body = (char*)malloc(strlen(data)-bbody-1); strncpy(p->body, data+bbody+2, strlen(data)-bbody-2 ); p->body[strlen(data)-bbody-1] = '\0'; }else{ char *buf = (char*)malloc( strlen(data)-fflb ); //buffer for workable data. strncpy(buf, data+fflb+1, strlen(data)-fflb); bbody = (int)strstr(buf, "\n\n"); if( bbody != (int)NULL ) // if '\n\n' is found. { bbody = bbody-(int)buf+2; // sets bbody to point right after the '\n\n'. p->body = (char*)malloc( strlen(buf)-bbody+1 ); strncpy( p->body, buf + bbody, strlen(buf) - bbody + 1 ); p->body[strlen(buf) - bbody + 1] = '\0'; strncpy(buf, buf, bbody - 2); buf[bbody] = '\0'; } int x = 0; char *pch1 = strpbrk (buf, "\n"); while (pch1 != NULL) { x++; pch1 = strpbrk (pch1+1, "\n"); } p->argc = x; p->args = (dAmnPacketArg*)calloc(p->argc, sizeof(dAmnPacketArg)); int k=0, y=0; char *pch2 = strtok(buf, "=\n\0"); while( pch2 != NULL && y <= p->argc-1 ) { if(k==0) { p->args[y].key = (char*)malloc(strlen(pch2)+1); strcpy( p->args[y].key, pch2); k=k+1; }else if(k==1) { p->args[y].value = (char*)malloc(strlen(pch2)+1); strcpy( p->args[y].value, pch2); y=y+1; k=k-1; } pch2 = strtok(NULL, "=\n\0"); } free(buf); } return p; } return p; } char *getValue( dAmnPacket *p, char *key ) { int x; for(x = 0; x == p->argc; x++){ if( key == p->args[x].key ) return p->args[x].value; } return (char*)NULL; } void destroyPacket (dAmnPacket *p){ free(p->cmd); if( p->param == (char*)NULL ) free(p->param); while( p->argc > 0 ) { free (p->args[p->argc].key); free (p->args[p->argc].value); p->argc = p->argc-1; } if (p->argc == 0) free(p->args); if (p->body == (char*)NULL) free(p->body); free(p); }
Here are the errors:
1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(26): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(29): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(36): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(40): error C2143: syntax error : missing ';' before 'type' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(41): error C2065: 'end' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(70): error C2143: syntax error : missing ';' before 'type' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(71): error C2143: syntax error : missing ';' before 'type' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(72): error C2065: 'pch1' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(72): warning C4047: '!=' : 'int' differs in levels of indirection from 'void *' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(74): error C2065: 'x' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(75): error C2065: 'pch1' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(75): error C2065: 'pch1' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(75): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(75): warning C4024: 'strpbrk' : different types for formal and actual parameter 1 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(75): warning C4047: '=' : 'int' differs in levels of indirection from 'char *' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(78): error C2065: 'x' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(81): error C2143: syntax error : missing ';' before 'type' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(82): error C2143: syntax error : missing ';' before 'type' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(83): error C2065: 'pch2' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(83): warning C4047: '!=' : 'int' differs in levels of indirection from 'void *' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(83): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(85): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(87): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(87): error C2065: 'pch2' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(87): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(87): warning C4024: 'strlen' : different types for formal and actual parameter 1 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(88): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(88): error C2065: 'pch2' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(88): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(88): warning C4024: 'strcpy' : different types for formal and actual parameter 2 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(90): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(90): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(91): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(93): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(93): error C2065: 'pch2' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(93): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(93): warning C4024: 'strlen' : different types for formal and actual parameter 1 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(94): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(94): error C2065: 'pch2' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(94): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(94): warning C4024: 'strcpy' : different types for formal and actual parameter 2 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(96): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(96): error C2065: 'y' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(97): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(97): error C2065: 'k' : undeclared identifier 1>c:\users\connor\documents\visual studio 2010\projects\damn\damn\damnpacket.c(99): error C2065: 'pch2' : undeclared identifier
I would really appreciate any help, btw this project is in full C, not C++.
Toutes les réponses
-
dimanche 26 février 2012 23:40
Remember that the C compiler in VC isn't C99 compliant, it is C90 compliant. That should give you everything you need to know.
If you need an extra hint, think about where the variables are declared.
This is a signature
Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.- Proposé comme réponse WayneAKingMicrosoft Community Contributor mardi 28 février 2012 21:55
- Marqué comme réponse Rob PanModerator mardi 6 mars 2012 09:27
-
lundi 27 février 2012 21:44
As a general rule, work with the first error then rebuild. Continue in this fashion until the complete program compiles and links.
In your case the compiler doesn't like some of your variable declarations. It is throwing it off big time or generaling confusing it. If you can rewrite the first declaration go ahead and rewrite the others before building, since you have solved one of many directly related errors.
Not all compilers work the same as you have just learned first hand.
I noticed two return statements with no obvious difference, I will assume you had a reason for that. Also keep variables in scope or local as much as possible. Again I will assume you have reasons for you code.
Good luck.
David
- Proposé comme réponse Rob PanModerator mardi 28 février 2012 06:20
- Marqué comme réponse Rob PanModerator mardi 6 mars 2012 09:27
-
mardi 28 février 2012 23:28
I am unsure what that means, I would love it if you gave me a full run through of the differences, but I took your hint and put all the variable declarations, and made them initialize at the beginning of the function and it all compiled as it should, so I thank you for your help.Remember that the C compiler in VC isn't C99 compliant, it is C90 compliant. That should give you everything you need to know.
If you need an extra hint, think about where the variables are declared.
-
mardi 28 février 2012 23:31Nope, it all looks like that because I still haven't read through a full tutorial of C and I know very little of what the good practices are. Thank you for pointing out some of the more embarrassing problems.
-
mercredi 29 février 2012 01:31
Three useful links, http://en.wikipedia.org/wiki/C_programming_language, http://en.wikipedia.org/wiki/ANSI_C and http://en.wikipedia.org/wiki/C99. But simply put, the Cxx says what version of the C standard it conforms to. It is either C90 which is short for ISO/IEC 9899:1990, C99 is short for ISO/IEC 9899:1999 and finally the newest C11 which is short for ISO/IEC 9899:2011.
So the VC compiler conforms to ISO/IEC 9899:1990, so this means most of the features listed in the C99 wikipedia entry don't exist. Some, like long long int, single line comments (//) exist though.
This is a signature
Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

