problems g++ (64bit) compiler ( const char * to char * etc... )

Hi everyone!

All my programming time i have spent programming on windows and using
microsoft visual c++ 2010

Since i felt that i need linux i started coding and convering my code for linux.

code for:microsoft visual c++ 2010 compiler (32 bit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void testing( void *in )
{
	int *a = (int *)in;
	cout << "a:" << *a << endl;
}

void testing2( void *in )
{
	char *c = (char *)in;
	cout << "c:" << c << endl;
}

void testing3( void *in )
{
	int a = (int)in;
	cout << "a:" << a << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 20;
	testing( &a );
	a = 24;
	testing3( (void *)a );
	char c[64] = "Hello world";
	testing2(c);
	testing2("why g++?");
	system("pause")
}


code for:linux g++ (64 bit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void syspause()
{
	do 
	{ 
		cout << "Press a key to continue..." << endl; 
	} 
	while (cin.get() != '\n');
}

void testing( void *in )
{
	int *a = (int *)in;
	cout << "a:" << *a << endl;
}

void testing2( void *in )
{
	char *c = in;
	cout << "c:" << c << endl;
}

void testing3( void *in )
{
	int a = (int)in;
	cout << "a:" << a << endl;
}

int main()
{
	int a = 20;
	testing( &a );
	a = 24;
	testing3( (void *)a ); // error
	// makes sense since void * in 64bit system is 64bit and int is still 
	// 32bit. In 32 bit system, both are 32bit so why they work are clear 
	// for me for now, however if g++ compiler see that
	// it should automatically treat that conversion
	// as done with testing() function 

	char c[64] = "Hello world";
	testing2(c);
	testing2("why g++?"); // this here is why i created thread

	syspause();
	return 1; 
}


Why does my win32 compiler let me pass const char * into char * and
g++64 compiler not?

I know i can do:
1
2
char s[9] = "why g++?";
testing2(s);

but why should i add one extra line?
Why can't compiler do that for me?

Is there any way i can compile my microsoft visual c++ 2010 compiler's
code in linux without editing my code?

I'm not sure what other surprises g++64 compiler holds since i've been working
with it very little.

edit:
i think i found a solution what is actually not bad:
1
2
3
4
5
6
7
8
9
10
11
void testing( const void *in ){
char *p = in;
cout << "p:" << p << endl;
}

int Main() {
testing("Hi!");
char p[32] = "hello world!";
testing(p);
return 0
}

Last edited on
Microsoft's compilers are notorious for ignoring the C++ standard and allowing you to write illegal code. It is illegal in C++ to initialize a char * from a string literal. It is allowed, however, do initialize a char[] from a string literal - the character array holds a copy of the literal.

By the way - you should try to write portable code that will work in all environments. We recently had a lengthy thread on this:
http://www.cplusplus.com/forum/general/161802/
Thanks.

The hardest part for me is using same things on both platforms.
For example multithreading on windows is quite is but in linux it is hard since
the features what are on windows, are missing.

Linux is missing alot of things what windows has and filling those missing features is
the part what makes coding in all environments hard.
C++11 and above has standardized threading facilities:
http://www.cplusplus.com/reference/multithreading/
http://en.cppreference.com/w/cpp/thread

I also strongly recommend using Boost - it is cross platform and does a lot of work for you:
http://www.boost.org/
Last edited on
Topic archived. No new replies allowed.