03 Aug, 2010, Zula110100100 wrote in the 1st comment:
Votes: 0
I was coloring my prompts so that <75% will be yellow hps, and so on…anway, seems to work fine, but when I redit show, some of the extra description info gets stuck in my prompt…it's as if the char *point; starts pointing to a buf1 from redit show….any ideas?


Quote
<30030hp ags: [none]
You see Main Street.
-South to [ 3034] Key: [ -1] Exit flags: [none]
You see the Jeweller's Shop.
-West to [ 3014] Key: [ -1] Exit flags: [none]
You see the market square.
30000m 30000mv>



Also from some other methods…not just redit.

Here is the code I put into bust_a_prompt in comm.c

hitbuf[0] = '\0';
manabuf[0] = '\0';
movebuf[0] = '\0';
whitebuf[0] = '\0';
clearbuf[0] = '\0';
hperc = 0;
mperc = 0;
vperc = 0;

if(IS_SET(ch->act,PLR_COLOUR)) {

strncpy(whitebuf,C_B_WHITE,strlen(C_B_WHITE));
strncpy(clearbuf,CLEAR,strlen(CLEAR));

hperc = ((ch->hit*100)/ch->max_hit);
mperc = ((ch->mana*100)/ch->max_mana);
vperc = ((ch->move*100)/ch->max_move);

if(hperc >= 75)
strncpy(hitbuf,C_B_WHITE,strlen(C_B_WHITE));
else if (hperc >=50)
strncpy(hitbuf,C_B_YELLOW,strlen(C_B_YELLOW));
else if (hperc >=25)
strncpy(hitbuf,C_B_RED,strlen(C_B_RED));
else
strncpy(hitbuf,C_RED,strlen(C_RED));

if(mperc >= 75)
strncpy(manabuf,C_B_WHITE,strlen(C_B_WHITE));
else if (mperc >=50)
strncpy(manabuf,C_B_YELLOW,strlen(C_B_YELLOW));
else if (mperc >=25)
strncpy(manabuf,C_B_RED,strlen(C_B_RED));
else
strncpy(manabuf,C_RED,strlen(C_RED));

if(vperc >= 75)
strncpy(movebuf,C_B_WHITE,strlen(C_B_WHITE));
else if (vperc >=50)
strncpy(movebuf,C_B_YELLOW,strlen(C_B_YELLOW));
else if (vperc >=25)
strncpy(movebuf,C_B_RED,strlen(C_B_RED));
else
strncpy(movebuf,C_RED,strlen(C_RED));


Then the usual stuff, then I put a thing to break down color codes for the prompt

sprintf(pbuf, C_B_GREEN); break;
case 'M':
sprintf(pbuf, C_B_MAGENTA); break;
case 'R':
sprintf(pbuf, C_B_RED); break;
case 'W':
sprintf(pbuf, C_B_WHITE); break;
case 'Y':
sprintf(pbuf, C_B_YELLOW); break;
case 'D':
sprintf(pbuf, C_D_GREY); break;
case '*':
sprintf(pbuf, "%c", 007 ); break;
case '/':
sprintf(pbuf, "%c", 012 ); break;
case '{':
sprintf(pbuf, "%c", '{' ); break;
}
i = pbuf;
++str;
while( (*point = *i) != '\0' )
++point, ++i;
continue;
}


Then later…
case 'h' :
sprintf( buf2, "%s%d%s", hitbuf, ch->hit, clearbuf);
i = buf2; break;
case 'H' :
sprintf( buf2, "%s%d%s", whitebuf, ch->max_hit, clearbuf);
i = buf2; break;
case 'm' :
sprintf( buf2, "%s%d%s", manabuf, ch->mana, clearbuf );
i = buf2; break;



I don't see how other stuff keeps getting locked in here…I haven't coded in awhile and was never a pro at all..
04 Aug, 2010, Sharmair wrote in the 2nd comment:
Votes: 0
My best guess on this is your use of strncpy(). The way you are using them, in the code
I can see here, you are not terminating them. The way you are using them would be if
you where replacing just the characters at the start of the string with your string, but
keeping the existing string intact. The strncpy() will not put a NUL at the end of your
copied string if the count is smaller then or equal to the length of your string.
04 Aug, 2010, ralgith wrote in the 3rd comment:
Votes: 0
Eeewwww! sprintf() as well?! Those should all be snprintf() imo.
05 Aug, 2010, Zula110100100 wrote in the 4th comment:
Votes: 0
So how do I fix it? What better use of strncpy or other method would put only what I want in there, only the color code

I guess really could i get some help with the difference between *char and char[]

How do I convert one to the other? What are the best uses for either, or what is the point of both…anything?
05 Aug, 2010, JohnnyStarr wrote in the 5th comment:
Votes: 0
Zula, [] is array notation, and * is pointer notation. A string in C is really just a series of characters.
Any time you declare a char array, you must use up a specific amount of memory, once allocated, it cannot be changed.
A type char* points to the first char in sequence and goes until it reaches the end of the "string" at character '\0' which is just
an extra character so it knows where to end.

This is very basic C, and I would suggest hitting the books before getting too much deeper into a MUD.
Have fun though, and keep dabbling, just read up.
05 Aug, 2010, Zula110100100 wrote in the 6th comment:
Votes: 0
Yeah I understand that [] means an arrray, just not the different uses of the two really. I need to warm up and get used to C, I was primarily a java coder…anyway, wound up fixing it without learning anything by doing it later in the game where it will be used and getting rid of all of those arrays.

case 'h' :
if(IS_SET(ch->act, PLR_COLOUR)) {
int hperc = ((ch->hit*100)/ch->max_hit);
if(hperc>=75) {
sprintf( buf2, "%s%d%s", C_WHITE, ch->hit, CLEAR);
} else if(hperc>=50) {
sprintf( buf2, "%s%d%s", C_YELLOW, ch->hit, CLEAR);
} else if(hperc>=25) {
sprintf( buf2, "%s%d%s", C_B_RED, ch->hit, CLEAR);
} else {
sprintf( buf2, "%s%d%s", C_RED, ch->hit, CLEAR);
}
} else {
sprintf( buf2, "%d", ch->hit);
}
i = buf2; break;


Seems more memory efficeint anyway no?
05 Aug, 2010, Runter wrote in the 7th comment:
Votes: 0
Array in c is just a packaged way to deal with pointers. A pointer can direct to a series of memory.

char *s = malloc(10);

Less known is that you can dereference to access elements.

*(s+sizeof(char) * index) = 'c';
s[index] ='c';
05 Aug, 2010, JohnnyStarr wrote in the 8th comment:
Votes: 0
Runter said:
Array in c is just a packaged way to deal with pointers. A pointer can direct to a series of memory.

char *s = malloc(10);

Less known is that you can dereference to access elements.

*(s+sizeof(char) * index) = 'c';
s[index] ='c';


Pretty neat, but ugly. :biggrin:
05 Aug, 2010, Runter wrote in the 9th comment:
Votes: 0
Alas, ugly can describe C as a whole.
12 Aug, 2010, Zula110100100 wrote in the 10th comment:
Votes: 0
How would you do that in reverse?

I need a pointer to a string contained in an array. To pass is as a fucntion 'void whatever(char* txt)'
when all I have is
char buf[MAX_STRING_LENGTH];

n = recv(fd, buf, sizeof buf, 0);


and then need to pass buf[] as a char*???
12 Aug, 2010, Sharmair wrote in the 11th comment:
Votes: 0
An array name is taken as a pointer to what it holds if you use just the name. So you
could just pass buf in that case. There are some places that there are difference, but
for passing strings, this works fine.
12 Aug, 2010, Tyche wrote in the 12th comment:
Votes: 0
Runter said:
Less known is that you can dereference to access elements.

*(s+sizeof(char) * index) = 'c';
s[index] ='c';

Even less known is this notation is legal…
index[s] ='c';


And this..
*(s+sizeof(char) * index) = 'c';

is normally written as…
*(s+index) = 'c';

C understands pointer arithmetic based on the type of 's'.
12 Aug, 2010, Sharmair wrote in the 13th comment:
Votes: 0
Runter said:
*(s+sizeof(char) * index) = 'c';
s[index] ='c';

Yes you can access like that, but as Tyche pointed out, pointer math in C/C++ is size of
type aware. Using sizeof() like this is a bug and only works in the case where the sizeof()
is 1. If it was used with any type not of size 1, it would point to the wrong place, possibly
out of bounds of the array.
But while we are on the subject of less known notations, you can also change the order of
the pointer and int arguments to the [] operator:
s[index] = 'c';

is the same as:
index[s] = 'c';
0.0/13