problem with mudstrlcpy

Posted by Nargsbrood on Thu 25 Oct 2007 11:23 PM — 13 posts, 35,410 views.

#0
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

   map  atlas[MAX_MAPS];


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?

USA #1
I'm a bit confused. Why would you be trying to copy a null string in the first place?
#2
I am trying to put the contents of
  afile 

into
  ch->pcdata->atlas[0].area



so I use

mudstrlcpy( ch->pcdata->atlas[0].area, afile, MAX_STRING_LENGTH );
USA #3
I'm not sure what afile is.
#4

   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.
Amended on Fri 26 Oct 2007 03:22 AM by Nargsbrood
USA #5
I've never used mudstrlcpy. Why not just use sprintf?
#6
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?
USA #7
sprintf(ch->pcdata->atlas[0].area, afile);
#8
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?
USA #9
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'.
#10
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... ?
USA #11
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'.
Australia Forum Administrator #12
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.