c - 是数字在 int 数组元素上不返回 true

标签 c struct pthreads

void* checkRow(void* in)
{
    //pthread_mutex_lock(&count_mutex);
    parameters* temp = (parameters*) in;
    int row = temp->row;
    int col = temp-> col;
    int i = 0;
    int j = 0;
    for (i = row; i < SIZE; i++)
        for (j = col; j< SIZE; j++)
        {
            printf("%d ", isdigit(temp->arr[i][j]));
            if(isdigit(temp->arr[i][j]) && temp->arr[i][j] <= SIZE)
            {
                //pthread_mutex_unlock(&count_mutex);
                return (void*) 1;
            }
        }

    return (void*) 0;
}

出于某种原因,我尝试运行 isdigit(temp->arr[i][j]) 时总是返回 false 而不是 true。该数组是用所有数字初始化的。我在数组上运行了 printf,它表明数组返回的结果是正确的,所以我不知道为什么 isdigit 不能将数组的元素识别为数字。

数组是结构体的成员

typedef struct
{
    int row;
    int col;
    int arr[SIZE][SIZE];
}parameters;

最佳答案

isdigit(int) 需要在 unsigned charEOF 范围内的值,否则为未定义行为。如果 char 已签名,则使用负 char 值的 isdigit(int) 是有问题的。

isdigit(int) 当值在 '0' ... '9' 范围内时返回 true。这些辅音的数值通常为 48 到 57",但它取决于字符集 - ASCII 是最常见的。这 10 个值必须是连续的。

要将数字转换为文本数字,请使用 x + '0'

要测试文本数字并避免使用范围有限的 isdigit(),请使用:

int test_isdigit(int x) {
   return x >= '0' && x <= '9';
}

这是可移植的。

关于c - 是数字在 int 数组元素上不返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998938/

相关文章:

c - 理解线程

php - 使用 phpbrew 使用 pthreads 构建 PHP

c - 如果字符串还包含字母,如何将 char 字符串转换为一个 int 数字

c - 如何使用结构体?

python - 使用 gcc/ubuntu 的 if 子句中的段错误

objective-c - NSValue 是如何发挥它的魔力的?

c - Linux Mint 中的时间结构

c++ - 在Linux C++中使用pthread在特定时间间隔调用函数

c - C 中运算符优先级的奇怪结果

c - 获取C中最高权限的工作路径