【问题标题】:How can I make this code output the actual text in the strings rather than the memory address?如何使此代码输出字符串中的实际文本而不是内存地址?
【发布时间】:2019-02-25 01:49:43
【问题描述】:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void printpart1(int length, string *printpart1[18]) {
    int dummy;
    for (int i = 0; i < length; i++)
        cout << (printpart1 + i) << endl;

    cin >> dummy;

}

int main() {
    const int ARRAY_SIZE = 18;
    int dummy;
    string part1[ARRAY_SIZE] = { "An example of a career associated with computer studies is a Software Engineer. To become a",
        "Software Engineer, a minimum of a Bachelor’s Degree in Software Engineering is required which ",
        "could be obtained by first going into Engineering in post-secondary and choosing Software ",
        "Engineering as your major. Alternatively you could get a degree in Computer Sciences or another ",
        "closely related field. While a Bachelor’s Degree is enough to get some jobs, some employment ",
        "opportunities may require a Master’s Degree. Tuition for Engineering at the University of Western ",
        "Ontario for Canadian students is around $6000 and requires four years for a Bachelor’s degree. ",
        "This means with tuition alone it will cost around $24000 for the minimum qualifications. This price is  ",
        "much higher factoring in books, living, food etc. An employee in this field makes an average of ",
        "$80000 yearly and could get a variety of benefits depending on the company employing them. An  ",
        "average day for a software engineer varies by company but generally seems to begin with a few  ",
        "hours of good work, followed by a break to walk around and talk to coworkers about either personal  ",
        "things or work related affairs followed by more coding. Some days there are interviews with clients  ",
        "where a software engineer and the client communicate and discuss details of the project. The ",
        "majority of the average workday of a Software Engineer is spent programming. ",
        "https://study.com/articles/Become_a_Computer_Software_Engineer_Education_and_Career_Roadmap.html",
        "https://www.univcan.ca/universities/facts-and-stats/tuition-fees-by-university/ ",
        "https://www.coderhood.com/a-day-in-the-life-of-a-software-engineer/"
    };

    string *part1PTR = part1;

    printpart1(ARRAY_SIZE, &part1PTR);

}

打印必须在以数组指针为参数的函数中完成。多年来,我一直在试图弄清楚如何使这项工作发挥作用。任何帮助将不胜感激。

【问题讨论】:

    标签: c++ arrays string pointers


    【解决方案1】:

    你忘了取消引用指针:

    cout << *(printpart1 + i) << endl;
            ^
    

    此外,您将参数声明为 指针数组,您应该删除数组部分:

    printpart1(int length, string *printpart1)
                                             ^
    

    ...并将函数调用更改为

    printpart1(ARRAY_SIZE, part1PTR);
                           ^
    

    【讨论】:

    • *(printpart1 + i) 将输出一个指针,因为&amp;part1PTR 提供了一个指向 stringprintpart1() 的指针。您将需要*(*printpart1 + i) 书面形式。
    • @DavidC.Rankin 难道我改变函数参数的建议不起作用?
    • 是的,您的第二部分很好,删除了数组,但您仍然需要处理来自main()string **。 (你需要把part1PTR放在那里)我猜OP意识到*(printpart1 + i)只是printpart1[i]
    【解决方案2】:

    请在下方查看。

    cout << (*printart1)[i]<< endl;
    

    【讨论】:

      【解决方案3】:

      您似乎对如何声明和使用数组 string指针数组 string 感到困惑。

          string part1[ARRAY_SIZE] = { "...", "...", ...
      

      您将part1 声明为字符串数组。但是,如何将该数组传递给printpart1() 似乎有些混乱。您提供的声明为:

      void printpart1(..., string *printpart1[18]) {
      

      其中printpart1 指定参数printpart1 将是一个指向字符串的指针数组。然后您将printpart1() 称为:

      printpart1(ARRAY_SIZE, &part1PTR);
      

      您已将part1PTR 声明为指向字符串的指针。通过传递part1PTR地址,您为printpart1() 提供了一个指向string 的指针。

      为了获得实际的打印字符串,您首先必须取消引用printpart1(例如*printpart1)以获得指向字符串的指针,然后再应用任何偏移和取消引用,例如*(*printpart1 + i)

      例如,以下将提供所需的字符串输出:

      void printpart1(int length, string *printpart1[18]) {
          int dummy;
          for (int i = 0; i < length; i++)
              cout << *(*printpart1 + i) << endl;
      
          cin >> dummy;
      
      }
      

      注意:*(*printpart1 + 1) 等价于(*printpart1)[i]。任何对您更有意义的形式。通常使用第二种形式)

      简单地传递数组

      现在所有这些都过于复杂了,就像将数组本身传递给 printpart1() 一样简单,例如如果您将功能更改为:

      void printpart1(int length, string *printpart1) {
          int dummy;
          for (int i = 0; i < length; i++)
              cout << printpart1[i] << endl;
      
          cin >> dummy;
      }
      

      您不再需要part1PTR,只需使用以下命令调用您的函数:

          printpart1 (ARRAY_SIZE, part1);
      

      仔细检查,仔细考虑,如果您还有其他问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-13
        • 2021-12-30
        • 2020-01-18
        • 2019-06-08
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多