从函数返回的指针打印值(C ++)

时间:2021-05-14 15:58:29

I have a function that returns a pointer to an array, and I want to print each value in the array

我有一个函数返回一个指向数组的指针,我想打印数组中的每个值

When I run it, it prints

当我运行它时,它会打印出来

int* merge_sort(int arr[], int size) {
    if (size <= 1) {
        return &arr[0];
    }

    int size1 = size/2;
    int arr1[size1];
    for (int i = 0; i < size1; i++) {
        arr1[i] = arr[i];
    }

    int size2 = size-size1;
    int arr2[size2];
    for (int i = 0; i < (size2); i++) {
        arr2[i] = arr[i+size1];
    }

    int p1 = 0;
    int p2 = 0;
    int sorted[size];

    while (p1 < size1 && p2 < size2) {
        if (arr1[p1] < arr2[p2]) {
            sorted[p1+p2] = arr1[p1];
            p1++;
        } else {
            sorted[p1+p2] = arr2[p2];
            p2++; 
        }
    }

    while (p1 < size1) {
        sorted[p1+p2] = arr1[p1];
        p1++;
    }

    while (p2 < size2) {
        sorted[p1+p2] = arr2[p2];
        p2++;
    }

    cerr << "sorted: ";
    for (int& i : sorted) {
        cerr << i << ",";
    }
    cerr << endl;

    return &sorted[0];
} 

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
int main()
{
    int N;
    int horses[N];

    cin >> N; cin.ignore();
    for (int i = 0; i < N; i++) {
        int Pi;
        cin >> Pi; cin.ignore();
        horses[i] = Pi;
    }

    int *sorted = merge_sort(horses, N);

    cerr << "~ " << sorted[0] << " " << sorted[1] << " " << sorted[2] << endl;

    //cerr << "~~ " << sorted << " " << *sorted << " " << endl;

    cerr << "^ " << *(sorted + 0) << endl;
    cerr << "^ " << *(sorted + 1) << endl;
    cerr << "^ " << *(sorted + 2) << endl;

    for (int i = 0; i < N; i++) {
        //cerr << "i " << i << endl;
        // cerr << "SORTED?? " << sorted[i] << endl;
        cerr << "value: " << *(sorted + i) << " ";
        //cerr << "*** " << *(sorted) + i << endl;

    }
    cerr << endl;
}

When I run it, it prints

当我运行它时,它会打印出来

sorted: 5,8,9, ~ 5 8 9 ^ -5728 ^ 942815029 ^ 6297320 value: 959592096 value: 0 value: -157570874

排序:5,8,9,~5 8 9 ^ -5728 ^ 942815029 ^ 6297320价值:959592096价值:0值:-157570874

Why is my for loop not printing the values "5, 8, 9"? How can I fix it so that it does?

为什么我的for循环不打印值“5,8,9”?我怎样才能修复它呢?

(Edited to be more detailed. Also, I realize my merge sort is wrong but I'm just trying to get it to return something I can use right now ^.^)

(编辑得更详细。另外,我意识到我的合并排序是错误的,但我只是想让它返回我现在可以使用的东西^。^)

2 个解决方案

#1


0  

Make sure that you returned correct pointer, for example (if pointer should point to values):

例如,确保返回正确的指针(如果指针指向值):

std::cerr << "\n" << sorted << " "<< &values << "\n";

should return same values:

应该返回相同的值:

003DFD64 003DFD64

#2


0  

Seems ok. Check your sort function

好像没问题。检查排序功能

cpp.sh/4rrk

// Example program
#include <iostream>
#include <algorithm>

const size_t N = 3;

int* sort(int arr[]) {
    // Sort arr and it should be equal to [5, 7, 8]
    std::sort(arr, arr+N);
    return arr;
}

int main() {
    int values[N];
    values[0] = 7;
    values[1] = 5;
    values[2] = 8;

    for (size_t i = 0; i < N; ++i) {
        std::cout << *(values + i) << " ";
    }
    std::cout << std::endl << values[0] << " " << values[1] << " " << values[2] << std::endl;

    int *sorted = sort(values);

    std::cout << "It is same arrays: " << values << " " << sorted << std::endl;

    for (size_t i = 0; i < N; ++i) {
        std::cout << *(sorted + i) << " ";
    }
    std::cout << std::endl << sorted[0] << " " << sorted[1] << " " << sorted[2] << std::endl;

    return 0;
}

Output:

7 5 8 
7 5 8
It is same arrays: 0x7329bfdc1d90 0x7329bfdc1d90
5 7 8 
5 7 8

#1


0  

Make sure that you returned correct pointer, for example (if pointer should point to values):

例如,确保返回正确的指针(如果指针指向值):

std::cerr << "\n" << sorted << " "<< &values << "\n";

should return same values:

应该返回相同的值:

003DFD64 003DFD64

#2


0  

Seems ok. Check your sort function

好像没问题。检查排序功能

cpp.sh/4rrk

// Example program
#include <iostream>
#include <algorithm>

const size_t N = 3;

int* sort(int arr[]) {
    // Sort arr and it should be equal to [5, 7, 8]
    std::sort(arr, arr+N);
    return arr;
}

int main() {
    int values[N];
    values[0] = 7;
    values[1] = 5;
    values[2] = 8;

    for (size_t i = 0; i < N; ++i) {
        std::cout << *(values + i) << " ";
    }
    std::cout << std::endl << values[0] << " " << values[1] << " " << values[2] << std::endl;

    int *sorted = sort(values);

    std::cout << "It is same arrays: " << values << " " << sorted << std::endl;

    for (size_t i = 0; i < N; ++i) {
        std::cout << *(sorted + i) << " ";
    }
    std::cout << std::endl << sorted[0] << " " << sorted[1] << " " << sorted[2] << std::endl;

    return 0;
}

Output:

7 5 8 
7 5 8
It is same arrays: 0x7329bfdc1d90 0x7329bfdc1d90
5 7 8 
5 7 8