[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error in function returning string value.

Posted on 2004-11-17
5
Medium Priority
?
268 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:jimdgar
  • 3
5 Comments
 
LVL 5

Expert Comment

by:ddunlea
ID: 12605197
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
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 12605225
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
 
LVL 5

Expert Comment

by:ddunlea
ID: 12605603
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
 

Author Comment

by:jimdgar
ID: 12606772
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
 
LVL 5

Expert Comment

by:ddunlea
ID: 12606875
Fair enough. I suspect it was the implementation as opposed to my solution that didn't work though as it worked for me.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question