Path: utzoo!utgpu!watmath!att!pacbell!ames!xanth!ginosko!uunet!intercon!amanda@intercon.uu.net From: amanda@intercon.uu.net (Amanda Walker) Newsgroups: comp.sys.mac.programmer Subject: Re: Seemingly equivalent code fragments in C Message-ID: <1342@intercon.UUCP> Date: 7 Aug 89 22:19:42 GMT References: <4882@tank.uchicago.edu> Sender: news@intercon.UUCP Reply-To: amanda@intercon.uu.net (Amanda Walker) Organization: InterCon Systems Corporation Lines: 42 In article <4882@tank.uchicago.edu>, s170@tank.uchicago.edu (harmon g washington) writes: [three code fragments] > I would thind the three code fragments are equivalent ways of doing the > same thing in C. Why did not the first code fragment work? > --- Harmon Washington [first fragment:] > void MenuInits() > { > int *bittenApple; > > *bittenApple = 0x0114; /* create Pascal string 1 byte long */ > AppleMenu = NewMenu(ApplID, bittenApple); > . > . > } /* menuinits() */ Well, it looks like you're pretty new to programming in C. It may take a while to get the hang of using pointer variables, but here's why this doesn't work: "bittenApple" is a pointer to an integer. The space for the integer itself is assumed to be allocated in some other way. Since it's a local variable, it will start out with an undefined (random) initial value. The assignment statement will then take this random value, treat it as a memory address, and stuff 0x0114 into it. Depending on what happens to be in memory, this will either crash your Mac or something else unpredictable. An easier way to handle the problem of creating an Apple symbol in a string is to take advantage of the extended string literal syntax that is available in most Macintosh C compilers. Try "\p\x14" or "\p\024". This says "make a Pascal string whose first character is 0x14 (or octal 024, which is equivalent), and allocates space initialized to that value. An even easier way is to put your menus into resources... Hope this helps, -- Amanda Walker InterCon Systems Corporation -- amanda@intercon.uu.net | ...!uunet!intercon!amanda