-1

I'm trying to make a program that puts the reverse of an array into another array. So, I made a function to do the reverse. And passed the array to be reversed by value. And the array used to store the reverse by reference. But when I run the program it crashes.

#include <stdio.h>
#include <conio.h>
#define size 50

void revarr (int num1[], int *num2)
{
    int i;
    for (i = 0; i<size; i++)
    {
     num2[3-1-i] = num1[i];
    }

}

int main()
{
    int num[] = {1,8,1};
    int reverse[3],x;
    revarr(num,reverse);
    for (x = 0; x<3; ++x)
        printf("%d ", reverse[x]);
    getch();
    return 0;
}

How can I fix this?

user3417785
  • 81
  • 1
  • 6
  • 1
    you're incrementing `i` to 49, at which point `num2[3-2-i]` will be `num2[-47]` -> negative offset is not allowed – Elias Van Ootegem May 23 '14 at 13:33
  • what is going on with num2[3-1-i]? – Matt Coubrough May 23 '14 at 13:35
  • you can fix this by putting some print statements between the lines of code so you can work out where it crashes. Then ask yourself "why does that line of code crash" and if you can't work that out then you can come to SO to ask for an answer. Do some investigation first. – dave May 23 '14 at 13:38

3 Answers3

0

First, there is no pass by reference in C.
Second, you are passing both of your arrays by pointer.
Third, Either change #define size 50 to #define size 3 or pass the array length to your function , otherwise it is wrong to access an array outside its boundary.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

What causes the crash is undefined behaviour. When passing an array to a function, that array decayes into a pointer (memory address) of the first element of that array. Fine, you can still use the [] operator on a pointer, but it's up to you to make sure that a_ptr[123] is still valid, and accessible memory.
If it isn't anything can happen. That's why it's called undefined behaviour.

If, say, the memory address of reverse[0] is 0x04, and sizeof(int) is 4, then reverse[-1] = 123 is the same as writing: *((int *) 0) = 123;. That is textbook undefined behaviour (dereferencing a NULL pointer!).
more on this matter

You are accessing memory you don't own, memory that is out of bounds: your arrays are defined as 3 long, but you're looping using the size macro (should be upper-case, BTW), which is 50. That is not allowed, hence the crash. Change the revarr function to take a third argument, specifying the length of the array to reverse, and make sure both arrays you pass are big enough to accommodate the data:

#include <stdio.h>
#include <stdlib.h>

void rev_array(int *to, int *from, size_t max)
{//arrays decay into pointers
    int i,j;
    for (i=max-1, j=0;j<max;--i,++j)
        to[j] = from[i];
}
int main ( void )
{
    int i,
        reverse[3],
        num[] = {1,2,3};
    rev_array(reverse, num, 3);
    for(i=0;i<3;++i)
        printf("%d -> %d\n", num[i], reverse[i]);
    return EXIT_SUCCESS;
}

As you can see here, it works perfectly.

Instead of hard-coding the 3, you can get the size of an array using sizeof, of course (note: sizeof works on arrays, not on pointers!):

rev_array(
    reverse,
    num,
    sizeof num/sizeof *num
    //or sizeof(num)/sizeof(num[0])
);
Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

It is not crashing because array index is negative. num2[3-1-i] boils down to *(num2+3-1-i) which is fine. The reason why it is crashing is because it is accessing protected area of the memory and the OS cannot allow that. Since num and reverse are allocated 3 integers each, you are going outside the array (in num1, index is greater than 2; in num2, index is less than 0) and you don't know what is outside the memory allocated for you. If it is accessing some protected area it will crash. If you're lucky, it may not crash. But you should not do this. As others have said, change size to 3 and it will be fine.

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33