Welcome to the Builder Academy

Question "buf" in old snippits

More
19 Jul 2012 15:13 #397 by Papaya Pete

Attachment eavesdrop.txt not found


Ok, taking a look at older code snippits, I'm noticing that a lot of them use something called "buf." For example, right now I'm trying to add in an eavesdrop snippit, and there is this line of code that I'm trying to figure out how to tackle.
Code:
if (!*buf) { send_to_char(ch, "In which direction would you like to eavesdrop?\r\n"); return; }

I'm trying to figure out what to do with this, what I should be checking for, but seeing as I'm not especially familiar with the code it's making it a little difficult.

I'll add the entire snippit txt here so you can get the context. :)
Attachments:

Please Log in or Create an account to join the conversation.

More
19 Jul 2012 16:11 - 19 Jul 2012 16:12 #398 by bakarus
Replied by bakarus on topic Re: "buf" in old snippits
Found this for you, should help.


Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".

Therefore, following with the values of the previous example, if we write:


beth = *ted;


(that we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25.


You must clearly differentiate that the expression ted refers to the value 1776, while *ted (with an asterisk * preceding the identifier) refers to the value stored at address 1776, which in this case is 25. Notice the difference of including or not including the dereference operator (I have included an explanatory commentary of how each of these two expressions could be read):

beth = ted; // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 )


Notice the difference between the reference and dereference operators:
& is the reference operator and can be read as "address of"
* is the dereference operator and can be read as "value pointed by"
Last edit: 19 Jul 2012 16:12 by bakarus.

Please Log in or Create an account to join the conversation.

More
19 Jul 2012 16:22 #399 by Papaya Pete
Hmm it does somewhat, but what I'm trying to figure out is what *buf is trying to point to in the first place. Also, lots of older snippits use it elsewhere in a different way.

Example (just an example, not from any code):
sprintf(buf, "You look around but fail to find anything of interest nearby", ch);

If you don't remove them then there are compiling errors since "buf" is not defined. So I'm trying to figure out what it is used for so I have a better idea on what to replace *buf with. I know what it is checking for: a direction that the player wants to eavesdrop on. So, if he doesn't input anything then it will give that message. That's the snag I've hit, and it's due to my lack of coding experience in general, I bet. :P

Thanks for the info though. :)

Please Log in or Create an account to join the conversation.

More
19 Jul 2012 16:23 #400 by Liko
Replied by Liko on topic Re: "buf" in old snippits

Papaya Pete wrote: Hmm it does somewhat, but what I'm trying to figure out is what *buf is trying to point to in the first place. Also, lots of older snippits use it elsewhere in a different way.

Example (just an example, not from any code):
sprintf(buf, "You look around but fail to find anything of interest nearby", ch);

If you don't remove them then there are compiling errors since "buf" is not defined. So I'm trying to figure out what it is used for so I have a better idea on what to replace *buf with. I know what it is checking for: a direction that the player wants to eavesdrop on. So, if he doesn't input anything then it will give that message. That's the snag I've hit, and it's due to my lack of coding experience in general, I bet. :P

Thanks for the info though. :)


define like this ;
Code:
char buf[MAX_STRING_LENGTH];

Randian(0.0.0)
Owner/Developer

Please Log in or Create an account to join the conversation.

More
19 Jul 2012 19:36 #401 by Papaya Pete
Hmmm that seemed to resolve that compiling error. Here is another one that's got me stumped (going to really need to study up on pointers!):

Now in structs.h, the snippit says to put this into the char_data struct...
Code:
struct char_data *next_listener; sh_int listening_to;

Now I'll mark ** where the compiler is having problems. This is in act.other.c.
Code:
ACMD(do_eavesdrop) { int dir; int pchance; char buf[MAX_STRING_LENGTH]; one_argument(argument, buf); if (!*buf) { send_to_char(ch, "In which direction would you like to eavesdrop?\r\n"); return; } if ((dir = search_block(buf, dirs, FALSE)) < 0) { send_to_char(ch, "Which directions is that?\r\n"); return; } if (!GET_SKILL(ch, SKILL_EAVESDROP)) { send_to_char(ch, "You try to eavesdrop on that room, but fail.\r\n"); return; } pchance = rand_number(1, 101); if (pchance > GET_SKILL(ch, SKILL_EAVESDROP)) { send_to_char(ch, "You try to listen in on the next room, but you can't make out anything.\r\n"); return; } else { if (EXIT(ch, dir)) { if (IS_SET(EXIT(ch, dir)->exit_info, EX_CLOSED) && EXIT(ch, dir)->keyword) { sprintf("The %s is closed.\r\n", fname(EXIT(ch, dir)->keyword)); //send_to_char(buf, ch); } else { ch->next_listener = world[EXIT(ch, dir)->to_room].listening; //****** world[EXIT(ch, dir)->to_room].listening = ch; ch->listening_to = EXIT(ch, dir)->to_room; //****** //send_to_char(OK, ch); } } else send_to_char(ch, "There is not a room there...\r\n"); } }

Here is the error message it's giving me.
Code:
act.other.c: In function ‘do_eavesdrop’: act.other.c:134:15: error: ‘struct char_data’ has no member named ‘next_listener’ act.other.c:136:15: error: ‘struct char_data’ has no member named ‘listening_to’

Please Log in or Create an account to join the conversation.

More
19 Jul 2012 23:50 #403 by Vatiken
Replied by Vatiken on topic Re: "buf" in old snippits
Make sure that *next_listener and listening_to are in the correct structure and make sure to do a clean compile.

tbaMUD developer/programmer

Please Log in or Create an account to join the conversation.

Time to create page: 0.397 seconds