Hello. I am a beginner. I have searched the past threads, but I haven't found any help. My assignment says to write a function to swap 2 values using pointers. Write a main function that inputs 2 numbers. Have it call the swap function if the numbers are NOT in descending order - include a loop to allow the user to enter 3 sets of numbers.

I have been working on this for hours. The deeper I get, the more I confuse myself.

Here's the code that I have. It is nowhere close to complete.
Can someone please give me some help? Any help is appreciated!

#include<stdio.h> 
int main()
{
void swap(int *, int *);    //prototype with a pointer parameter
int count;
int num1, num2;
int swapvalue;
int holdScreen; // holdscreen is used to keep the ouput window open

   for (count = 1; count <4; count++)  //begin loop
       printf("Enter the first number -> ");
       scanf("%d", &num1);
       printf("Enter the second number -> ");
       scanf("%d", &num2);

    if (num1 < num2 )
    {
     swap(&num1, &num2);                //Function Calling Statement    
    }
    else
    printf("No swap needed")

    printf("The address that will be passed for num1 is %d\n\n", &num1);
    printf("The address that will be passed for num2 is %d\n\n", &num2);
    count ++


   scanf("%d", &holdScreen);
   return 0;
}

void swap(int *num, *num2)    //Function Header Line
{


}

That's a good start.

swap is generally done with the following algorithm:

create a temporary variable, assign it the value of A
assign the value of B into A
assign the value of the temporary variable into B

Then I suggest you compile because there are a bunch of syntax errors in there ;)

For starters, you might want to move this above the main() function like:

void swap(int *, int *); //prototype with a pointer parameter

int main()
{
...
}

I tried to do the swap, but I isn't working.
I just don't quiet understand what I need to do.
I cleaned up some of my syntax problems, but I am still lost.

#include<stdio.h> 

void swap(int *, int *);    //prototype with a pointer parameter

int main()
{

int count;
int num1, num2;
int swapvalue;
int holdScreen; // holdscreen is used to keep the ouput window open

   for (count = 1; count <4; count++)  //begin loop
       printf("Enter the first number -> ");
       scanf("%d", &num1);
       printf("Enter the second number -> ");
       scanf("%d", &num2);

    if (num1 < num2 )
    {
     swap(&num1, &num2);                //Function Calling Statement    
    }
    else
    printf("No swap needed");

    printf("The address that will be passed for num1 is %d\n\n", &num1);
    printf("The address that will be passed for num2 is %d\n\n", &num2);
    count ++;


   scanf("%d", &holdScreen);
   return 0;
}

void swap(int *num1, int *num2)    //Function Header Line
{
     int tempvar1;


    tempvar1 = *num1;
    num1  = *num2;
    num2 = tempvar1;



}

You are very close. Let's see what your swap function does.

int tempvar1; // This line creates an integer tempvar1. Good.

tempvar1 = *num1; // This line assigns the VALUE of the integer POINTED TO by the pointer num1 into the INTEGER tempvar1. Good.
num1 = *num2; // This line assigns the VALUE of the integer POINTED TO by the pointer num2 into the POINTER num1. 
num2 = tempvar1; // This line assigns the VALUE of the integer tempvar1 into the POINTER num2.

Do you see the problem now? Pointers hold addresses, and instead of writing your values into the addresses pointed to by the pointers, you are replacing the address with the value..

I'm sorry, but I don't understand.
I keep looking at my code, but I really don't know what I need to do.

I'm sorry, but I don't understand.
I keep looking at my code, but I really don't know what I need to do.

void swap(int *num1, int *num2)
{
    int tempvar1;

    tempvar1 = *num1;
    *num1 = *num2; /* learn the difference between num1 and *num1 */
    *num2 = tempvar1; 
}

As mentioned, you were trying to assign an integer to a pointer rather than dereferencing the pointer to store the value.

After 5 years either he will have got the answer or have given up. Please don't re-awaken old dead threads without a good reason.

But spamming your blog is a good reason, right? ...Right?

sound of crickets

;p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.