r/cprogramming • • 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

17 comments sorted by

15

u/lehmagavan Oct 13 '26

You're passing by value.

1

u/StressingPlant Oct 13 '26

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

12

u/bearheart Oct 13 '26

A c-string is a pointer.

-3

u/nerd5code Oct 14 '26

No, a string is a sequence of characters terminated by a null=0 byte. It's often referred to by a pointer, but the two are wholly distinct jobbies.

6

u/SmokeMuch7356 Oct 13 '26

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 Oct 14 '26 edited Oct 14 '26

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

1

u/TraylaParks Oct 14 '26

It can be confusing especially if the variable in 'main' has the same name as the variable in your function, I used to give my students this example to help them make sense of things:

#include <stdio.h>

static void functionOne(int x)
{
   x = 5;
}
    
int main()
{
   int x = 3;

   functionOne(x);

   printf("%d\n", x);

   functionOne(42);
}

Now, one could perhaps sensibly argue that the first function call might change the "real" variable x, but in the second - we're passing a literal value! The idea is to make it crystal clear that the 'x' in your function is something else entirely from the similarly-named variable back in main.

7

u/tstanisl Oct 13 '26

There is no int array in your function.

1

u/StressingPlant Oct 13 '26

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

6

u/lehmagavan Oct 13 '26

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

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.

1

u/StressingPlant Oct 13 '26

ah, okay. thank you!

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.

main:
int a[]={0, 1, 2, 3, 4}
int left=1, right=3;
callswap(a, left, right);
// The array becomes
//  0,3,2,1,4

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.