Write a function to swap two values using pointers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 5
Reputation: LostnC is an unknown quantity at this point 
Solved Threads: 0
LostnC LostnC is offline Offline
Newbie Poster

Write a function to swap two values using pointers

 
0
  #1
Oct 27th, 2009
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
{


}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: ylchen is an unknown quantity at this point 
Solved Threads: 0
ylchen ylchen is offline Offline
Newbie Poster
 
0
  #2
Oct 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 371
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz
 
0
  #3
Oct 27th, 2009
For starters, you might want to move this above the main() function like:
  1. void swap(int *, int *); //prototype with a pointer parameter
  2.  
  3. int main()
  4. {
  5. ...
  6. }
Last edited by gerard4143; Oct 27th, 2009 at 5:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: LostnC is an unknown quantity at this point 
Solved Threads: 0
LostnC LostnC is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
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;



}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: ylchen is an unknown quantity at this point 
Solved Threads: 0
ylchen ylchen is offline Offline
Newbie Poster
 
0
  #5
Oct 27th, 2009
You are very close. Let's see what your swap function does.

  1. int tempvar1; // This line creates an integer tempvar1. Good.
  2.  
  3. tempvar1 = *num1; // This line assigns the VALUE of the integer POINTED TO by the pointer num1 into the INTEGER tempvar1. Good.
  4. num1 = *num2; // This line assigns the VALUE of the integer POINTED TO by the pointer num2 into the POINTER num1.
  5. 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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: LostnC is an unknown quantity at this point 
Solved Threads: 0
LostnC LostnC is offline Offline
Newbie Poster
 
0
  #6
Oct 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,038
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
2
  #7
Oct 28th, 2009
Originally Posted by LostnC View Post
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; 
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #8
Oct 28th, 2009
As mentioned, you were trying to assign an integer to a pointer rather than dereferencing the pointer to store the value.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC