Function implementation on two-number swap - c

Why doesn't this program compile with a warning message that "uninitialized pointers were used"? If I change pTemp to an int variable instead of an int pointer variable, is it okay? Please explain why.
void Swap(int *x, int *y) {
int *pTemp;
pTemp = x;
x = y;
y = pTemp;
}

Here would be a simple example of swap without using ptemp as pointer but as simple local int as it would be easier to think. Having c as an int variable is not more right or more wrong, it is just different. If u have a ptemp int, then u should work with the content of the variables, which means that u have to access the addess of &x using *x as i used inside the swap. If u worked with ptemp pointer then u should work by memory addresses which is overkill for this example I think.
#include <stdio.h>
void Swap(int *x, int *y)
{
int c;
c = *x;
*x = *y;
*y = c;
}
int main()
{
printf("Hello World\n");
int x = 3;
int y = 5;
Swap(&x,&y);
printf("X: %d, Y: %d", x,y);
return 0;
}

As posted, the code swaps the values of the function arguments, which has no effect outside the function. You should instead dereference the pointers to swap the values they point to, which requires a temp variable with type int.
Here is a modified version:
void Swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}

If you change a pointer variable to a variable, the function loses the ability to swap two numbers
void Swap(int *c, int *d) {
int *temp;
temp = *c;
*c = *d;
*d = temp;
}

Related

Why isn't this swap program working in C?

I'm completely new to C and I'm trying to create a basic swap program, can someone please explain why the code below isn't working.
#include <stdio.h>
void swap (int *p1, int *p2);
int *temp = *p1;
*p1 = *p2;
*p2 = temp;
int main ()
{
int x = 10;
int y = 20;
int *p1 = &x, *p2 = &y;
swap (p1, p2);
printf ("x: %d, y: %d\n", x, y);
}
Thanks in advance.
int *temp = *p1; will not compile (this is a constraint violation and must result in a compiler diagnostic): you are assigning an int (other than 0) to a pointer type. And that's not allowed.
Write int temp = *p1;, fix the obvious typos in the swap function - give it a body! - and all will be well.
The void is a sub-process, it must be enclosed in braces and must not end in a semicolon.
on the other hand as the community says int * temp = * p1; is a constaint violation, it must be int temp = * p1;
Nor was exchanging values, it showed only as when they were entered.
Finally, the main returns an int, you must specify that you return a 0, that is done with return 0;
The whole program would be like that
#include <stdio.h>
void swap (int *p1, int *p2){
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main ()
{
int x = 10;
int y = 20;
swap (p1, p2);
printf ("x: %d, y: %d\n", x, y);
return 0;
}

Pointers and Address in C

I write a C program. I have 2 pointers *a and *b. Now, I impose *a = 20 and *b = 10. After that I impose a = b in subroutine but the value seem doesn't change. I expect that *a = 10 and *b = 10. Please help me find a solution for this. Thank you.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void Copy(int *a1, int *a2)
{
a1 = a2;
}
void Test()
{
// a = 20
int *a = (int *)malloc(sizeof(int));
*a = 20;
// b = 10
int *b = (int *)malloc(sizeof(int));
*b = 10;
Copy(a, b); // a = b
printf("\na = %d", *a);
printf("\nb = %d", *b);
}
int main(int argc, char *argv[]){
Test();
fflush(stdin);
printf("\nPress any key to continue");
getchar();
return 0;
}
Function arguments are passed by value. So inside the function Copy you are modifying a copy of the pointer not the original pointer you had in the Test function. So when you leave the Copy function the pointers outside are not modified.
Change the Copy to receive a int **, so you can change inside the value of pointers
void Copy(int **a1, int **a2)
{
*a1 = *a2;
}
And call the function in this way:
void Test() {
...
Copy (&a, &b);
...
}
If you want to assign the values, such that after the call of Copy *a == *b holds, then you'd have to change your function as follows:
void Copy(int *a1, int *a2)
{
*a1 = *a2;
}
If you want to assign the pointers, such that after the call of Copy a==b holds, then you have to pass a pointer to the pointers:
void Copy(int **a1, int **a2)
{
*a1 = *a2;
}
void Test() {
...
Copy (&a, &b);
...
}
Note that a==b also implies that *a == *b.

Trying to swap multiple values in a user entered array

I am trying to take a user entered array and swap the 0th value with the 7th, 8th value with the 3rd and the 0th with the 14th value. I have to use a function to swap which is where i think i messed up. When i compile it says to few arguments for function swap. also, its my 1st time using pointers so i wouldn't be surprised if there errors with that,. this is what i have, thanks!
#include <stdio.h>
void swap (int a[15], int *p, int *q, int *r ,int *s , int*t) {
int temp;
p = &a[0];
q = &a[7];
r = &a[8];
s = &a[3];
t = &a[14];
temp = *p;
*p = *q;
*q = temp;
temp = *r;
*r = *s;
*s = temp;
temp = *t;
*t = *p;
*p = temp;
}
int main (int argc, char *argv[]) {
int a[15], i;
printf(" Enter 15 integers: ");
for (i=0; i <15; i++)
swap(a);
printf(" Swapped array:\n %d", a[15]);
return 0;
}
Its not an error related to pointers.
You called swap() with one argument, like this swap(a);
And while defining it you have more then one arguments. Like
void swap (int a[15], int *p, int *q, int *r ,int *s , int*t) {
...
You do not need those extra arguments in the function definition.
Just change it to,
void swap (int a[15]) {
int temp;
int* p = &a[0];
int* q = &a[7];
int* r = &a[8];
int* s = &a[3];
int* t = &a[14];
...
A much better approach would be define a general function swap() and call it with proper required arguments multiple times when required.
void swap(int *p, int *q)
{
int tmp = *p;
*p = *q;
*q = tmp;
}
Call the above API something like
swap (&a[0], &a[7])
swap (&a[8], &a[3])
...
..

swap function didn't work

Why this swap method didn't work
void swap(int *x,int *y){
int *temp;
temp = x;
x = y;
y = temp;
}
Why? I think it's same as the common one..
C passes function arguments by value: you are only swapping copies of pointers.
If you want to swap two int:
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
You are swapping the addresses stored in temporary pointers on the stack, not the values stored in the memory they point to. You want to do this instead:
void swap(int *x,int *y){
int temp = *x;
*x = *y;
*y = temp;
}
x and y behave just like local variables.
your code is swapping x and y values, not the values they point to.

Pass By Reference Using C

I understand that when you pass by reference through a function in C, the parameters of the function take in the address of the pointer that will be modified. I am extremley boggled on why this example of pass by reference is not working. Can anyone point me in the right direction....
This should output a swap but when i compile the swap does not occur why is this pass by reference not working?
#include <stdio.h>
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
getchar();
getchar();
return 0;
}
You forgot to dereference your pointers inside the function. Thus you end up reassigning the local pointer values rather than changing the actual value being pointed to and it has no effect.
So, use the * dereference operator:
int temp = *i;
*i = *j;
*j = temp;
I'm wondering why you didn't get any compiler warning/error. You need to dereference your reference in the function:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
The reason is i and j inside swapnum() are addresses to original variables when the function is called. So when you use only i or j, you're getting the address of the variable, not the content. Here is an idea of what is going on:
int a = 10;
int b = 20;
-----------------
0x1000 | 10 | <-- a
-----------------
-----------------
0x1004 | 20 | <-- b
-----------------
swapnum(&a, &b);
Then, inside swapnum(int *i, int *j):
-----------------
0x2000 | 0x1000 | <-- i (*i == 10)
-----------------
-----------------
0x2004 | 0x1004 | <-- j (*j == 20)
-----------------
In the swapnum function, you are only assigning the variables i and j which are local to this function. This won't have any effect outside of this function. You should try:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
It's (hopefully) crashing because in this function:
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
i and j are pointers, and temp is an int. You cannot assign a pointer to an int. If you want to swap the values in i and j do this:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
You want:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
In swapnum, you are swapping the pointers (which are also int values). Try this instead:
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
Your implementation of swapnum() is a bit improper. The function takes 2 (int *) parameters. That is, i and j are integer pointers storing references of a and b. when you do int temp = i; you are actually assigning a pointer to integer variable (which is incorrect) and then instead of swapping the values, the code snippet plays around with addresses. This is what you need
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}

Resources