Thread: How does this function work?

  1. #1
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011

    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.
    void reverse (int *num, int count)
    	int 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.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Quote Originally Posted by TheWhiffet View Post
    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?!
    Last edited by GReaper; 05-10-2011 at 06:56 AM.
    Devoted my life to programming...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    What's with the hard-coded '9' when you are seemingly passing in a "count"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to work on this function
    By void12 in forum C Programming
    Replies: 1
    Last Post: 02-07-2011, 12:16 PM
  2. oh, no! my function won't work.
    By student0806 in forum C Programming
    Replies: 5
    Last Post: 09-27-2010, 06:15 PM
  3. cant get my function to work!
    By scaven in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:08 PM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. Trying to work with Cin Function
    By ProgrammingDlux in forum C++ Programming
    Replies: 14
    Last Post: 01-24-2002, 03:43 PM