返回描述增长率的字符串的C函数

np8igboo  于 2023-02-21  发布在  其他
关注(0)|答案(3)|浏览(125)

我必须在C语言中创建一个函数,其中参数是一个double(growth rate),并且必须返回一个描述增长率的字符串const char*,例如,如果增长率是2.9,它将返回一个字符串high
到目前为止我只有这些,

const char *get_growth_rate_description(double growth_rate) {

  char *description;
  double growth_percentage = growth_rate * 100;

  if (growth_rate < 0) {
    char *description = "Negative";
  } else if (growth_rate >= 0 && growth_rate < 1) {
    char *description = "reasonable";
  } else if (growth_rate >= 1 && growth_rate < 2) {
    char *description = "ambitious";
  } else if (growth_rate >= 2 && growth_rate < 4) {
    char *description = "high";
  } else {
    char *description = "unreasonable";
  }

  return description;
}

int main(void) {

  double growth = calculate_growth_rate(27367, 40000, 2022, 2035);

  const char *description = get_growth_rate_description(growth);

  printf("%s", description);
}

我收到一个警告,说明描述未初始化,如果我尝试在主驱动程序中运行该程序,则会打印“0\365\337o”

gpnt7bae

gpnt7bae1#

问题在于,您在每个if/else if分支中声明了一个新的、不同的(如果名称相似)char *description,因此,顶层description实际上保持未初始化状态。
因为你要返回一个常量字符串,你可以

const char *get_growth_rate_description(double growth_rate) {
  if (growth_rate < 0) {
    return "Negative";
  }
  if (growth_rate < 1) {
    return "reasonable";
  }
  if (growth_rate < 2) {
    return "ambitious";
  } 
  if (growth_rate < 4) {
    return "high";
  }
  return "unreasonable";
}
gkl3eglg

gkl3eglg2#

char *description;未初始化。
然后在if语句作用域中定义新的description变量,但是它们在这些作用域之外停止存在,并且返回在函数作用域中定义的description,而这个变量没有初始化。
您需要从分配中删除char *

const char *get_growth_rate_description(double growth_rate) {

  char *description;
  double growth_percentage = growth_rate * 100;

  if (growth_rate < 0) {
    description = "Negative";
  } else if (growth_rate >= 0 && growth_rate < 1) {
    description = "reasonable";
  } else if (growth_rate >= 1 && growth_rate < 2) {
    description = "ambitious";
  } else if (growth_rate >= 2 && growth_rate < 4) {
    description = "high";
  } else {
    description = "unreasonable";
  }
  return description;
}
rlcwz9us

rlcwz9us3#

对于初学者,变量growth_percentage声明如下

double growth_percentage = growth_rate * 100;

未在函数中使用。
由于函数的返回类型是const char *,因此将变量description也声明为具有类型const char *将在逻辑上更加一致。

const char *description;

在每个if语句的子语句中,你用一个名字description来声明新变量,这个新变量隐藏了在if语句集之前声明的变量,所以这个变量保持未初始化状态,函数返回的就是这个变量。
与在of语句中声明新变量不同,您需要使用assignment语句将字符串字面值赋给在if语句集之前声明的变量。

const char * get_growth_rate_description(double growth_rate) {

  const char *description;
  double growth_percentage = growth_rate * 100;

  if (growth_rate < 0) {
    description = "Negative";
  } else if (growth_rate >= 0 && growth_rate < 1) {
    description = "reasonable";
  } else if (growth_rate >= 1 && growth_rate < 2) {
    description = "ambitious";
  } else if (growth_rate >= 2 && growth_rate < 4) {
    description = "high";
  } else {
    description = "unreasonable";
  }

  return description;
}

或者你可以把函数写成

const char * get_growth_rate_description(double growth_rate) {

  const char *description = "unreasonable";
  double growth_percentage = growth_rate * 100;

  if (growth_rate < 0) {
    description = "Negative";
  } else if (growth_rate >= 0 && growth_rate < 1) {
    description = "reasonable";
  } else if (growth_rate >= 1 && growth_rate < 2) {
    description = "ambitious";
  } else if (growth_rate >= 2 && growth_rate < 4) {
    description = "high";
  }

  return description;
}

相关问题