r/cprogramming • u/StressingPlant • Oct 13 '26
editing int array in function not changing array in main, but it worked for char array?
edit: thank you all so much for the help! my code works now and i understand pointers a little better!
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;
}
6
Upvotes
15
u/lehmagavan Oct 13 '26
You're passing by value.
6
u/SmokeMuch7356 Oct 13 '26
So I assume you're doing something like this:
C passes all function arguments by value; when you call
cardMove, the expressionsarr[i]andarr[j]are evaluated, and the resulting values are copied tocard1andcard2.IOW, the formal arguments
card1andcard2are different objects in memory fromarr[i]andarr[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:
card1andcard2are still different objects in memory fromarr[i]andarr[j], but instead of getting the values stored in those array elements, they get their addresses:The expressions
*card1and*card2act as kinda-sorta aliases forarr[i]andarr[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.
7
u/tstanisl Oct 13 '26
There is no int array in your function.
1
u/cjmarquez Oct 13 '26
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.
2
u/Kadabrium Oct 13 '26
Are you saying you passed char* card1 and then modified card1[i]
2
u/Niklaus9 Oct 14 '26
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/RDGreenlaw Oct 13 '26
You could pass the array, and 2 indices to the function, then swap array elements. If the entire array is passed then the function receives a pointer to the array. Then swap values using a temporary int or computation to complete the swap, then return.
callswap: void callswap(*b, lft, rt){ int temp; temp = b[lft]; b[lft] = b[rt]; b[rt] = temp; return;
Just be sure to use the correct declaration for your swapping function to match what you are sending. The biggest difference between a string array and an int array is the size of the elements. Otherwise
1
u/Classic-Rate-5104 Oct 14 '26
C functions use call-by-value so this is a very fundamental element of the language
1
u/Syntax-Tactics Oct 14 '26
The values are changed inside the function, but are not being returned because they are passed by value. The string works, because it is a pointer and the operation works on what it is pointed to, so you "see" the change outside the function.