Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ problem with mudstrlcpy
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Thu 25 Oct 2007 11:23 PM (UTC) |
| Message
| I am having issues with copying strings into null values
i have this struct:
struct map
{
char *area;
};
and stuck it inside struct pc_data
when i try to copy something into it
mudstrlcpy( ch->pcdata->atlas[0].area, afile, MAX_STRING_LENGTH );
i get this in game bug message:
Log: [*****] BUG: mudstrlcpy: NULL dst string being passed!
comparison note:
if i try to print out ch->pcdata->atlas[0] then it just prings "NULL"
but if I create a new char array within a local function and print it without initializing it or assigning any values it just prints blank.
So my question I guess is How do I come about getting the atlas values to not be null so I can use string copy functions?
| | Top |
|
| Posted by
| Zeno
USA (2,871 posts) Bio
|
| Date
| Reply #1 on Fri 26 Oct 2007 02:20 AM (UTC) |
| Message
| | I'm a bit confused. Why would you be trying to copy a null string in the first place? |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | | Top |
|
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Reply #2 on Fri 26 Oct 2007 03:00 AM (UTC) |
| Message
| I am trying to put the contents of
into
ch->pcdata->atlas[0].area
so I use
mudstrlcpy( ch->pcdata->atlas[0].area, afile, MAX_STRING_LENGTH ); | | Top |
|
| Posted by
| Zeno
USA (2,871 posts) Bio
|
| Date
| Reply #3 on Fri 26 Oct 2007 03:04 AM (UTC) |
| Message
| | I'm not sure what afile is. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | | Top |
|
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Reply #4 on Fri 26 Oct 2007 03:20 AM (UTC) Amended on Fri 26 Oct 2007 03:22 AM (UTC) by Nargsbrood
|
| Message
|
char *afile;
...
mudstrlcpy ( afile, ch->in_room->area->filename, MAX_STRING_LENGTH );
...
mudstrlcpy( ch->pcdata->atlas[0].area, afile, MAX_STRING_LENGTH );
so in the example above I am copying the area filename into char array 'afile'. after a few checks are passed I then want to copy the area file name that is now stored within 'afile' into atlas[0].area which is also another char array.
the first copy into afile works but from afile to atlas.area does not work. that is when I get the in game bug message
Log: [*****] BUG: mudstrlcpy: NULL dst string being passed!
Where dst, I assume, stands for destination. I tried printing the dst variable which was atlas.area and it boldly displays that it is the one that is null. I am not able to copy a *char into a NULL variable. The struct as shown above in the initial post shows that it is of the right *char type.
How am I supposed to save anything into atlas[0].area when nothing can be saved over that NULL value? If i try to change atlas.area[0] i get a crash.
:( I am at a loss of ideas on how to get it work right. | | Top |
|
| Posted by
| Zeno
USA (2,871 posts) Bio
|
| Date
| Reply #5 on Fri 26 Oct 2007 04:53 AM (UTC) |
| Message
| | I've never used mudstrlcpy. Why not just use sprintf? |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | | Top |
|
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Reply #6 on Fri 26 Oct 2007 05:17 AM (UTC) |
| Message
| can i get an example? i only see how sprintf can be used to format strings and probably sends it to a buffer when done.
is there a way to copy one string to another using it? | | Top |
|
| Posted by
| Zeno
USA (2,871 posts) Bio
|
| Date
| Reply #7 on Fri 26 Oct 2007 05:28 AM (UTC) |
| Message
| sprintf(ch->pcdata->atlas[0].area, afile);
|
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | | Top |
|
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Reply #8 on Fri 26 Oct 2007 05:44 AM (UTC) |
| Message
| sprintf ( ch->pcdata->atlas[0].area, afile );
i try to compile the above statment and get -
$ make
make -s smaug
Compiling o/act_move.o....
act_move.c: In function `void add_map(CHAR_DATA*)':
act_move.c:3674: warning: format not a string literal and no format arguments
make[1]: *** [o/act_move.o] Error 1
make: *** [all] Error 2
is there another method that work in this way? | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #9 on Fri 26 Oct 2007 05:48 AM (UTC) |
| Message
| You need to allocate memory for the string before you can copy something to it. A NULL pointer means that there is no memory for the string.
The problem is that strings in C aren't actual values like integers; a string is a pointer in memory to a series of characters, where the last one is the zero byte.
The short version is that you'll need to allocate a buffer for the ch->pcdata->atlas[0].area pointer using e.g. malloc. (There are lots of examples of this in the code.)
Or, you could use str_dup or some other appropriate string duplication function on 'afile'. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Nargsbrood
(54 posts) Bio
|
| Date
| Reply #10 on Fri 26 Oct 2007 08:02 AM (UTC) |
| Message
| That did the trick. Thanks.
I guess I am having problems understanding what the difference is between
*char and char[x]
how does **char fit in to all of that? does it compare to char[x][y]?
and &char?
I was under the impression that *char and char[x] were identical... ? | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #11 on Fri 26 Oct 2007 09:02 AM (UTC) |
| Message
| A char* is a pointer to a character. char[x] is an array of characters; it happens that an array is really a pointer to the first element of the array.
However, char[x] creates a contiguous block of x chars, whereas char* does not allocate any memory at all.
Similarly for char**.
&c is the memory address of 'c', meaning that it gives you a pointer to the contents of the variable 'c'. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #12 on Sat 27 Oct 2007 06:46 AM (UTC) |
| Message
|
Quote:
I was under the impression that *char and char[x] were identical... ?
Take a look a the C FAQ, which covers this sort of stuff in detail.
http://c-faq.com/
In particular:
http://c-faq.com/aryptr/index.html
That covers arrays and pointers.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
33,284 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top