Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / C++ pointers comparing problem

Author
Message
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:08 Edited at: 12th Mar 2012 20:09
Ehh.

Heeey.

I am so, so, so, so angry now. I am trying to fix this for the whole day, and no use yet.

I have two pointers, one is part of a class, another is a 'normal' char pointer.

Initialization of 1st is like:



Initialization of the 2nd is normally,



Then, I assign values to those this way:



So, one gets it's string from the network and other one from a file.

Thing is, when I want to compare them, (ptp[ClientID].name == name) it always returns 0, as they wouldn't be the same. When I run "debug", I check those values and this is what I get for example:

Quote: ".kamac. is NOT equal to .kamac."


This is how I am preparing that message:




This is driving me mad. Is there somebody to help?

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 20:20 Edited at: 12th Mar 2012 20:22
I am not sure (and I have no time to suggest a working solution), but:


is a pointer to a character, right?


returns not a pointer but a 'string' class
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:22
Nope. Go there: http://www.appgamekit.com/documentation/Reference/Multiplayer/GetNetworkMessageString.htm and select C++ instead of BASIC in the right upper corner. You'll see that it changes to:



for C++.

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 20:28 Edited at: 12th Mar 2012 20:32
Oops, you are right, I missed the C++ definition
Did you allocate enough memory for both pointers?

EDIT:
OK, it might be not necessary, but...
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:32
Memory allocation is automatic in C++ atleast, when not using operator new. I'm not using it.

As I said, this:



Prints this:

Quote: ".kamac. is NOT equal to .kamac."


Seems they're both the same, but it yet returns 0 when comparing them.

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 20:39
The

statement, compares the values of the pointers, as far as I know, it does not compares the contents of the memory
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:43
Uhm, yes, that compares the values, but those values seem to be the same. Since they're the same, why won't it return 1?

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 20:44 Edited at: 12th Mar 2012 20:47
Quote: "seem to be the same"

Are you sure about that?
Are you running a debugger in order to check these values?
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:52
Indeed.

I've just ran debug, checked values and they're exactly the same. God, what's up with this?

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 20:56
Sorry, I quit...
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 12th Mar 2012 20:58
In C++ comparing two char* variables is not the same as comparing two strings.

You need to use strcmp or strncmp. See this reference:
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

You can compare pointers using "==" or "!=", but that will only check to see if they are looking at the same physical location in memory. It won't compare the contents of the memory.

With well in excess of 20 years of programming in C++, I am 100% certain of this.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 20:58 Edited at: 12th Mar 2012 21:04
Atleast you've tried Moraldi.

I guess this needs an answer from Paul/Lee as they might be the only ones who know what's up. Seems to be the case of what GetNetworkMessageString and ReadString returns.

@EDIT

I'm checking your idea Ancient Lady.

@EDIT2

That was fairly good idea, but it yet returns false. Here is the if:



Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 12th Mar 2012 21:04 Edited at: 12th Mar 2012 21:04
Moraldi, don't give up. Your first response
Quote: "compares the values of the pointers, as far as I know, it does not compares the contents of the memory"

was correct (see my previous response).

While using the two variables (one in a class) in format statements and debug and such, the same value will display.

But the two variables are pointing to two different dynamically created strings in different memory locations.

So, Kamac, use strcmp or strncmp (and remember to cast the passed values as 'const char*' to avoid the compiler warning).

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 21:08 Edited at: 12th Mar 2012 21:09
Quote: "So, Kamac, use strcmp or strncmp (and remember to cast the passed values as 'const char*' to avoid the compiler warning)."


Nothing changed. Here is the updated if:



Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 12th Mar 2012 21:15
strcmp will return 0 when the strings are equal. This is because it will return a negative value if the first string is lower in the alphabet than the second, and a positive value if the first string is higher in the alphabet than the second.
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 21:18 Edited at: 12th Mar 2012 21:21
I agree with you Ancient Lady.
Kamac: could you please post a more complete code snippet. There is something you are missing...

Quote: "While using the two variables (one in a class) in format statements and debug and such, the same value will display."

This is a very important point!
Also I would like to know where is the
statement. Is it in the body of the class?
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 12th Mar 2012 21:18
Yaay!

Thank you Moraldi, Ancient Lady and Paul !

It works... Like a charm!

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Mar 2012 21:23
Well done!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 13th Mar 2012 16:19
Always glad to help.

Good luck with your project.

Cheers,
Ancient Lady
3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 14th Mar 2012 04:05 Edited at: 14th Mar 2012 04:12
use .c_str() for changing a string to a char. I don't know if it will work with what you have, but it always works for me.

exp


Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps.
Developed the tiled map engine seen on the showcase.
Veterian for the military.

Login to post a reply

Server time is: 2024-11-23 05:32:06
Your offset time is: 2024-11-23 05:32:06