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

时间:2016-09-23 07:46:35

标签: c++ arrays pointers

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

当我运行它时,它会打印

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;
}

当我运行它时,它会打印

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

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

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

2 个答案:

答案 0 :(得分:0)

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

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

应返回相同的值:

003DFD64 003DFD64

答案 1 :(得分:0)

似乎没问题。检查排序功能

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;
}

输出:

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