Cannot Understand Compile error

1
2
3
4
5
6
7
8
SOCKET s;
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection();
	char ip[14] = "192.168.1.104";
	char* ip_ptr;
	ip_ptr= &ip[0];
	//bool connection_worked = ConnectToHost(55555,ip_ptr);
bool connection_worked = false;


Here is the compile error I am getting. I cannot understand why it keeps saying ip_ptr is an int. I clearly have initialized it as a pointer to a char.

>c:\users\user\documents\visual studio 2008\projects\amibas\amibas\amibas.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\user\documents\visual studio 2008\projects\amibas\amibas\amibas.cpp(33) : error C2040: 'ip_ptr' : 'int' differs in levels of indirection from 'char *'

1>c:\users\user\documents\visual studio 2008\projects\amibas\amibas\amibas.cpp(33) : error C2440: 'initializing' : cannot convert from 'char *' to 'int'
I figured it out. You can only innitialize variables globaly, you cannot modify them globally.
Now that you know that the global (white space) is really there for variable declarations and thing like that - you continued to think a bit more and realised that your problem would have been solved
like this:
1
2
3
4
5
6
bool ConnectToHost(int PortNo, char* IPAddress);
char ip[14] = "192.168.1.104";
//char* ip_ptr;
//ip_ptr= &ip[0]; //Error

bool connection_worked = ConnectToHost(55555,ip);


Of course you would then carry on thinking and realize that if ip to to be a global variable
that you plan to use and modify some more time in you code, then an array of 14 chars is useless for an ip address which can be 192.169.100.100 which is 15 chars - which means an array of 16 chars (to include the terminating 0).

BUT if the iparray was made sole for the purpose of initializing the connection_worked variable and will only be used once - then it is redundant because you
could do this:
1
2
3
4
5
6
bool ConnectToHost(int PortNo, char* IPAddress);
//char ip[14] = "192.168.1.104";
//char* ip_ptr;
//ip_ptr= &ip[0]; //Error

bool connection_worked = ConnectToHost(55555,"192.168.1.104");
.
Topic archived. No new replies allowed.