[vox-tech] C: functions that return a string

Peter Jay Salzman vox-tech@lists.lugod.org
Wed, 30 Jan 2002 14:49:03 -0800


i have a function, GetCPUType(), that returns a local (but static)
string.  this works from main():

   char buf[BUFFERSZ];
   strcpy(buf, GetCPUType(proc_cpu));
   printf("CPU Type: %s", buf);

this doesn't (it prints garbage):

   printf("CPU Type: %s", GetCPUType(proc_cpu));

the function looks like:

   char *GetCPUType(const char *file)
   {
      static char *ptr;
      /* do stuff with ptr */
      return ptr;
   }

what's the difference between putting ptr into a buffer and printing the
buffer vs. printfing the pointer to a string directly?  ptr ends with
\0.

pete