Changing a variable in another function using a pointer

I have C code and I am not allowed to modify the structure. I have an array called arr in my main function:

int arr[4] = {1,2,3,4}

      

I need to change the values ​​of this array in another function. It is called inside the main function using:

change_array(arr);

      

The change_array method is implemented in another .c file. I am allowed to make changes inside the change_array function.

void change_array(void *the_array)
{

}

      

In this function, I want to change the values ​​from arr. I found out that I can get the first value of an array using:

int first_value = *(int*)the_array;

      

And I can get the location of the first value using:

int location = the_array;

      

I tried to change the values ​​with:

*the_array = 1;

      

but that didn't solve my problem. Any ideas?

+3


source to share


2 answers


In the C programming language, the address of an array is the address of the first element.

  • * the_array points to the first element (value 1 in your case). To traverse the array, you must use [] or pointer arithmetic .

  • You also need the size of the array so that you can iterate through the array without going out of bounds.



Here's an example:

void change_array(void *the_array, int size)
{
   int a = 0, b = 0, i = 0;

   // how to set and iterate
   for(i = 0; i < size; i++)
   {
       ((int*)the_array)[i] = i;
   }

   // how to get
   a = ((int*)the_array)[0];
   b = ((int*)the_array)[1];
   b = *((int*)the_array + 1); // same as the line before. Example of pointer arithmetic
}

      

+2


source


You can create an int pointer in your change_array () function and change the values ​​as shown below. This works because the pointer you passed is only the memory address of the first element in the array. Therefore, by assigning another pointer - of the appropriate type - to the same location, you can directly change the original values. That is, any changes you make to that memory location in your function will also change the original array, since they occupy the same memory location.

As pointed out by others, you will have no way of knowing the size of the array they are passing to you. You mentioned that you cannot change the invocation code, so I guess this would not be an option for you. Assuming your function will only be called with a fixed size, it will be fragile, but it will still work for you.



#include "stdio.h"

void change_array( void * the_array );

int main()
{
int arr[ 4 ] = { 1, 2, 3, 4 };

printf( "Before:\n%d | %d | %d | %d\n", arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ] );
change_array( arr );
printf( "After:\n%d | %d | %d | %d\n", arr[ 0 ], arr[ 1 ], arr[ 2 ], arr[ 3 ] );

return( 0 );
}

void change_array( void * the_array )
{
int * arr_ptr = the_array; //NOTE: This is implicitly converting the void * to an int *
printf( "In the changing function:\n%d | %d | %d | %d\n", arr_ptr[ 0 ], arr_ptr[ 1 ], arr_ptr[ 2 ], arr_ptr[ 3 ] );
arr_ptr[ 0 ]  = 5;
arr_ptr[ 1 ]  = 6;
arr_ptr[ 2 ]  = 7;
arr_ptr[ 3 ]  = 8;
}  

      

You can see it in action here: http://codepad.org/6PXBeZlY

+3


source







All Articles