- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
C 标准库中的strchr
函数在字符串中查找char
,但其签名采用int
作为搜索字符.在我发现的这两个实现中,实现将此 int
转换为 char
:
char *strchr(const char *s, int c) {
while (*s != (char)c)
if (!*s++)
return 0;
return (char *)s;
}
char *strchr(const char *s, int c) {
while (*s && *s != (char)c)
s++;
if (*s == c)
return (char *)s;
return NULL;
}
有人知道为什么吗?为什么不直接将 char
作为参数?
最佳答案
这纯粹是历史原因。请注意,在过去的 C 语言 (K&R C) 中,没有函数原型(prototype) 这样的东西。 strchr
那个时候的函数将被声明为
char *strchr();
并以 K&R 风格定义为
char *strchr(s, c)
char *s;
char c;
{
/* whatever */
}
然而,在 C 语言中(在 K&R C 和现代语言中)如果函数声明时没有原型(prototype)(如上所示),则在每个函数调用中传递的参数都服从所谓的默认值争论促销。在默认参数提升下,任何小于 int
的整数类型(或 unsigned int
)总是转换为 int
(或 unsigned int
)。 IE。当参数未声明时,无论何时传递 char
值作为参数,此值隐式转换为int
, 实际上 body 上通过了 int
. short
也是如此. (顺便说一句,float
在默认情况下会转换为 double
参数提升)。如果在函数内部参数实际上声明为 char
(如上面的 K&R 样式定义),它被隐式转换回 char
键入并用作 char
在函数里面。这就是它在 K&R 时代的工作方式,实际上直到今天,当函数没有原型(prototype)或使用可变参数时,这也是它在现代 C 中的工作方式。
现在,提示现代 C,它具有函数原型(prototype) 并使用现代风格的函数定义语法。为了保留和复制 strchr
的“传统”功能,如上所述,我们别无选择,只能声明strchr
的参数。作为int
并将其显式转换为 char
在函数里面。这正是您在引用的代码中观察到的。这正是 strchr
的功能在标准中有描述。
此外,如果您有一个已编译的遗留库,其中 strchr
如上所示以 K&R 样式定义,您决定为该库提供现代原型(prototype),即 strchr
的正确声明。会是
char *strchr(const char *s, int c);
因为int
是上述遗留实现期望实际接收到的 c
.用 char
声明它参数不正确。
因此,您永远不会看到期望类型为 char
的参数的“传统”标准库函数, short
或 float
.所有这些函数都将使用 int
类型的参数声明或 double
相反。
char 指针和 void *
的标准保证背后有一个非常相同的基本原理。指针共享相同的表示和对齐要求。依靠这个保证你可以申报malloc
作为void *
-返回函数,然后将此声明与标准库的预编译遗留版本一起使用,其中 malloc
实际返回char *
.
引用:C99 基本原理,5.10 版
7.1.4 Use of library functions
/--/
All library prototypes are specified in terms of the “widened” types: an argument formerly declared as char is now written as int. This ensures that most library functions can be called with or without a prototype in scope, thus maintaining backwards compatibility with pre-C89 code
关于c - 为什么 strchr 需要一个 int 来查找 char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8462137/
这个问题在这里已经有了答案: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? (11 个
当我尝试运行以下代码时,List(.of) 无法编译并给出主题错误。 package collections; import java.util.LinkedHashSet; import java.
我正在尝试编译使用 ChatScript 库的程序。这是我在名为 main.cpp 的文件中的代码: #include #include "common.h" using namespace std
我想在我的程序中外部使用 ChatScript。在documents它说: Embedding Step #1 First, you will need to modify `common.h and
假设我有一个 char,我想用一行代码将其 strcat() 转换为 char 数组。对于 [一个非实用的] 示例: strcat("ljsdflusdfg",getchar()); 或者我想做相反的
我有以下类型签名: *Main Lib> let f :: a -> a -> a -> a; f = undefined *Main Lib> let x :: Char; x = undefin
我正在学习如何在 C 中使用指针(使用 malloc 和 free),但我在这个练习中遇到了一些麻烦。我只想制作一个指针数组,我想在其中保存每个单词的方向。然后我想为一个特定的词做一个 free(),
我有一个字符*: char* version = "10.5.108"; 我想通过字符分隔符获取两个新的 char*。 char delimiter = '.'; 执行以下代码后: printf("|
最近在学习Cpp,今天在学习使用Clion做测试的时候,发生了奇怪的事情。 这是我的代码 int main() { char c = 'b'; char carr[1]{'a'};
我对 c 很陌生,我正在审查一些代码。我遇到了这个: static char * fromDataType; static char * toDataType; static char * fromR
我有一个像这样的动态结构: struct network { int count; char** ips; } 如果我知道每个字符串数组都是 16 个字节(即 INET_ADDRSTR
我有一个旧程序,其中使用了一些库函数,但我没有那个库。 所以我正在使用 C++ 库编写该程序。在那个旧代码中有一些函数是这样调用的 *string = newstrdup("这里有一些字符串"); 字
我正在编写一个函数,该函数接受 ArrayList,然后将每个 char[] 复制到另一个增加长度的 char[] 中,然后将新的 char[] 添加到新的 ArrayList 中。当我尝试复制数组时
我正在寻找 map >并生成每个可能的 map从它。 我知道这可能会占用大量内存并需要一些时间。 每个map需要包含每个字母 a-z,并映射到唯一的 a-z 字符。 IE。啊bjcp迪EVfh嘎血红蛋
#define NAME_LEN 20 #include "stdio.h" #include "stdlib.h" #include "string.h" #pragma warning(disab
所以我必须创建一个函数来找到一对带有第一个字母并返回第二个字母的函数。 我实际上找到了一个答案,但是使用 map 功能却找不到。 lookUp :: Char -> [(Char, Cha
我最近接受采访并要求写mystrcat(*s1, *s2, *s3) 其中s1 和s2 是源字符串连接结果由 s3 给出。有人告诉我,不要担心 s3 的内存分配,并假设 s1 和 s2 不是空/无效字
今天我与一位同事讨论了他(对我来说)不寻常的“main”函数签名。他喜欢这样声明: int main(int argc, char* (*argv)[]) { printf("at index
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: What's the difference between new char[10] and new cha
通常字符串文字是 const char[] 类型。但是当我把它当作其他类型时,我得到了奇怪的结果。 unsigned char *a = "\355\1\23"; 使用此编译器会抛出警告“初始化中的指
我是一名优秀的程序员,十分优秀!