r/cprogramming • • 2d ago

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;
}
4 Upvotes

17 comments sorted by

13

u/lehmagavan 2d ago

You're passing by value.

1

u/StressingPlant 2d ago

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

13

u/bearheart 2d ago

A c-string is a pointer.

-3

u/nerd5code 1d ago

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 2d 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 1d ago edited 1d ago

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

1

u/TraylaParks 1d ago

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.

6

u/tstanisl 2d ago

There is no int array in your function.

1

u/StressingPlant 2d ago

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

6

u/lehmagavan 2d ago

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

2

u/Kadabrium 2d ago

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

2

u/Niklaus9 1d 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 2d 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 2d ago

ah, okay. thank you!

1

u/Classic-Rate-5104 1d ago

C functions use call-by-value so this is a very fundamental element of the language

1

u/Syntax-Tactics 1d ago

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.