| | |
Write a function to swap two values using pointers
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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
{
}
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
{
}
0
#3 Oct 27th, 2009
For starters, you might want to move this above the main() function like:
C Syntax (Toggle Plain Text)
void swap(int *, int *); //prototype with a pointer parameter int main() { ... }
Last edited by gerard4143; Oct 27th, 2009 at 5:38 pm.
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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;
}
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;
}
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#5 Oct 27th, 2009
You are very close. Let's see what your swap function does.
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..
C Syntax (Toggle Plain Text)
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..
2
#7 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.
void swap(int *num1, int *num2)
{
int tempvar1;
tempvar1 = *num1;
*num1 = *num2; /* learn the difference between num1 and *num1 */
*num2 = tempvar1;
}![]() |
Similar Threads
- How To Write A Function (C++)
- Function to return two values (C++)
- Swap two numbers with pointers (C++)
- how to delete some text from a txt file?? (C++)
- JAVA Expert Help me.. Write a function that accepts an array of integers and returns (Java)
- writing swap function in Python (Python)
- Sorting arrays of pointers with function? (C)
- The improtance of replacing values in pointers (C)
Other Threads in the C Forum
- Previous Thread: pass excel sheet in linux C using libxls
- Next Thread: C language help required (2 Questions here )
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






