# How does this function work?

• 05-10-2011
TheWhiffet
How does this function work?
OK so a friend of mine showed me a function which reverses a string of numbers. But I don't understand how it works for the part with the variable 'count'. If I change it to anything other than 'count/2' it gives a different answer. But, he did nothing with 'count' except declare it in the main function. Why does it work? And how does it work? What value will 'count' have if no value has been assigned to it in the first place?

Please assume that the variables are already declared in a function that calls it.
Code:

```void reverse (int *num, int count) {         int i;  for(i=0;i<(count/2);i++)  {                 *(num+i)=*(num+i) + *(num+9-i);                 *(num+9-i)=*(num+i)-*(num+9-i);                 *(num+i)=*(num+i)-*(num+9-i);  }         }```
I'm pretty new to all of this and the simpler explanations would be very helpful! Thanks in advance to those who will help. :D
• 05-10-2011
GReaper
Quote:

Originally Posted by TheWhiffet
What value will 'count' have if no value has been assigned to it in the first place?

If it is declared in the main or any function, noone knows! If it's made global, it's assigned zero.

EDIT: Why is he using pointer arithmetics instead of easy indexing? To get to your nerves?!
• 05-10-2011
tabstop
If there is a variable named "count" in the main program, that doesn't really matter, since this count is going to be a different variable anyway. What matters is what gets passed in to the function when the function is called -- that's what value count gets when the function starts.

Granted, mixing the passed-in value count with the hard-coded 9 means bad things are going on anyway.
• 05-10-2011
mike65535
What's with the hard-coded '9' when you are seemingly passing in a "count"?