【问题标题】:Why does memcpy return string is never equal to the same string present in an array?为什么 memcpy 返回字符串永远不等于数组中存在的相同字符串?
【发布时间】:2018-12-11 07:20:28
【问题描述】:

我正在使用简单的字符串匹配和搜索。 为什么*arr 永远不等于point 尽管它们的值(为什么我)是相同的?我很困惑这是什么原因?它与字符串有关还是有其他原因?任何帮助,将不胜感激。如果问题不清楚,我很抱歉。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int search(char ** arr, int size, char *point);

int main()
{
    char *array[5]={0};
    char original[50];
    char toSearch[50];
    char *point;     
    int size=5,  searchIndex,i;

    /* Copy a string into the original array */
    strcpy(original, "why this is not equal");

    /* Copy the first 10 characters of the original array into the newcopy    array*/
    point = memcpy(toSearch, original, 10);

    /*string to search*/     
    array[2]="why this i";

    searchIndex = search(array, size, point);

    if(searchIndex == -1)
    {
        printf("%s does not exists in array \n", point);
    } else
    printf("%s is found at %d position.", toSearch, searchIndex + 1);

    return 0;
}

int search(char ** arr, int size, char *point)
{
    int index = 0;
    // Pointer to last array element arr[size - 1]
    char ** arrEnd = (arr + size - 1);

    /* Why *arr!=point is never false,
       even when both are giving out same values (why this I)?
    */
    while(arr <= arrEnd && *arr!=point)
    {
        printf("%s == %s", *arr,point);
        arr++;
        index++;
    }

    if(arr <= arrEnd)
    {
        printf("%s after found \n",*arr);
        return index;
    }

    return -1;
}

输出:

(null) == why this i 
(null) == why this i   
why this i == why this i  
(null) == why this i   
(null) == why this i 
why this i does not exists in array

谢谢

【问题讨论】:

  • 你是在问为什么array-of-char类型的变量的地址和你复制到它的字符串字面量的地址不一样?
  • 我不得不重新扫描五次才能发现search() 的定义。您的格式几乎似乎旨在隐藏该功能... ;-) 我的错误,但我会修复您的缩进以使其对其他人更容易。
  • 不是*arr != point 比较“指针”而不是它指向的字符串吗?你不应该使用字符串比较吗?例如strcmp()
  • @Yunnosch 答案已经存在:stackoverflow.com/a/8004250/7477557 但我对 StackOverflow 还是很陌生,不知道如何将此线程标记为已回答。如果你不介意,我让你去做。谢谢。
  • @xdrm-brackets 在问题下方尝试 lgrey 单词“flag”。它应该说类似“...的重复”,如果你点击那里,你可以提供一个链接到问题相同的问题,并有一个接受或赞成的答案。现在就随意做吧。但是,我不相信您提供的链接可以作为此链接的原始链接。

标签: c arrays memcpy


【解决方案1】:

我假设您在问为什么类型为字符数组的变量的地址与您复制到它的字符串文字的地址不同。

这是一个棘手的问题,因为,请原谅我这么说,不清楚是什么让您期望任何变量的地址在复制某些内容后会有所不同。

考虑用相同的字符串文字填充的不是一个而是两个不同的 char 数组的示例。

char toSearch1[50];
char toSearch2[50];
char *point1;
char *point2;

point1 = memcpy(toSearch1, original, 10);
point2 = memcpy(toSearch2, original, 10);
/* or, depending on what to you is more convincingly copying the same literal */
point2 = memcpy(toSearch2, toSearch1, 10);

之后,这两个数组仍然是两个不同类型的char数组变量,memcpy返回目标地址,即两个不同数组的地址。
请想象一下这两个数组现在如何具有相同的地址。

如果你跟进

strcpy(original1, "is this in one or both arrays?");

您是否会期望这两个数组再次具有不同的地址?

如果这不能回答您的问题,那么您对变量的指针和地址的理解与大多数程序员根本不同,我建议您通过查找指针和地址的教程来修改它。

(请注意,对于您所询问的内容的解释,我将跳过关于检查与数组大小相关的长度的讨论,使用更安全的字符串复制版本,memcpy 不能正确地为空终止复制的字符串。然而,所有这些都很重要用于编写可靠的代码,例如 Lundin 正确地提醒我们。)

【讨论】:

  • 你没有提到memcpy(toSearch1, original, 10); 切断了一个子字符串但没有添加空终止符,这是原始代码中最大的错误之一。
  • @Lundin 好点,假设您允许,我在回答的附注中使用了它。谢谢。
【解决方案2】:

在 C 中,字符串只是以空字符结尾的字符数组。作为普通数组,当直接用于表达式时,它们会衰减为指针。因此,当您编写 str1 == str2 时,您只是将指针与它们的第一个元素进行比较。

只有当两个指针指向同一个对象(相同地址)时,它们才相等。这就是为什么标准库提供strcmp 来比较以空结尾的字符串,并提供memcmp 来比较已知大小的任意缓冲区的原因。

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多