Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   stdafx.h and const char* error (http://www.go4expert.com/forums/stdafxh-const-char-error-t28544/)

Prasanna_Adhikari 7Jun2012 18:05

stdafx.h and const char* error
 
I was creating a C program for creating Windows Socket, using Visual Studio 2010. I got the following errors while trying to compile it:

1. error C1083: Cannot open include file: 'stdafx.h': No such file or directory [For the line, #include <stdafx.h>]

2. IntelliSense: cannot open source file "stdafx.h" [For the line, #include <stdafx.h>]

3. IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "char" [For the line, buf[0] = " ";]

How to fix those problems, I tried google and solved other 4 errors but these three are being headache for me, I have just started Win32 Application in C so I don't have deep knowledge. I am waiting for your help.

xpi0t0s 8Jun2012 12:20

Re: stdafx.h and const char* error
 
1 & 2, the error probably means stdafx.h doesn't exist. Check if Precompiled Headers are switched on in Project Properties - Configuration - C/C++ - Precompiled Headers. The options are Create, Use and Not Using; change this to Not Using and delete the #include line and that should solve these errors.

3. If buf is defined as a char array then the error means your code is wrong, but as you haven't stated what the intent of that line is, I can't say what the code should be. It looks like you're trying to assign a single space character to the first character in buf; if this is the case then you need to use single quotes, not double quotes. But if you intend to copy a space and terminating NULL into buf, then you need to use strcpy instead. The syntax something="string constant"; only works if something is declared as char * (but don't use that, generally, unless you're completely familiar with the implications; in the early stages you should declare char something[some number large enough to contain all possible variations of the data]; strcpy(something, "string constant"); ).

Incidentally both errors can be understood from the plain English understanding. No such file means....no such file. It means the file isn't there. The second is also clear: something of type TYPE1 cannot be assigned to something of TYPE2 and means you cannot stuff a value of type TYPE1 into a variable of TYPE2, for example int a="Twenty-seven";, which fails because "Twenty-seven" is a string constant, not an integer. The rules for assignment are a bit more complex* but in general you can only assign a value of a specific type to a variable of that specific type. If you have char buf[32];, then buf itself is of type char[32] and in some contexts char*, and buf[0] is of type char (single character, which is equivalent to an 8-bit unsigned int).

*If you really want to know, you can assign values that can be easily converted. Number types can be up-converted by the compiler, for example double dbl=32; - 32 is actually an int constant, but the compiler knows this can be converted to a double. When you get into C++, if you have a user-defined type CleverInt which has some way to convert "Twenty-seven" to 27, then you can do CleverInt a="Twenty-seven"; and that will be fine. Also in C++ when you get into polymorphism an object of a derived class can be assigned to a pointer declared of one of its base class types, which is *really* useful.

Prasanna_Adhikari 9Jun2012 13:27

Re: stdafx.h and const char* error
 
strcpy(something, "string constant");

That really helped me. Thanks xpi0t0s


All times are GMT +5.5. The time now is 22:45.