[Courses] [C] Lesson 12: debugging exc. 1

Morgon Kanter admin at surgo.net
Sun Nov 17 19:37:02 EST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alright, I managed to make it work, although I don't really see why 
it wouldn't have worked the first time. It still returns a pointer to a local 
variable. Could the -pointer- part of that sentence be the reason? Anyway, 
here is my fixed code, added an easy-to-see comment at my changed part.
Guess this wasn't all that bad - I learned a bit of DDD/GDB doing this =)
/*****************************************************************/
#include <stdio.h>
#include <string.h>

int main(void)
{
    char *tmp_name(void); /* function prototype */ 
/* THIS IS THE PART I CHANGED. ADDED THE NEXT 2 LINES */
    char name[10];
    strcpy(name, tmp_name());
/* now it prints the char array name instead of tmp_name() */
    printf("Name: %s\n", name);

    return 0;
}
/*****************************************
** tmp_name -- return a temporary filename
**             Each time it is called, a
**             new name will be returned.
**
** Returns
**     pointer to a temporary name
******************************************/
char *tmp_name(void)
{
     /* the name */
    char name[5];
    static int sequence = 0; /* sequence number for the last digit */

    sequence++;  /* bump sequence number */
    strcpy(name, "tmp");

    /* turn a numeric sequence number into an ascii digit */
    name[3] = sequence + '0';

    /* store end of string */
    name[4] = '\0';

    /* return pointer */
    return name;
}
/*****************************************************************/

- --
Fetch my UPDATEd public key from http://www.surgo.net/pubkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)

iQEVAwUBPdgoIg+aqp0pfOpbAQKQ+wgAotBdOuXunIkucpW/YcoJTjRUKCqIjN/I
YFZVQSsmA4hAtFE/RuYYAeaqbskFokYIh45hnGZx70u6prX+Hw6NBwdqaQvnI68N
Dn6PWrOiRylobCkp6mpFXUcidwVNHSh9aeIctQv8z2vckhFS1lvLoyKjAFs51uaL
07LXrxvYZyvXWLcHa71tMp3GhrgbexRCg37S7MooMB1g7cnhAx+Pb5+UyH5CI6E4
OsLZu242Bw+vvctdUleJCV+wwk8LBZWeFp3/F4iHlb8iJcoq3Qk0S1UaeAPtkY8f
5pUT9yelSx6p9d754gwb5/c1B85bwbohAMm5GESN+YNKGokMa/2r9Q==
=XBed
-----END PGP SIGNATURE-----



More information about the Courses mailing list