比较从strdup函数返回的值与strncmp函数可以使用以下代码示例:
#include
#include
int main() {
char str1[] = "Hello";
char* str2 = strdup(str1);
// 使用strncmp函数比较str1和str2的前5个字符
int result = strncmp(str1, str2, 5);
if (result == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
// 释放str2的内存
free(str2);
return 0;
}
在上述代码中,我们首先使用strdup函数将str1的内容复制到str2中,然后使用strncmp函数比较str1和str2的前5个字符。如果它们相等,我们打印"str1 and str2 are equal",否则打印"str1 and str2 are not equal"。最后,我们使用free函数释放str2的内存。