RE: [Dev-C++] Weirdness with sprintf
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ed C. <em...@to...> - 2003-09-10 01:13:43
|
Oops... First off, my pseudo-code below was off (I just KNEW I would do that...) = - a and b should have been declared as char *a, *b, as Reid pointed out. = More importantly, in my real code, I had accidentally declared char foo[1], thinking I had declared foo[10]. No good. Sorry about that. -Ed -----Original Message----- From: Reid Thompson [mailto:rei...@at...]=20 Sent: Tuesday, September 09, 2003 8:31 PM To: Ed Cottrell Subject: Re: [Dev-C++] Weirdness with sprintf change char a and char b to char *a and char *b and see what you get. =20 Looks like you are assigning a string to a single char variable and =20 overflowing the allocated memory location. ie char a is a one char field = and you are assigning a 5 char variabel to it and thus not 'getting' and = end of string. char *a =3D "Hello", *b =3D "World"; char c[20]; Ed Cottrell wrote: > Hi - > > When I run the following code: > =20 > char a =3D "Hello", > b =3D "World", > c[20]; > sprintf(c, "%s %s", a, b); > > I expect c to contain "Hello World", but it contains "HelloWorld=20 > HelloWorld". In other words, it replaces EACH of the %s types in=20 > sprintf with a AND b. > > What's up with that? I'm doing almost exactly the same thing,=20 > elsewhere, with multiple arguments of the same type, and I'm not=20 > having problems there... Anybody who can help, I'd be much obliged! > > -Ed > > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf=20 > _______________________________________________ > Dev-cpp-users mailing list Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |