Thread: Using Pointers

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Using Pointers


    I am very bad at interpreting programming questions, but I can program decently.

    I have an assignment to write a program to swap the contents of two memory locations using a function and the swapping performed by the function will appear in the main program after the calling the function.

    So this is what I wrote

    #include <stdio.h>

    void main(void)
    {
    int first_num = 10;
    int second_num = 50;

    int *local_int;
    int x = 10;
    int y = 50;
    local_int = x;
    x = y;
    y = local_int;


    printf("The value of first_num is %d", first_num);
    printf("\nThe value of second_num is %d\n", second_num);

    local_int = &first_num;

    printf("\nIn the function first_num is %d", x);

    local_int = &second_num;

    printf("\nIn the function second_num is %d\n", y);

    printf("\nThe value of first_num is %d", first_num);
    printf("\nThe value of second_num is %d\n", second_num);
    }

    It works that isn't the problem but apparently it isn't right cause my teacher emailed me: Write a swap function. Make sure to call the swap function from main.

    I honestly have no clue what that means so if anyone could explain it or write out a shell on what it is supposed to look like I would appreciate it.

    Thank You!!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,223
    Rep Power
    2222
    Please use code tags when you post code here. HTML strips out extra white space, so code loses its indentation and becomes unreadable. Using code tags keeps that from happening.

    It's simple.
    [code] insert your formatted code here [/code]

    Here's what your code listing looks like with code tags (I retrieved the original indenting via the Reply button):
    Code:
    #include <stdio.h>
    
    void main(void)
    {
        int first_num = 10;
        int second_num = 50;
    
        int *local_int;
        int x = 10;
        int y = 50;
        local_int = x;
        x = y;
        y = local_int;
    
    
        printf("The value of first_num is %d", first_num);
        printf("\nThe value of second_num is %d\n", second_num);
    
        local_int = &first_num;
    
        printf("\nIn the function first_num is %d", x);
    
        local_int = &second_num;
    
        printf("\nIn the function second_num is %d\n", y);
    
        printf("\nThe value of first_num is %d", first_num);
        printf("\nThe value of second_num is %d\n", second_num);
    }
    Your teacher said it all: write a function. That function will take the addresses of two int variables (AKA "pointers") and it will use them to swap the values in the two variables.

    Writing a function is a basic skill that you need to learn. I'm sure it's covered in your textbook. The only special concern here is that you need to pass pointers to it in order to change the variables that you're passing to it. You have demonstrated that you already know how to declare pointers and to get the address of a variable, so the hard part is already out of the way.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Code:
     #include <stdio.h>
    
    void swap(int x, int y);
    
    void main(void)
    {
        int first_num = 10;
        int second_num = 50;
    
    
        printf("The value of first_num is %d", first_num);
        printf("\nThe value of second_num is %d\n", second_num);
    
        int (*functionPtr)(int,int);
        functionPtr = &swap;
    
        functionPtr = &first_num;
        printf("\nIn the function first_num is %d", first_num);
    
        functionPtr = &second_num;
        printf("\nIn the function second_num is %d\n", second_num);
    
        printf("\nThe value of first_num is %d", first_num);
        printf("\nThe value of second_num is %d\n", second_num);
    }
    
    void swap(int x, int y)
    {
        int local_int;
    
        local_int = x;
        x = y;
        y = local_int;
    }
    I know how to use functions, but the problem is getting the function to work with the pointer. I tried using a function pointer and I tried to first call the function 'swap' and then use a pointer right after it and that did not work either.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,223
    Rep Power
    2222
    This is what I wrote (emphasis added):
    Originally Posted by dwise1_aol
    That function will take the addresses of two int variables (AKA "pointers") and it will use them to swap the values in the two variables.
    You did not do that. What will happen in your code is that the values of the two arguments that you pass will be copied into the function's local variables, your function will swap the values of those copied local variables, and when you return from that function those copies will no longer exist and the original variables whose values you wanted to swap will remain unchanged.

    As I told you, you need to pass the addresses of the two variables to the function so that that function can use those two addresses to swap the values of the original variables. That is a basic common skill in C, which is why you need to learn it and the swap function is a perfect example to learn from.

    In swap, change x and y from int to int pointers. And when you use them within the function, dereference the pointers.

    There are such things as function pointers, but that is entirely different from what you need to do.

    Also, instead of just saying "it doesn't work", describe what it is doing as opposed to what you expect it to do. The latter gives us something to work with while the former is you expecting us to read your mind. We cannot read your mind.

IMN logo majestic logo threadwatch logo seochat tools logo