• C

Error in function returning string value.

I have this function which takes a double value and returns the engineering notation as a string. Two problems: 1) it can't be called multiple times in a printf, see run results below. Two, if I remove the static declaration I get a compile error (I wrote this code long ago and don't know why I made it static).

#define  true            1
#define  false           0
#define  len            32

char *dtos_engr (double value, char *units, short precision)
/*******************************************************************************************
 Returns the string equivalent to <value> in engineering units, i.e.,
 <value> = 3.14e-9 and <units> = 'S' will return 3.14nS.
 *******************************************************************************************/
{
  double lvalue;
  short neg=false;
  static char str1[len],str2[len];

  lvalue = value;
  if (lvalue < 0.0) {
    lvalue = -lvalue;
    neg = true;
  }

  if (precision == 6) {
    if (lvalue == 0.0)
      strcpy (str1,"0");
    else if (lvalue >= 1.0e9) {
      sprintf (str1, "%-.6f",(lvalue/1.0e9));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"G");
    } else if (lvalue >= 1.0e6) {
      sprintf (str1, "%-.6f",(lvalue/1.0e6));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"M");
    } else if (lvalue >= 1.0e3) {
      sprintf (str1, "%-.6f",(lvalue/1.0e3));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"K");
    } else if (lvalue >= 1.0) {
      sprintf (str1, "%-.6f",lvalue);
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
    } else if (lvalue >= 1.0e-3) {
      sprintf (str1, "%-.6f",(lvalue/1.0e-3));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"m");
    } else if (lvalue >= 1.0e-6) {
      sprintf (str1, "%-.6f",(lvalue/1.0e-6));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"u");
    } else if (lvalue >= 1.0e-9) {
      sprintf (str1, "%-.6f",(lvalue/1.0e-9));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"n");
    } else {
      sprintf (str1, "%-.6f",(lvalue/1.0e-12));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"p");
    }
  } else if (precision == 3) {
    if (lvalue == 0.0)
      strcpy (str1,"0");
    else if (lvalue >= 1.0e9) {
      sprintf (str1, "%-.3f",(lvalue/1.0e9));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"G");
    } else if (lvalue >= 1.0e6) {
      sprintf (str1, "%-.3f",(lvalue/1.0e6));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"M");
    } else if (lvalue >= 1.0e3) {
      sprintf (str1, "%-.3f",(lvalue/1.0e3));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"K");
    } else if (lvalue >= 1.0) {
      sprintf (str1, "%-.3f",lvalue);
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
    } else if (lvalue >= 1.0e-3) {
      sprintf (str1, "%-.3f",(lvalue/1.0e-3));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"m");
    } else if (lvalue >= 1.0e-6) {
      sprintf (str1, "%-.3f",(lvalue/1.0e-6));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"u");
    } else if (lvalue >= 1.0e-9) {
      sprintf (str1, "%-.3f",(lvalue/1.0e-9));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"n");
    } else {
      sprintf (str1, "%-.3f",(lvalue/1.0e-12));
      while (str1[strlen(str1)-1] == '0')
      str1[strlen(str1)-1] = '\0';
      if (str1[strlen(str1)-1] == '.')
      strcat (str1,"0");
      strcat (str1,"p");
    }
  } else {
    if (lvalue == 0.0)
      strcpy (str1,"0");
    else if (lvalue >= 1.0e9)
      sprintf (str1, "%-.1fG",(lvalue/1.0e9));
    else if (lvalue >= 1.0e6)
      sprintf (str1, "%-.1fM",(lvalue/1.0e6));
    else if (lvalue >= 1.0e3)
      sprintf (str1, "%-.1fK",(lvalue/1.0e3));
    else if (lvalue >= 1.0)
      sprintf (str1, "%-.1f",lvalue);
    else if (lvalue >= 1.0e-3)
      sprintf (str1, "%-.1fm",(lvalue/1.0e-3));
    else if (lvalue >= 1.0e-6)
      sprintf (str1, "%-.1fu",(lvalue/1.0e-6));
    else if (lvalue >= 1.0e-9)
      sprintf (str1, "%-.1fn",(lvalue/1.0e-9));
    else
      sprintf (str1, "%-.1fp",(lvalue/1.0e-12));
  }
  if (!strcmp(str1,"0") && !strcmp(units,"S")) {
    strcat (str1,"n");
    strcat (str1,units);
  } else
    strcat (str1,units);
  if (neg) {
    strcpy (str2,"-");
    strcat (str2,str1);
    strcpy (str1,str2);
  }
  return (str1);
}   /* end of dtos_engr */


main (int argc, char *argv[])
{
  double value[6];

  value[0] = 1.6e-9;
  value[1] = 1.6e-8;
  value[2] = 1.6e-7;
  value[3] = 1.6e-6;
  value[4] = 1.6e-5;
  value[5] = 1.6e-4;

  printf ("Engr values = %s, %s, %s, %s, %s, %s\n",
        dtos_engr(value[0],"S",1),dtos_engr(value[1],"S",1),
        dtos_engr(value[2],"S",1),dtos_engr(value[3],"S",1),
        dtos_engr(value[4],"S",1),dtos_engr(value[5],"S",1));
}

Output:
Engr values = 160.0uS, 160.0uS, 160.0uS, 160.0uS, 160.0uS, 160.0uS
jimdgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddunleaCommented:
Hi jimdgar,

You are having trouble because of pointers and variable scoping. What you should do is call the funtion with a char** as an arg.

So in main, have

char str1[LEN];

And then call the function with str1 as an argument.

If you remove the static declaration, then when you return, the char* you are returning goes out of scope and no longer references memory. By having the str1 variable defined within main, it remains in scope and is not destroyed when the function exists. Static variables are persistent between calls, which is why the compile error went away, but that really is not what they are meant for.

0
imladrisCommented:
Without the static modifier the strings would be "automatic", that is, they would have been allocated on the stack.
The problem with that is that as soon as the function returns (and before the value can be used), the stack is cleaned up, and so the strings disappear!

Making the strings static ensured that they would persist. A static variable is not on the stack. However, when the function is called the next time, the string variable is overwritten with the new value, of course, leading to problems with calling the function multiple times.

The standard solution for this kind of issue is to pass in a pointer to a character array, to be used as a spot in which to return the result. So you would get something like this:

#define  true            1
#define  false           0
#define  len            32

char *dtos_engr (double value, char *units, short precision,char *res)    // <- New argument
/*******************************************************************************************
 Returns the string equivalent to <value> in engineering units, i.e.,
 <value> = 3.14e-9 and <units> = 'S' will return 3.14nS.
 *******************************************************************************************/
{
  double lvalue;
  short neg=false;
  static char str1[len],str2[len];

  lvalue = value;
  if (lvalue < 0.0) {
    lvalue = -lvalue;
    neg = true;
  }

  if (precision == 6) {
    if (lvalue == 0.0)
      strcpy (str1,"0");

  //  all the other stuff

   if (neg) {
    strcpy (str2,"-");
    strcat (str2,str1);
    strcpy (str1,str2);
  }
  strcpy(res,str1);
  return (res);           // <- return result in passed in string
}   /* end of dtos_engr */


main (int argc, char *argv[])
{
  double value[6];
  char res1[20],res2[20],res3[20],res4[20],res5[20],res6[20];   // define variables for results

  value[0] = 1.6e-9;
  value[1] = 1.6e-8;
  value[2] = 1.6e-7;
  value[3] = 1.6e-6;
  value[4] = 1.6e-5;
  value[5] = 1.6e-4;

  printf ("Engr values = %s, %s, %s, %s, %s, %s\n",
       dtos_engr(value[0],"S",1,res1),dtos_engr(value[1],"S",1,res2),          // pass in arguments for results
       dtos_engr(value[2],"S",1,res3),dtos_engr(value[3],"S",1,res4),
       dtos_engr(value[4],"S",1,res5),dtos_engr(value[5],"S",1,res6));
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddunleaCommented:
Not to be a stickler, but I am curious to know why the person who posted after me, with the same answer as me, was awared the points?
0
jimdgarAuthor Commented:
ddunlea,

The two responses arrived almost simultaneously (yours was first). Even tho you pointed out the root problem your solution didn't work; passing str1 six times returned the same value. imladris' solution to define six separate vars worked right off the bat.

jimdgar
0
ddunleaCommented:
Fair enough. I suspect it was the implementation as opposed to my solution that didn't work though as it worked for me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.