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.

Dark GDK / dbPrint strange problem

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Aug 2008 18:32
The below code should output 'C' but instead outputs:


Why is this?

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Aug 2008 19:08 Edited at: 17th Aug 2008 19:09
dbPrint deals with strings, not single characters. A string is nul terminated. Your character apparently has random characters following the memory space where the single character is stored.

char Char [2] = "C";
dbPrint (Char);

would work, I believe, but it doesn't cover all the nuances you need to understand about how C++ and other functionality deal with strings.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Aug 2008 19:11
Ohh I see, the following code works:
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Aug 2008 19:21
As long as the second character in the array is set to zero. I think most recent implementations will initialize all elements to zero but I wouldn't depend on it completely.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 17th Aug 2008 19:57 Edited at: 17th Aug 2008 20:02
Quote: "Ohh I see, the following code works"

Never assume that just because at the time something works, it's correct. Variables are never initialized automatically, and I'm guessing when you ran that code the byte after the first character was luckily already set to 0. You can zero-initialize an array or struct variable like this:



Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Aug 2008 20:02
Yeah I missed that; it should be:
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Aug 2008 20:53
Why the extra code?

char Char [2] = "C";

will take care of adding the nul terminator without the extra steps.

Likewise you could do

char Char [2] = {'C', '\0'};

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Aug 2008 21:47
The extra code stops me from forgetting the null terminator. I write code nice and spaced out with lots of comments so that its easy to read and understand
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Aug 2008 21:51
A string literal automatically provides the nul terminator. No forgetting involved.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Aug 2008 21:56
I know I know. I always look over my code to try and improve bits so when I see: 'char Char [2] = "C"' I'll probably forget that element 1 is the null terminator and change it back to the code in the original post. With the longer version of the code I can see what element 1 has been set to so I shouldn't make any mistakes.

Login to post a reply

Server time is: 2024-09-30 05:23:03
Your offset time is: 2024-09-30 05:23:03