arrays - C++ 新手 - 无法获取指向数组的指针来工作

标签 arrays pointers cin

尝试创建指向用户输入的数组(字符串)的指针。

在这里,我在类中定义了指针和数组:

private:

char userWord[200];     // input word
char * userWordPtr; // Pointer to the beginning of the input word

这是读取信息的函数:

void WordFont::inputWord()
{

    cout << "What word would you like to draw?\n"
         << "The useable characters are: A E I O U Y B C D L M N R S T\n" ;
    cout << "Please enter your word: ";
    cin.get(userWord, 200);
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    userWordPtr = &userWord;

}

用户输入一个单词,我的代码根据该单词的字母执行某些操作(长 if-else 梯形图)。但是,在将 * Ptr 传递给 if-else 梯形图时,当我递增指针的位置时,我无法让 if-else 梯形图识别空 (/0) 字符。我还使用“&”引用 char 数组开头的地址时出现错误。我不明白为什么我会收到这个错误,以及为什么下面的 if-else 梯子不会退出,直到“i”到达内存中具有“/0”字符的任何随机位置。如果我执行像输入“AAA”这样简单的操作,它将远远超过“/0”字符所在的第四个位置。

这是循环:

void WordFont::buildOutputArray(char * Ptr)
{

    length = 0;
    bool switchLooper = true;
    int i = 0;

    do
    {
        if(*Ptr + i == 'a' || *Ptr + i == 'A')
            constructA();
        else if (*Ptr + i == 'e' || *Ptr + i == 'E')
            constructE();
        else if (*Ptr + i == 'i' || *Ptr + i == 'I')
            constructI();
        else if (*Ptr + i == 'o' || *Ptr + i == 'O')
            constructO();
        else if(*Ptr + i == 'u' || *Ptr + i == 'U')
            constructU();
        else if(*Ptr + i == 'b' || *Ptr + i == 'B')
            constructB();
        else if(*Ptr + i == 'c' || *Ptr + i == 'C')
            constructC();
        else if(*Ptr + i == 'd' || *Ptr + i == 'D')
            constructD();
        else if(*Ptr + i == 'l' || *Ptr + i == 'L')
            constructL();
        else if(*Ptr + i == 'm' || *Ptr + i == 'M')
            constructM();
        else if(*Ptr + i == 'n' || *Ptr + i == 'N')
            constructN();
        else if(*Ptr + i == 'r' || *Ptr + i == 'R')
            constructR();
        else if(*Ptr + i == 's' || *Ptr + i == 'S')
            constructS();
        else if(*Ptr + i == 't' || *Ptr + i == 'T')
            constructT();
        else if(*Ptr + i == '\0')
            switchLooper = false;
        i++;
    }while(switchLooper);


}

这可能是一个简单的新手错误,我相信您会注意到许多其他问题,但我非常感谢任何帮助。

谢谢, 汤姆

最佳答案

The user enters a word and my code does something (long if-else ladder) based on the letters of the word. But in passing a * Ptr to the if-else ladder, I can't get the if-else ladder to recognize the null (/0) character when I'm incrementing the position of the pointer.

实际上,您使用 *Ptr + i 所做的是:

  • 解除对 Ptr 指针的引用(您正在访问 Ptr 指向的地址处的值)
  • 然后您将 i 添加到地址 Ptr 包含的值

Serge给了你答案,你必须使用*(Ptr + i)来获取该地址处的字符。

在这里,您正在移动i“ block ”的Ptr指针。

I also get an error using the '&' to reference the address of the beginning of the char array. I can't figure out why I'm getting that error,

char(或其他)数组“就像”一个指针(有很多差异,但我让你很高兴发现它们),所以当你使用 str[i]*(str + i) 完全相同。

And why the if-else ladder below doesn't exit til 'i' gets to whatever random location in memory has a '/0' character. If I do something as simple as entering 'AAA', it will well past the 4th position where the '/0' character is.

你不应该再遇到这个问题了。

如果用户输入 200 个字符怎么办?

关于arrays - C++ 新手 - 无法获取指向数组的指针来工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994664/

相关文章:

arrays - 通过匹配另一个数组中元素的属性从数组中删除元素

c - 使用 qsort 对 C 中的字符串数组进行排序时出现段错误

c++ - cin 输入可以有多大?

c++ - C++ 中的 cin.ignore() 和 cin.clear()

c++ - do-while-loop 的问题

javascript - react : filter array: Can I work with elements that didn't pass the test?

c - 如何在C中复制多维字符串

c - 右箭头等于右箭头

c++ - 是否分配给临时对象未定义行为的字段?

c++ - 指向指针的指针导致崩溃