在C ++中通过指针还原char数组的问题

我很清楚这方面的说明效率很低/几乎已经过时。我实际上使用字符串开发了一个简单得多的解决方案,但是出于教育目的,我的大学决定要求使用指针和char数组而不是字符串来解决此问题。

基本上,我需要传递一个预定义的char数组(一个特定的短语),并将其返回反向。

到目前为止,我的输出是绝对乱码(有些字符甚至无法在此处键入),所以我认为我必须做错了什么,但我不明白为什么。

为了进行实验,我尝试在函数体末尾手动将* ptr_cha分配为类似“ Hello”的名称,但是随后遇到类型转换错误。如果我手动将temp [0]分配为“ hello”,并跳过 for 循环,然后在最后简单说* ptr_cha [0]等于temp [0],我仍然会得到胡言乱语。 而且,如果我尝试在函数开始时输出clone的内容(在说它等于ptr_cha [0]之后),它会说内容是“ d”,即原句中根本没有的字母。 / p>

这是我的完整代码:

#include <iostream>
#include <wchar.h>
#include <locale.h>
#include <array>

using namespace std;

void invertChar(char *ptr_cha[]);

int main()
{
   setlocale(LC_ALL,"");
   char sentence[] {"Pablito clavó un clavito que clavito clavó Pablito"};
   char *ptr_sentence[] {nullptr};
   *ptr_sentence = sentence;
   invertChar(ptr_sentence);
   cout << ptr_sentence[0];
};

void invertChar(char *ptr_cha[]) {
   char clone[] = {""};
   char temp[] =  {""};
   ptr_cha[0] = clone;
   int length = sizeof(clone)/sizeof(*clone);
   int j = length;
   for(int i = 0; i < length; i++) {
    temp[i] = clone[j];
    j--;
   };
   *ptr_cha[0] = temp[0];
};

如前所述,该想法是将句子设置为倒置形式并显示在输出中。

还有一个附带的问题:为什么在c ++中计算char数组的长度是如此复杂/冗长?对于字符串,有一个简单的方法,在这里您必须执行整个“技巧”,将数组的大小除以其引用的大小。我什至看不到数组的存储大小除以其指针的存储大小如何返回数组的长度...

maoyink 回答:在C ++中通过指针还原char数组的问题

让我分解一下每个步骤中发生的事情。如果您使用的是IDE,我强烈建议您调试程序并查看每个步骤中发生的情况。 (在您的代码中,我假设您想反转“句子”,而不返回其反转版本的副本。)

在这里不使用

wchar.harray,您可以删除这些行。 除此之外,您不应该将字符串传递给类似的函数。请参阅this的操作方法。

#include <iostream>
#include <wchar.h> // Not used.
#include <locale.h>
#include <array> // Not used.

using namespace std;

void invertChar(char *ptr_cha[]);

在这里,您只需将sentence传递给invertCharptr_sentence是不必要和奇怪的(char* ptr_sentence就足够了,因为现在它是一个包含指向{{ 1}},然后将char的地址存储在其第一个插槽中。

sentence

您正在将int main() { setlocale(LC_ALL,""); char sentence[] {"Pablito clavó un clavito que clavito clavó Pablito"}; char *ptr_sentence[] {nullptr}; *ptr_sentence = sentence; invertChar(ptr_sentence); cout << ptr_sentence[0]; }; void invertChar(char *ptr_cha[]) { char clone[] = {""}; char temp[] = {""}; 分配给clone,现在ptr_cha[0]引用了ptr_cha[0]而不是clone。此时,您无法到达sentence

sentence

下面的第一行将更具描述性,例如: ptr_cha[0] = clone; 。这会将int length = sizeof(clone) / sizeof(clone[0])的大小除以其第一个元素的大小,基本上是为您提供clone拥有的元素数。除以元素大小非常重要,因为如果clone使用某些东西来存储长度不超过1个字节的字符(如clone,该怎么办?您可以使用此技巧获取任何数组的元素数。

char

请记住, int length = sizeof(clone)/sizeof(*clone); int j = length; for(int i = 0; i < length; i++) { temp都是空字符串,更准确地说,它们有1个元素,而clone字符表示字符串的结尾。 在循环的第一次运行中,您正在执行以下操作:\0temp[0] = clone[1]没有第二个元素(索引1)。此时,您正在访问数组之外​​的内容,并将其分配给clone,在那里它将被解释为temp,从而导致某些“乱码”。

char

总体而言,我建议您研究一下指针及其工作方式,因为它们可能会有些棘手和令人困惑。


有效的C(++)实现:

    temp[i] = clone[j];
    j--;
   };
   *ptr_cha[0] = temp[0];
};

仅用于比较的C ++实现:

#include <iostream> // cout
#include <locale.h> // setlocale
#include <string.h> // strlen

void reverse(char* string)
{
    // Check whether our pointer really points to something or not.
    if (string == nullptr) return;

    // 'strlen' returns the size of a '\0' terminated character sequence (including the '\0').
    // We subtract 1 from the length because we don't want to swap the terminating
    // '\0' character with the first one.
    const int length = strlen(string) - 1;
    for (int i = 0,j = length; i < j; ++i,--j) {
        const char temp = string[i];
        string[i] = string[j];
        string[j] = temp;
    }
}

int main()
{
    setlocale(LC_ALL,"");
    char sentence[] = "Pablito clavó un clavito que clavito clavó Pablito";
    reverse(sentence);
    std::cout << sentence << '\n';
    return 0;
}
本文链接:https://www.f2er.com/3154663.html

大家都在问