Advertisement

Const char vs. char?

Started by October 26, 2005 10:51 AM
6 comments, last by hplus0603 19 years, 3 months ago
Ok, so I'm following a WinSock tutorial by a guy named Johnnie. It's gone good so far, but now I'm trying to make a custom send function based on his code. But it doesn't work. :(
int SendData(char *str_buffer[256])
{
	//char buffer[256];		// Declaring a buffer on the stack
	char *buffer = new char[256];	// or on the heap

	ZeroMemory(buffer, 256);
	strcpy(buffer, str_buffer);

	nret = send(theSocket,
			buffer,
			strlen(buffer),	// Note that this specifies the length of the string; not
					// the size of the entire buffer
			0);			// Most often is zero, but see MSDN for other options

	delete [] buffer;		// If and only if the heap declaration was used

	if (nret == SOCKET_ERROR) {
		// Get a specific code
		// Handle accordingly
		return NETWORK_ERROR;
	} else {
		// nret contains the number of bytes sent
	}
}




This code gives me this error:
--------------------Configuration: Sending socket - Win32 Debug--------------------
Compiling...
main.cpp
c:\programs\c++\winsock\sending socket\main.cpp(110) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char *[]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Sending socket.exe - 1 error(s), 0 warning(s)





I've no idea why! :\ Anyone? Edit: Tried doing this:
strcpy(buffer, str_buffer -> buffer);

That gave me this error:
--------------------Configuration: Sending socket - Win32 Debug--------------------
Compiling...
main.cpp
C:\Programs\C++\WinSock\Sending socket\main.cpp(110) : error C2227: left of '->buffer' must point to class/struct/union
Error executing cl.exe.

Sending socket.exe - 1 error(s), 0 warning(s)


_______________________Afr0Games
Your function takes a pointer to an array of char, changing this to either a pointer or just an array would work. E.g.
int SendData(char str_buffer[256])orint SendData(char *str_buffer)

If you wish to keep the same function definition you could change your strcpy line to
strcpy(buffer, *str_buffer);
Advertisement
You're trying to convert an array of char pointers to a const char pointer.
char *str_buffer[256] is a pointer to an array, since for all practical purposes an array is a pointer to the first element of itself, you re asking for a pointer to a pointer.

Try either changing it to char *str_buffer or char str_buffer[256].
Thanks guys! Made it work. :)
_______________________Afr0Games
Ok, now I declared a buffer like so:

char *buffer[256]; // The string to send over the network.


And then I tried sending data like this:

	// Send/receive, then cleanup:	SendData(buffer);	closesocket(theSocket);	WSACleanup();


But then I get this error:

--------------------Configuration: Sending socket - Win32 Debug--------------------Compiling...main.cppC:\Programs\C++\WinSock\Sending socket\main.cpp(91) : warning C4715: 'WinMain' : not all control paths return a valueC:\Programs\C++\WinSock\Sending socket\main.cpp(129) : warning C4715: 'SendData' : not all control paths return a valueLinking...main.obj : error LNK2001: unresolved external symbol "int __cdecl SendData(char * * const)" (?SendData@@YAHQAPAD@Z)Debug/Sending socket.exe : fatal error LNK1120: 1 unresolved externalsError executing link.exe.Sending socket.exe - 2 error(s), 2 warning(s)


The parameter for the SendData function is now:

int SendData(char *str_buffer)


Edit: Nevermind, fixed it
_______________________Afr0Games
Advertisement
Ok guys, I'm nearly done with what I hope will be enough for a framework for a simple chat application now (the idea is to bypass MFC by using SDL (I have bookmarked the article that was posted about using SDL to create windows here earlier)). Anyway, I'm still a bit confused about this char stuff. :( Now I have a buffer declared like so:

char *buffer;


and a function implemented like so:

char RecvData(int nret, int socket){	//char buffer[256];		// On the stack	char *buffer = new char[256];	// or on the heap	nret = recv(socket,		   buffer,		   256,		// Complete size of buffer	       0);	delete [] buffer;// Manipulate buffer, then delete if and only if					 // buffer was allocated on heap	if (nret == SOCKET_ERROR)	{		// Get a specific code		// Handle accordingly		MessageBox(NULL, (char*)nret, "RecvData() error!", MB_OK);	} 	else 	{		return buffer[256];	}}


I try to call this function by doing:

	// Send and receive from the client, and finally,	buffer = RecvData(nret, theClient);	closesocket(theClient);	closesocket(listeningSocket);


That gives me this error:

--------------------Configuration: Listening socket - Win32 Debug--------------------Compiling...main.cppC:\Programs\C++\WinSock\Listening socket\main.cpp(93) : error C2440: '=' : cannot convert from 'char' to 'char *'        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style castError executing cl.exe.Listening socket.exe - 1 error(s), 0 warning(s)


Any ideas? :(

Edit: That is fixed now, but it seems that the return in the function only returns the last element. How do I do this, and how would that require the buffer outside the function to be declared?
_______________________Afr0Games
I'm sorry, but compile errors are not on topic for the networking and multiplayer forum. I suggest you try the General Programming forum or the For Beginners forum.

Good luck!
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement