• Welcome to Valhalla Legends Archive.
 

How do I Adding two variables

Started by Final, January 03, 2006, 03:32 AM

Previous topic - Next topic

Final

Ok well im making a program in c++ and gui and i hit a speed bump how do i add these into one variable.

                            char* server=strTemp;
                            char* URL = "steam://-applaunch 240 -game cstrike +connect ";

FrOzeN

char* server=strTemp;
char* URL = "steam://-applaunch 240 -game cstrike +connect ";

char BothVariables[255];
sprintf(BothVariables, "%s%s", server, URL);

BothVariables now contains both server and URL in it.
~ FrOzeN

Final

thanks lots  frozen your a real pal

MyndFyre



Dude, you programmed Descent?  That was a really cool game.  But I always thought it was written in C or C++....  obviously it was something else.  What was it written in?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Kp

Quote from: FrOzeN on January 03, 2006, 03:50 AM
char* server=strTemp;
char* URL = "steam://-applaunch 240 -game cstrike +connect ";

char BothVariables[255];
sprintf(BothVariables, "%s%s", server, URL);

BothVariables now contains both server and URL in it.

You're not checking whether there's actually enough room in the buffer.  If strTemp is too long, the program will crash or execute undesirable code when it attempts to return from this function.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

FrOzeN

I realise that, though I assumed the CS: Source Server's DNS name wouldn't exceed 209 characters. If it does I'm going to further assume it lags and is not worth playing on anyway.
~ FrOzeN

Warrior

Wow..

http://www.mkssoftware.com/docs/man3/strcat.3.asp

Why not use the super duper function in string.h!?

Warning: Heed the warnings on the page, you MUST ensure that s1 is large enough  to hold itself and whatever is appened to it.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

warz

I think that "descent" joke has happened before. I'm not sure if it was about his description, but I know I've seen that happen here once.

Blaze

Quote from: warz on January 03, 2006, 10:03 PM
I think that "descent" joke has happened before. I'm not sure if it was about his description, but I know I've seen that happen here once.
Yes it has happened before and Nope, it was someone else. :P
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

Final

#9
Oh sorry I mispelled my lil personnal comment I ment decent I just forgot how to spell it please forgive me!
Frozen told me its a game of somesort 3d game right ? well i didnt know about it at all never even heard of it

and the space for the buffer is easiely solved by this:

instead of putting a number just put this

"MAX_PATH"

heck ive used it alot before so the space prob didnt hurt cuz i fixed that when i read the code.

Kp

How is using a buffer of size 260 safer than a buffer of size 255?  They're both vulnerable to an overflow if you pass too much data in.  The correct thing to do would be to use a length-checking variant, such as strncat, strncpy, or snprintf, of the string function you want.  strncpy behaves strangely though, so you might be better off copying the BSD strlcpy instead.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Joe[x86]

@Myndfyre: He spelled decent wrong. =p
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Mephisto

Quote from: Kp on January 03, 2006, 11:41 PM
How is using a buffer of size 260 safer than a buffer of size 255?  They're both vulnerable to an overflow if you pass too much data in.  The correct thing to do would be to use a length-checking variant, such as strncat, strncpy, or snprintf, of the string function you want.  strncpy behaves strangely though, so you might be better off copying the BSD strlcpy instead.

For personal knowledge, can you explain how it behaves strangley and is unique compared to other length checking string functions?

Kp

Quote from: Mephisto on January 04, 2006, 06:18 PM
Quote from: Kp on January 03, 2006, 11:41 PM
How is using a buffer of size 260 safer than a buffer of size 255?  They're both vulnerable to an overflow if you pass too much data in.  The correct thing to do would be to use a length-checking variant, such as strncat, strncpy, or snprintf, of the string function you want.  strncpy behaves strangely though, so you might be better off copying the BSD strlcpy instead.

For personal knowledge, can you explain how it behaves strangley and is unique compared to other length checking string functions?

I could, but it's easier just to quote the man page. :)

       The  strncpy()  function  is similar, except that not more
       than n bytes of src are copied. Thus, if there is no  null
       byte  among  the first n bytes of src, the result will not
       be null-terminated.

       In the case where the length of src is less than  that  of
       n, the remainder of dest will be padded with nulls.


Other length-limited string functions ensure the string is null terminated (even if they must cut off some characters to do so), and don't do anything to the space beyond where the null is dropped.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Warrior

Hmm, I think it would be better practice to just account for the null terminator when doing the allocation or whatever for s1. The others seem like workarounds that withought checks can easily confuse a person.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?