如何使此代码在字符串而不是内存地址中输出实际文本

如何使此代码在字符串而不是内存地址中输出实际文本

#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);

}


打印必须在函数中完成,并以指向数组的指针作为参数。我一直在尝试解决这个问题。任何帮助,将不胜感激。

最佳答案

您忘记了取消引用指针:

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


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

printpart1(int length, string *printpart1)
                                         ^


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

printpart1(ARRAY_SIZE, part1PTR);
                       ^

关于c++ - 如何使此代码在字符串而不是内存地址中输出实际文本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54858462/

10-12 00:02