Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!csn!ub!galileo.cc.rochester.edu!rochester!kodak!ispd-newsserver!ism.isc.com!bud.sos.ivy.isc.com!willcr From: willcr@bud.sos.ivy.isc.com (Will Crowder) Newsgroups: comp.lang.c Subject: Re: Need help in Message-ID: <1991Jun13.230452.2274@ism.isc.com> Date: 13 Jun 91 23:04:52 GMT References: <1991Jun12.174326.5390@Veritas.COM> Sender: usenet@ism.isc.com (Ism Usenet News) Reply-To: willcr@ivy.isc.com Organization: Interactive Systems Corp. Lines: 53 In article <1991Jun12.174326.5390@Veritas.COM>, tsai@Veritas.COM (Cary Tsai) writes: |> Could you C guru tell me why the third ooprint() stmt causes coredump. |> I must be missing something in ooprint(). |> Compile and execute 'a.out'. You'll get 'flag is on', 'flag is off' and |> ...(coredump) or any bus error message. |> |> [most of the source deleted] |> |> va_start(ap); |> foo = va_arg(ap, char *); |> if ((func = va_arg(ap, PFV)) != (PFV) 0) { |> if ((client = va_arg(ap, char *)) != (char *) 0) It looks like you're expecting va_arg to return NULL if there is no argument. This isn't how va_arg() works. REMEMBER: In C, the called function has no idea how many arguments you passed! It generally has no way of knowing, but that's not important. The point is, as far as the language definition is concerned, IT DOESN'T KNOW. You must use either a sentinel value, an explicit argument count, or some other method (such as counting the number of '%' in a printf() format string) to determine how many times you can safely call va_arg() or use the result. I suspect what is happening is you are getting non-NULL garbage data from your ...func = va_arg(ap, PFV)... and then garbage non-NULL data from ...client = va_arg(ap, char *)... and then executing the garbage. If you were to call it as ooprint("ooprint", (PFV) 0); the third time it should work. Hope this helps, Will P.S. (See the FAQ!) -------------------------------------------------------------------------------- Will Crowder, MTS | "That was setting #1. Anyone want to see (willcr@ivy.isc.com) | setting #2?" INTERACTIVE Systems Corp. | -- Guinan