Subject: The mysteries of C: what does this code do?

From: Dominik Vogt <[email-masked]>
Date: Wed, 24 May 2000 18:28:23 +0200

I just came upon a most charming masterpiece of C code.  Is
anybody able to understand what it actually does?  (Except
core dumping of course).

    char *foo[1] = { "bar" };
    sprintf(foo[0], "%x", 123);

:-) 

Bye

Dominik ^_^

-- 
Dominik Vogt, Agilent Technologies, Dept. BVS
Herrenberger Str.130, 71034 Boeblingen, Germany
phone: 07031/464-4596, fax: 07031/464-3883, [email-masked]
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Dan Espen <[email-masked]>
Date: Mon, 29 May 2000 07:18:13 -0400

>     sprintf(foo[0], "%x", 123);

Gee, I'm really bad with these.

Using  Sun's compiler, this  only core dumps when  I use -xstrconst to
compile.  I think gcc has a  simpilar option, but  I'm not sure of the
gcc default.

The first  line   sets up a   1  entry array of  character   pointers,
initializing the first entry to point to the string constant "bar".

The second line uses the format string  "%x" to change the integer 123
to the string "7b".  It then copies the 7b to  the location pointed to
by the first entry in  the foo array.  This is  where the string "bar"
used to be.

--
Dan Espen
444 Hoes Lane  Room RRC 1C-214           E-mail: [email-masked]
Piscataway, NJ 08854                     Phone: (732) 699-5570
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Dominik Vogt <[email-masked]>
Date: Tue, 30 May 2000 02:20:50 +0200

> Gee, I'm really bad with these.

Hey, I can't believe it.  It took me and a colleague about
twenty minutes to understand that.  The first line looks
simple at first, but if your name is not Kernighan or Ritchie
it can give you a headache 8-)

> compile.  I think gcc has a  simpilar option, but  I'm not sure of the

The default is 'dump core'.  I think it's always a good idea - this
way you notice right from the start when your constants are overwritten.

Bye

Dominik ^_^

--
Dominik Vogt, [email-masked]
Reply-To: [email-masked]
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]