c - 哪个是 C 安全编码中嵌套 if 的替代方案?

标签 c

<分区>

例如:

#define SUCCESS 1u
status_t status;

/* Initialize a peripheral */
status = start_Timer();

if(status == SUCCESS)
{
    /* Proceed */
    status = another_initialization();

    if(status == SUCCESS)
    {
        /* Proceed further */
    }

}

这在几个连续的过程中得到了很多缩进 对于程序中的实际算法来说,线宽非常小。 除了 C 中的异常处理之外,还有其他选择吗?

最佳答案

灵感来自 CrisBD 答案,但在执行过程中没有返回(MISRA 不推荐)。

#define SUCCESS 1u
status_t status;

/* Initialize a peripheral */
status = start_Timer();

if(status == SUCCESS)
{
   /* Proceed */
   status = another_initialization();
}

if(status == SUCCESS)
{
    /* Proceed further */
}

关于c - 哪个是 C 安全编码中嵌套 if 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61455656/

相关文章:

c - getpwuid_r 和 getgrgid_r 很慢,如何缓存它们?

c - 为什么编译的时候会出现如下错误

c execv通过函数传递参数

c++ - "dog-tag"字段是否在某些软件中使用?

iphone - 从 NSData 读取 NSNumber

使用这些标志编译时代码无法执行

c - memcpy 发生错误

c++ - 细粒度锁定

c - 如何从 GTK 旋转按钮中检索 float 形式的输入值?

CppUTest:如何将更多数据传递给特定的模拟调用?