Thread: Program about pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Program about pointers

    Hi, everyone. I have here a code of a program that interchanges the values of two int variables, x and y, by using pointers:

    Code:
    #include <stdio.h>
    void interchange(int * u, int * v);
    
    int main(void)
    {
        int x = 5, y = 10;
        printf("Originally, x = %d and y = %d.\n", x, y);
        interchange(&x, &y); //send addresses to function
        printf("Now, x = %d and y = %d.\n", x, y);
    
        return 0;
    }
    
    void interchange(int * u , int * v)
    {
        int temp;
    
        temp = *u; //temp = x    //temp gets value that u points to
        *u = *v;   //x = y
        *v = temp; //y = temp
    }
    The thing is: how are the values of x and y changed in the main function, if the funtion interchange() doesn't return anything? Is it because this one works with pointers that point to x and y, in such a way that these pointers are the "bridge" between the main() and the interchange() functions?

    Thanks in advance!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The thing is you are not swapping the pointers... you are swapping the values at the addresses in the pointers... The pointers in the function are copies of the ones in the main program... so the change is relected back to the calling function.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stdq View Post
    Hi, everyone. I have here a code of a program that interchanges the values of two int variables, x and y, by using pointers:
    ...
    The thing is: how are the values of x and y changed in the main function
    ...
    Is it because this one works with pointers that point to x and y, in such a way that these pointers are the "bridge" between the main() and the interchange() functions?
    Yes. Without a pointer, you pass the value stored in a variable. Through a pointer, you pass the value that represents where that varaible is stored in memory. Since you know where it is in memory, you can change what value it holds (since it's not declared as constant).

    It sounds like you need to reread whatever it is you read about pointers again.


    Quzah.
    Last edited by quzah; 09-08-2011 at 05:47 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by CommonTater View Post
    The thing is you are not swapping the pointers... you are swapping the values at the addresses in the pointers... The pointers in the function are copies of the ones in the main program... so the change is relected back to the calling function.
    Thanks!

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by quzah View Post
    Yes. Without a pointer, you pass the value stored in a variable. Through a pointer, you pass the value that represents where that varaible is stored in memory. Since you know where it is in memory, you can change what value it holds (since it's not declared as constant).

    It sounds like you need to reread whatever it is you read about pointers again.


    Quzah.
    So I am interchanging the values in the addresses of x and y?

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    For a quick review on pointers, read through Lesson 6- Pointers For a more thorough treatment of the subject I would suggest Prelude's Pointer Tutorial.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by quzah View Post
    It sounds like you need to reread whatever it is you read about pointers again.
    Quzah.
    Thanks; actually, I am still starting on this subject. I guess I'll have to read more about it.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by AndrewHunter View Post
    For a quick review on pointers, read through Lesson 6- Pointers For a more thorough treatment of the subject I would suggest Prelude's Pointer Tutorial.
    Thanks!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We had a good analogy going once before ... and here it is: Questions to understand malloc better


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Quote Originally Posted by quzah View Post
    We had a good analogy going once before ... and here it is: Questions to understand malloc better


    Quzah.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program Pointers Help
    By MetDude21 in forum C Programming
    Replies: 2
    Last Post: 03-13-2011, 01:57 PM
  2. need some pointers on program
    By noob2c in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 05:16 AM
  3. Pointers program
    By Jamina in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 02:31 PM
  4. I need help with pointers in my program
    By CheeseMonkey in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2001, 09:31 PM