Why doesn't my program work?

Hi guys.

I was trying to make a function that reverses an array. First I wrote a for loop that reverses the array and it works. But after I put that for loop inside a function (called reverse) and called that function from main it doesn't work. It seems like the for loop only loops once through the array!

What am I missing here? Why doesn't the loop inside the function loop through all the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void reverse(int arr[]) {

    for (int i=0; i<(sizeof(arr)/sizeof(int))/2; i++){
        int temp = arr[i];
        arr[i] = arr[sizeof(arr)/sizeof(int)-1-i];
        arr[sizeof(arr)/sizeof(int)-1-i] = temp;
//        cout<<"runing for the "<<i<<" time";
    }
}


int main(){

    int array[]={1,2,3,4,5,6};


    reverse(array);


//    for (int i=0; i<(sizeof(array)/sizeof(int))/2; i++){
//        int temp = array[i];
//        array[i] = array[sizeof(array)/sizeof(int)-1-i];
//        array[sizeof(array)/sizeof(int)-1-i] = temp;
//        cout<<"runing for the "<<i<<" time"<<endl;
//    }


    for (int i=0; i<(sizeof(array)/sizeof(int)); i++){
        cout<<array[i]<<"   ";
    }



    return 0;
}



Thank you for your time and/or any replies.
Last edited on
You passed the array by value and not by reference.

void reverse(int& arr[])
Last edited on
When you pass an array to a function like that, it really gets passed as a pointer. Therefore the sizeof() on line 8 is returning the size of a pointer, not the full size of the array.

Change reverse() to take two parameters: the array and the number of elements N. Then change sizeof(arr)/sizeof(int) to N in all 3 places.

@cody0023. Thanks for the answer. I knew how passing by reference works but I was wondering why the for loop worked in main but not when ported to another function. And why the loop only run once.

@dhayden. It's mind blowing how 'books' fail to mention things as important as this. How can they leave out the fact that when you pass an array to function, the function only gets a pointer to that array.

I was looking for a way to use the array without the function knowing it's length. But I still can't figure out a way to make that work. Don't even know if it is possible.

Thanks for the answers though. :-)
Last edited on
I was looking for a way to use the array without the function knowing it's length.

You could use vector<int> instead. This is just one more reason to use vector instead of arrays.
closed account (E0p9LyTq)
I was looking for a way to use the array without the function knowing it's length. But I still can't figure out a way to make that work. Don't even know if it is possible.


Use vector<int> as dhayden suggested, or if you are really needing a fixed size use the new C++11 Standard Template Library (STL) std::array<> class template.

Using the STL containers lets you query the container for the size (the number of elements) of the container, even std::array<>.

Learning C++ from books is good, but there are not many true C++/C++11 books available to my knowledge. Most C++ books I have looked at underutilize the STL.

The (STL) really is the way to go when it comes to containers.
Topic archived. No new replies allowed.