Angband.oook.cz
Angband.oook.cz
AboutVariantsLadderForumCompetitionComicScreenshotsFunniesLinks

Go Back   Angband Forums > Angband > Development

Reply
 
Thread Tools Display Modes
Old December 4, 2009, 00:40   #1
konijn_
Hellband maintainer
 
konijn_'s Avatar
 
Join Date: Jul 2007
Location: New York, the Big Apple
Age: 43
Posts: 367
Donated: $120
konijn_ is on a distinguished road
Newbie question

In my usual passive aggresive way I smack down in one thread and humbly ask for help in another

So,

Code:
/* Check for entering a dungeon */
if ((p_ptr->visits & (0x1 << dun_level)) == 0)
{
  const char *msg = NULL;

if (dun_level == 0)
  msg = "You enter the sewers of Volterra.";
else if (dun_level == 1)
  msg = "You descend into the lower levels of Hell.";
else if (dun_level == 10)
  msg = "You descend into the circle of the Heretics."
...
msg_print(msg);
;

Could this code crash at any moment ? I seem to be putting a string in a pointer that points to one char only ?? What would be the proper way of doing this without jumping through hoops to make the msg_print work ?

T.
__________________
* Are you ready for something else ? Hellband 0.8.8 is out! *
konijn_ is offline   Reply With Quote
Old December 4, 2009, 04:47   #2
d_m
Angband Devteam member
 
d_m's Avatar
 
Join Date: Aug 2008
Location: Philadelphia, PA, USA
Age: 39
Posts: 1,516
d_m is on a distinguished road
Assigning a string literal to a "const char*" is a perfectly fine thing to do.

Your pointer (const char *msg) points to an address in memory. Your string constants will all initialize the necessary amount of memory (length + 1). Thus, your pointer does not point to "one character" but rather "an address whose offsets are character-length."
__________________
linux->xterm->screen->pmacs
d_m is offline   Reply With Quote
Old December 5, 2009, 02:00   #3
zaimoni
Knight
 
zaimoni's Avatar
 
Join Date: Apr 2007
Posts: 590
zaimoni is on a distinguished road
Quote:
Originally Posted by konijn_ View Post
In my usual passive aggresive way I smack down in one thread and humbly ask for help in another

So,

Code:
/* Check for entering a dungeon */
if ((p_ptr->visits & (0x1 << dun_level)) == 0)
{
  const char *msg = NULL;

if (dun_level == 0)
  msg = "You enter the sewers of Volterra.";
else if (dun_level == 1)
  msg = "You descend into the lower levels of Hell.";
else if (dun_level == 10)
  msg = "You descend into the circle of the Heretics."
...
msg_print(msg);
;

Could this code crash at any moment ?
No, you live. msg_print is pleased.
Quote:
Originally Posted by konijn_ View Post
I seem to be putting a string in a pointer that points to one char only ?
The pointer doesn't care about how many characters follow the one it's pointing to. As long as it's NUL-terminated before the valid memory ends, it's fine -- and this is always true for string constants.
__________________
Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg.
Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.)
Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011
zaimoni is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie questions Shaman Vanilla 8 June 3, 2009 04:28
Newbie Question about reporting bugs miyazaki Vanilla 1 January 9, 2009 00:30
Am I being thick? Newbie question merrivale Vanilla 5 December 10, 2008 16:28
More Newbie Help? Oh yeah. Funksultan Vanilla 32 October 21, 2008 07:52
Newbie problem mcgryfi Vanilla 17 March 26, 2008 02:44


All times are GMT +1. The time now is 18:34.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.