小编oua*_*uah的帖子

如果先前将double初始化为零,那么将double比较为零是正确的吗?

我了解到比较双重使用==并不是明智的做法.但是我想知道检查双重是否已经初始化可能是危险的.例如,知道变量doubleVar如果已初始化则不能为零,这样做是否安全?

Foo::Foo(){
    doubleVar = 0.0;  // of type double
}

void Foo::Bar(){
    if(doubleVar == 0){ // has been initialized?
        //...
    }else{
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

c c++ floating-point

15
推荐指数
1
解决办法
2633
查看次数

pow()转换为整数,意外结果

pow()在C编程语言中使用整数强制转换函数时遇到了一些问题.我正在使用的编译器是Windows平台的Tiny C编译器(tcc版本0.9.24).执行以下代码时,它会输出意外结果100, 99:

#include <stdio.h>
#include <math.h>

int main(void)
{
    printf("%d, ", (int) pow(10, 2));
    printf("%d", (int) pow(10, 2));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在这个在线编译器中,输出符合预期:100, 100.我不知道是什么导致了这种行为.有什么想法吗?我编程错误,编译错误?

c floating-point casting tcc pow

8
推荐指数
2
解决办法
2819
查看次数

C:我用pow(10,2)和pow(10,j)获得了不同的结果,j = 2;

这个打印100:

int j=2;
int i= pow(10,2);  
printf("%d\n", i);
Run Code Online (Sandbox Code Playgroud)

这个打印99:

int j=2;
int i= pow(10,j);  
printf("%d\n", i);
Run Code Online (Sandbox Code Playgroud)

为什么?

c floating-point pow

8
推荐指数
1
解决办法
5715
查看次数

从不兼容的指针类型(指向函数的指针)初始化

我完全陷入了困境.我在3个文件中有以下代码:

文件mixer_oss.c

#include "mixer.h"

static char *devices[] = SOUND_DEVICE_NAMES;

static char **oss_get_device(void)
{
    int i, o, devs, res;
    char **result;

    if ((ioctl(fd, SOUND_MIXER_READ_RECMASK, &devs)) == -1) {
        return NULL;
    } else {
        result = malloc(sizeof(char*)*SOUND_MIXER_NRDEVICES);
        o = 0;
        for (i=0; i < SOUND_MIXER_NRDEVICES; i++) {
                res = (devs >> i)%2;
                if (res) {
                    result[o] = malloc(strlen(devices[i])+1);
                    sprintf(result[o], "%s", devices[i]); 
                    o++;
                }
                result[o] = NULL;   
            }
    }

    return result;
}

struct mixer oss_mixer = {
    .get_device = oss_get_device,
};
Run Code Online (Sandbox Code Playgroud)

文件mixer.h

#ifdef …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

2
推荐指数
1
解决办法
753
查看次数

标签 统计

c ×4

floating-point ×3

pow ×2

arrays ×1

c++ ×1

casting ×1

pointers ×1

tcc ×1