我用C编写了这段小代码来测试C中的memcmp() strncmp() strcmp()函数.
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *word1="apple",*word2="atoms";
if (strncmp(word1,word2,5)==0)
printf("strncmp result.\n");
if (memcmp(word1,word2,5)==0)
printf("memcmp result.\n");
if (strcmp(word1,word2)==0)
printf("strcmp result.\n");
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释我的差异,因为我对这三个功能感到困惑吗?
我的主要问题是我有一个文件,我在其中标记它的行,问题是,当我将文件中的"atoms"字标记时,我必须停止标记化的过程.
我第一次尝试strcmp()但不幸的是当它到达文件中放置"原子"这个词的时候它没有停止并且它继续,但当我使用它memcmp()或strncmp()它停止时我很高兴.
但后来我想,如果有一个字符串,其中前5个字母是a,t,o,m,s,并且这些字母后面跟着其他字母.
不幸的是,我的想法是正确的,因为我使用上面的代码测试它,通过初始化word1为"atomsaaaaa"和word2原子,memcmp()并strncmp()在if语句中返回0.另一方面,strcmp()它没有.似乎我必须使用strcmp().
这是一段代码:
import System.Environment
myReverse :: [a] -> [a]
myReverse [] = []
main = print (myReverse [])
Run Code Online (Sandbox Code Playgroud)
当我使用GHC编译时,我收到以下错误:
[1/1]编译Main(problem5_myReverse.hs,problem5_myReverse.o)problem5_myReverse.hs:6:8:因使用
print' The type variablea0 而产生的(显示a0)的实例不明确可能的修复:添加修复这些的类型签名type variable(s)注意:有几个潜在的实例:实例Show Double - 在GHC.Float' instance Show Float -- Defined inGHC.Float'实例中定义(Integral a,Show a)=> Show(GHC.Real.Ratio a) - 在GHC.Real' ...plus 23 others In the expression: print (myReverse []) In an equation formain'中定义: main = print(myReverse [])
但是,当我从更改签名myReverse::[a]->[a],以myReverse::[Int]->[Int]源代码是没有任何问题的编译
有人可以告诉我如何保留一般签名[a] -> [a]但让它适用于空的整数列表?