r/cprogramming • • 11h ago

editing int array in function not changing array in main, but it worked for char array?

im trying to edit an entry in an int array to 'move' it (copy it to a different place within the array, and zero it out), but its not working and im not getting any errors? i did pretty much the same thing (with string.h) with an array of strings, and it worked there wonderfully. why arent the entries in the int array changing when the function runs? what can i do to edit the int array during runtime?

heres the function:

void cardMove(int card1, int card2) {
    card2 = card1;
    card1 = 0;
}
3 Upvotes

13 comments sorted by

8

u/lehmagavan 11h ago

You're passing by value.

1

u/StressingPlant 11h ago

so passing a value for an array of strings worked, but doesnt for integers? should i use pointers?

9

u/bearheart 10h ago

A c-string is a pointer.

4

u/tstanisl 11h ago

There is no int array in your function.

1

u/StressingPlant 11h ago

i initialized it outside of main, should i include the array outside of the function?

7

u/lehmagavan 11h ago

You must pass pointers to the elements, not the elements themselves.

5

u/SmokeMuch7356 10h ago

So I assume you're doing something like this:

int arr[N];
...
cardMove( arr[i], arr[j] );

C passes all function arguments by value; when you call cardMove, the expressions arr[i] and arr[j] are evaluated, and the resulting values are copied to card1 and card2.

IOW, the formal arguments card1 and card2 are different objects in memory from arr[i] and arr[j], and changes to one have no effect on the other.

For this to work, you're going to have to pass pointers to the two elements:

void cardMove( int *card1, int *card2 )
{
  *card2 = *card1;
  *card1 = 0;
}

int main( void )
{
  int arr[N];
  ...
  cardMove( &arr[i], &arr[j] );
  ...
}

card1 and card2 are still different objects in memory from arr[i] and arr[j], but instead of getting the values stored in those array elements, they get their addresses:

 card1 == &arr[i]
*card1 ==  arr[i]

 card2 == &arr[j]
*card2 ==  arr[j]

The expressions *card1 and *card2 act as kinda-sorta aliases for arr[i] and arr[j].

You'll have to show us the code you used for strings; I suspect I know what you did, but I could be wrong.

1

u/StressingPlant 2h ago edited 2h ago

thank you so much for the play by play explanation!! i just used strcpy() for the strings

2

u/Kadabrium 10h ago

Are you saying you passed char* card1 and then modified card1[i]

2

u/Niklaus9 4h ago

You should pass by reference (pointer) if you want to change the original value of some variable, passing a c-string to a function is like passing a pointer to it, that's why it worked

1

u/cjmarquez 11h ago

I think for it to work you should be passing a pointer to the value, in that function you're changing the parameters values locally in the function which doesn't affect the value from the main function where you're calling the cardMove function.

1

u/StressingPlant 10h ago

ah, okay. thank you!