jacob navia <ja***@nospam.orgwrites:
|-|e|_|_ B0Ý wrote:
>I got a problem. I declared a SOCKET var in my C program but when i
compiled the program it displayed like
*--------------------------------------------------------------*
*'SOCKET': illegal use of this type as an expression *
* C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
*--------------------------------------------------------------*
What i wrote in my program is like this..:
//-------------------------------------------------------------
//I am still trying to create a Socket and use it
SOCKET m_socket; // <- *this is where i got mad!
m_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(m_socket==INVALID_SOCKET){
printf("Error at socket(): %ld\n socket yadre!!",
WSAGetLastError());
WSACleanup();
return 0;
}else{
printf("socket() is ok [sijinaba yare]!\n");
}
[snip]
>
Microsoft compiler doesn't use standard C and you can't declare
something somewhere in your function, but only at the beginning
of a block. I guess you have something BEFORE the declaration that
you are not showing us. Please send more context.
That's misleading, or at least incomplete. Simply asserting that
Microsoft's compiler "doesn't use standard C", without bothering to
explain that it simply implements an earlier standard, is not
particularly helpful.
Microsoft's compiler, as far as I know, implements the C90 standard
(perhaps C95, which is C90 with a few minor additions). Under the C90
standard, declarations and statements may not be mixed within a block;
all the declarations must come first, followed by all the statements.
(If you want a declaration following a statement, you can introduce a
nested block.)
The C99 standard permits declarations and statements to be mixed
within a block. <OT>C++ also allows this.</OTUnfortunately, very
few compilers fully implement the C99 standard.
The line ``SOCKET m_socket;'' appears to be a declaration of a
variable named ``m_socket'', of type ``SOCKET''. The most likely
reason for the error message is that you have a statement preceding
the declaration; a compiler that implements C90 or C95 rather than C99
will complain about this. It would be nice if your compiler produced
a clearer message, something like "declarations may not follow
statements within a block", but since it's actually a syntax error,
the compiler gets confused; it can't quite figure out what you *meant*
to say. Since it's expecting to see a statement, it doesn't even
recognize ``SOCKET m_socket;'' as a declaration.
This is only a guess, since I can't see the rest of your code. If
I've guessed right, you can fix the problem by rearranging your code
so that statements follow declarations in each block (it's probably as
simple as moving ``SOCKET m_socket;'' up a line or two). If not, post
again (here or elsewhere, depending on the nature of the problem).
SOCKETs are not part of standard C; they're a system-specific feature.
If your problem were caused by your use of SOCKETs, you'd need to ask
about it in a system-specific newsgroups, probably
comp.os.ms-windows.programmer.win32 or one of the microsoft.* groups.
But in this case, your problem is *probably* related to which version
of the C standard your compiler supports; you'd have the same problem
declaring a variable of type int rather than SOCKET. If that's the
case, then this is exactly the right place to ask.
Incidentally, "//" comments are non-standard in C90 and C95; they were
introduced to the language in C99. Apparently your compiler supports
them as an extension. If you want your code to be as portable as
possible, you should use "/* ... */" comments rather than "// comments
-- but since you're using SOCKETs, your code isn't portable anyway.
Using "//" comments in Usenet postings can also cause problems, since
line-wrapping can introduce syntax errors.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"