Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex, Yahoo!

Members: 0
Guests: 14
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » Problem getting a value from ...
Forum Rules | Mark all | Recent Posts

Problem getting a value from a const char
< Newer Topic :: Older Topic > SmaugFUSS

Pages:<< prev 1 next >>
Post is unread #1 May 27, 2009, 11:28 am
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

I'm sure I'm missing something completely obvious here, but for the life of me I can't figure out what the problem is.

I've added in two extra const char to the skill_type structure in mud.h, as follows:

const char *pr1name;
const char *pr2name;

What I'm looking to do is check these values, then be able to compare them to a few different things. I've been trying to set variables to copy these values, but I can't seem to get it to work. This is what I've tried.

prnameone = skill->pr1name[sn];

I've declared prnameone as everything I can think of (char, char[], const char, etc) and I'm still getting the error,

invalid conversion from 'const char' to 'const char*'

Any help or ideas would be greatly appreciated.
       
Post is unread #2 May 27, 2009, 11:47 am
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

void do_practice( CHAR_DATA* ch, const char* argument)
{
   char prnameone;
   char prnametwo;
   int sn;
   SKILLTYPE *skill;

 
      sn = skill_lookup( argument ); //skill to learn
      prnametwo = skill->pr2name[sn];
      prnameone = skill->pr1name[sn];
      //prone = skill_lookup( prnameone );
      //prtwo = skill_lookup( prnametwo );

   pager_printf( ch, "You preq1 is %s.\r\n", prnameone);
   pager_printf( ch, "You preq2 is %s.\r\n", prnametwo);

return;
}



This is now the error I'm getting from compiling, referring to the pager. How is this possible given the pr1name is defined as a char in the structure?

format '%s' expects type 'char*', but argument 3 has type 'int'
format '%s' expects type 'char*', but argument 3 has type 'int'
       
Post is unread #3 May 27, 2009, 11:56 am
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

There are several issues here.

1. A string in C is not of type char, but of type char* or const char*.

2. When you use %s as a format specifier, you must give it a string -- i.e., something of type char* or const char* -- and not char, which is just a single character. Recall that characters are numbers, hence why it sees argument 3 as an int (admittedly that's a little strange).

3. It makes no sense to write this:
const char *pr1name;
prnameone = skill->pr1name[sn]; 

For starters, the two occurrences of pr1name are not referring to the same thing. The first one is declaring a variable; the second is a member field of the skill structure. Second, prnameone is not declared here, so it doesn't make sense to use it.
This also isn't like the code you gave later, so I'm not sure what exactly you're looking at.



Since skill->pr2name[sn] is of type const char*, you need to store it in something that is a const char*. In particular, prnameone and two should be a const char*, not just a char.

(Are the names supposed to be meaningful?)
       
Post is unread #4 May 27, 2009, 12:03 pm   Last edited May 27, 2009, 12:05 pm by Trevlyn13
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

void do_practice( CHAR_DATA* ch, const char* argument)
{
   CHAR_DATA *mob;
   const char *prnameone;
   const char *prnametwo;
   int sn;
   //int prone, prtwo;
   SKILLTYPE *skill;

   if( IS_NPC( ch ) )
      return;

      if( argument == '\0' )
      {
      send_to_char( "No argument.\r\n", ch );
      return;
      }

      if( !IS_AWAKE( ch ) )
      {
         send_to_char( "In your dreams, or what?\r\n", ch );
         return;
      }

      if( !mob )
      {
         send_to_char( "You can't do that here.\r\n", ch );
         return;
      }


      sn = skill_lookup( argument ); //skill to learn
      prnameone = skill->pronename[sn];
      prnametwo = skill->prtwoname[sn];
      //prone = skill_lookup( prnameone );
      //prtwo = skill_lookup( prnametwo );

   pager_printf( ch, "You preq1 is %s.\r\n", prnameone);
   pager_printf( ch, "You preq2 is %s.\r\n", prnametwo); 


That's the exact code I have now, I pruned some of it to make it easier to read. That code right there will not compile, it is giving me

invalid conversion from 'const char' to 'const char*'


The names are somewhat meaningful, the goal is to have two names and two values in a skill that correspond to the two prereq skill names and values required. The goal is to check those against what the character currently has before letting them practice.
       
Post is unread #5 May 27, 2009, 12:22 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Oh, I would have written prereq, not preq or pr.

As for your compilation error... How are pronename and prtwoname declared?
       
Post is unread #6 May 27, 2009, 12:27 pm
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

const char *pronename;
const char *prtwoname;

in struct skill_type

And yes, I think I will change the names of these after I get this sorted out. Which is generally more acceptable, Prereq1 or PrereqOne?
       
Post is unread #7 May 27, 2009, 12:56 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

What exactly is your intent with these things? Is it an array of prerequisites indexed by skill? Why are you indexing with sn?

I would go with first_prereq and second_prereq personally. (Well, actually, I'd go with a general-purpose list of prerequisites, allowing any number of prerequisite skills.)
       
Post is unread #8 May 27, 2009, 1:23 pm
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

This is what I had planned on doing.

Skill A would have two prerequisite skills that you would have to know to a certain value before being able to practice it. These two skills are defined in the Skill A, in skills.dat. When you try to practice Skill A, it will look up the two prerequisite skill names and the values required(haven't even bothered with that yet, those will be shorts). It will then take the first prereq, and see if the character has learned it to the appropriate level. If not, it will give a message to the player saying they are missing a prerequisite. If the first passes, it will run the second, and do the same thing. If both are at or above the required values, it will let the character practice the skill.

I can't really think of any other way to do it other than this.
       
Post is unread #9 May 27, 2009, 1:38 pm
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

The problem has to be somewhere in what the skill-> is returning, even if I try something like

pager_printf( ch, "Your preq1 is %s.\r\n", skill->name[sn]);

Which should return the name of the original skill they tried to practice, it is doing the int thing again. I *know* that part of the struct is fine, it's used all over the place. Question is, why would it be returning as a const char instead of const char* in this specific function?
       
Post is unread #10 May 27, 2009, 2:47 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

The problem has to be somewhere in what the skill-> is returning, even if I try something like

pager_printf( ch, "Your preq1 is %s.\r\n", skill->name[sn]);

Which should return the name of the original skill they tried to practice, it is doing the int thing again. I *know* that part of the struct is fine, it's used all over the place. Question is, why would it be returning as a const char instead of const char* in this specific function?

Well, this isn't really fine at all. You're taking a string (const char*) and indexing into it. The result of indexing a pointer is getting something of the type that the pointer points to. In this case, indexing into a const char* means getting out a const char (basically a single character).

Why are you trying to index into the string? If you want the string 's', you should just write 's', not 's[x]'.
       
Post is unread #11 May 27, 2009, 3:00 pm
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

I realized that, and fixed. Here is what is going on now, from the top.

1. Argument comes in (the skill name they want to practice, S1 from this point on).
2. skill_lookup on S1 to find the sn.
3. *skill = skill_table[sn], so now *skill has the data for S1
4. Now accessible are skill->pronename (S2) and skill->prtwoname (S3)
5. skill_lookup on S2 and S3 to find their respective sn.
6. Use learned function to see if character has learned S2/S3, and if so at what value.
7. Compare these values to skill->pronevalue and skill->prtwovalue.

I'm now at step 5, and getting a seg fault. gdb is telling me that skill_lookup( skill->pronename) is the issue. Any idea why argument (which is a const char *) works, and skill->pronename (which should also be a const char *) is not working? I even tried using skill->name and it is segfaulting as well.
       
Post is unread #12 May 27, 2009, 3:05 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

You'll have to run it through gdb (see this very good guide) to be able to answer that. You'll have to make sure that skill has some reasonable value, and that skill->name and skill->pronename also have reasonable values.

Could you show what exactly you do in step 3? As written, it's a little weird to assign skill_table[sn] to *skill, depending on the type of skill.
       
Post is unread #13 May 27, 2009, 3:13 pm   Last edited May 27, 2009, 3:24 pm by Trevlyn13
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

void do_practice( CHAR_DATA* ch, const char* argument)
{

   char arg[MAX_INPUT_LENGTH];
   argument = one_argument( argument, arg );
   int sn,p1sn, p2sn;
   int chpct1, chpct2;
   SKILLTYPE *skill;
   SKILLTYPE *p1skill;
   SKILLTYPE *p2skill;


   if( arg[0] == '\0' )
      {
      send_to_char( "No argument.\r\n", ch );
      return;
   }

   if( IS_NPC( ch ) ){return;}


      sn = skill_lookup( argument ); //skill to learn
      skill = skill_table[sn];

      p1sn = skill_lookup( skill->pronename );
      p2sn = skill_lookup( skill->prtwoname );


      p1skill = skill_table[p1sn];
      p2skill = skill_table[p2sn];

      chpct1 = ch->pcdata->learned[p1sn];
      chpct2 = ch->pcdata->learned[p2sn];

      pager_printf( ch, "Your preq1 %s needs %d, currently %d.\r\n", skill->pronename,skill->pr1value,chpct1);
      pager_printf( ch, "Your preq2 %s needs %d, currently %d.\r\n", skill->prtwoname,skill->pr2value,chpct2);



Program received signal SIGSEGV, Segmentation fault.
0x0807a850 in do_practice (ch=0x847b538, argument=0xbfeb1fc9 "";) at act_info.c:3048
3048 p1sn = skill_lookup( skill->pronename );

I'll see if I can get a core dump, I've never been able to get it using andlinux, I'm just attaching to the process directly.

EDIT:

backtrace is showing this.

#0 0x0807a850 in do_practice (ch=0x847b538, argument=0xbfb6a719 "";) at act_info.c:3048
#1 0x0816f34d in interpret (ch=0x847b538, argument=0xbfb6a715 "harm";) at interp.c:547
#2 0x0811dc14 in game_loop () at comm.c:843
#3 0x0811fb28 in main (argc=1, argv=0xbfb6abe4) at comm.c:528



       
Post is unread #14 May 27, 2009, 9:28 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

You need to check that skill is non-null; to do so, print 'skill'. But I can already tell you that it's null: argument points to the empty string ("";), meaning that the skill lookup will return null.

argument = one_argument( argument, arg );
...
sn = skill_lookup( argument ); //skill to learn

Since you're picked off a portion of argument into arg, the remainder of argument is empty, and you should be searching for what you put into arg.
       
Post is unread #15 May 27, 2009, 11:10 pm
Go to the top of the page
Go to the bottom of the page

Trevlyn13
Apprentice
GroupMembers
Posts64
JoinedNov 30, 2007

Fixing that sn being null was exactly what I needed. Worked my way methodically through it, everything is now working as planned.

Thanks for the help!
       
Pages:<< prev 1 next >>