-1

I want to read from user input some numbers and then display them 5 on each line. My code is like this:

#include <iostream>
using namespace std;

const int INPUT_SIZE = 7;

void printValues(int *b) {

    int counter = 0;
    while (counter < INPUT_SIZE) {
        cout << *(b+counter) << " ";
        if ((counter + 1) % 5 == 0 && counter>0) {
            cout << endl;
        }

        counter++;
    }
    cout << endl;
    system("pause");
}

int * readValues() {
    int b[INPUT_SIZE];
    for (int i = 0; i < INPUT_SIZE; i++) {
        cout << "Enter element on position: " << i << ": ";
        cin >> b[i];
    }
    return b;
}

int main() {

    int* b;
    b = readValues();
    printValues(b);

    return 0;
}

However, when I try to print them, I get some weird numbers which I think are memory locations. How do I print an array and also how do I write a function that returns an array? I have little experience with pointers, as I only coded in Java so far. Help much appreciated.

1 Answers1

2

Your array, b, is a local variable inside the function readValues.

When readValues exits, the array is destroyed.

The pointer which you return from readValues is invalidated as soon as the function returns. Trying to use it (e.g. by passing it to printValues) is incorrect (formally, it causes undefined behaviour).

You may have caused some confusion by giving the same name b to a pointer variable in main as you do to the array b in readValues. They are entirely separate variables.

If you want to use the same array in your two functions, you need to make sure it lives in a scope which ensures it lives as long as you need it to. This could be by making it a local variable in main.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Good point. How do I do that? More explicitly, how do I return a usable array from the readValues() function? – Tiberiu Dorian Moşescu May 08 '16 at 08:29
  • @TiberiuDorianMoşescu `std::array readValues(){...}` – 463035818_is_not_an_ai May 08 '16 at 08:30
  • You cannot pass or return arrays by value in C++. If you create the array in main, then you can pass a pointer to it to both of your functions. Alternatively, you can put your array into a struct. You can pass and return structs containing arrays to and from functions. – CB Bailey May 08 '16 at 08:31
  • @tobi303 namespace std has no member "array" says the almighty Visual Studio – Tiberiu Dorian Moşescu May 08 '16 at 08:33
  • @CharlesBailey Some code with some logic and flow to it would be much appreciated, instead of theory. No need for words if the code speaks for itself :D – Tiberiu Dorian Moşescu May 08 '16 at 08:37
  • @TiberiuDorianMoşescu it is declared in `` (since C++11, use `std::vector` from `` if you cant use C++11). – 463035818_is_not_an_ai May 08 '16 at 08:39
  • @TiberiuDorianMoşescu "No need of words if the code speaks for itself" code only answers are usually really bad. No offense, but if you want to know how to do it right next time you better care about some "theory" instead of asking others to write code for you – 463035818_is_not_an_ai May 08 '16 at 08:41
  • @tobi303 If I knew how to do it right, I wouldn't have asked the question, right? If someone tells me how to do it right, I'll do it right the next times. It's bad enough that C/C++ code, in general, looks like junk thrown in a corner of the room, especially for beginners. And I didn't ask for code, I asked for a small correction to the code I posted, it's a small huge difference. – Tiberiu Dorian Moşescu May 08 '16 at 09:27
  • @TiberiuDorianMoşescu Still no offense, we are just trying to help: right, code with undefined behaviour like yours is junk that should be thrown in some corner. If you want to know how to avoid undefined behaviour you need to read more than just some code snippets. – 463035818_is_not_an_ai May 08 '16 at 09:32
  • ... and this answer explains quite well what is the problem with your code – 463035818_is_not_an_ai May 08 '16 at 09:32
  • @tobi303 any good fun reads that aren't 1000 pages long? – Tiberiu Dorian Moşescu May 08 '16 at 09:39
  • @TiberiuDorianMoşescu [this answer](http://stackoverflow.com/a/6445794/4117728) might help (still no code :P) – 463035818_is_not_an_ai May 08 '16 at 09:56
  • @tobi303 this is the epitome of no code, but I'll read it and love it and learn from it like it's the face of a homeless person – Tiberiu Dorian Moşescu May 08 '16 at 10:31