swap function didn't work - c

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.

Related

Function implementation on two-number swap

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;
}

C - Passing pointer as an argument to a swap function

#include<stdio.h>
void swap (char *x, char *y)
{
char *t = x;
x = y;
y = t;
}
int main()
{
char *x = "geeksquiz";
char *y = "geeksforgeeks";
char *t;
swap(x, y);
printf("(%s, %s)", x, y);
t = x;
x = y;
y = t;
printf("n(%s, %s)", x, y);
return 0;
}
I would expect that the original pointers would swap but it isn't the case here even though i pass the pointer to the function, The reason for it is that it makes local pointers ?
How do i swap the original pointers using the function?
The reason for it is that it makes local pointers ?
Yes. x, y and t are all local variables and x and y are copies of the original pointers.
How do i swap the original pointers using the function?
Swapping pointers probably doesn't make any sense, but if you some reason need to do it, then:
void swap (char** x, char** y)
{
char* t = *x;
*x = *y;
*y = t;
}
If you want to swap pointers you need to pass references to them (pointer to pointer)
void swap (char **x, char **y)
{
char *t = *x;
*x = *y;
*y = t;
}
usage:
swap(&x, &y);
or you can define the macro:
#define SWAP(x,y,type) do {type t; t = (x); (x) = (y); (y) = t; } while(0)
usage:
SWAP(x,y, char *);
or
#define SWAP(x,y) do {const void * t; t = (const void *)(x); (x) = (y); (y) = t; } while(0)
usage:
SWAP(x,y);

Can I swap two matrixes with a function just interchanging its base address, without swapping element by element?

Summary
It is just a doubt that came when studying about pointers, can I swap two matrixes using a function without needing to swap element by element in it?
I've created 2 matrixes dynamically, using malloc, and declared its elements` values in main(). So, I could swap like this:
(Swapping element by element)
void swapMatrix(int** a, int** b){
for(int i = 0; i< row; i++){
for(int j = 0; j< col; j++){
int tmp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = tmp;
}
}
}
...
int main() {
...
print(M1);
swapMatrix(M1, M2);
print(M1);
...
(Swapping row by row)
void swapMatrix(int** a, int** b){
int* tmp = *a;
*a = *b;
*b = tmp;
for (int i = 0; i<col; i++){
int* tmp = *(a+i);
*(a+i) = *(b+i);
*(b+i) = tmp;
}
}
...
int main() {
...
print(M1);
swapMatrix(M1, M2);
print(M1);
...
The Attempts
But is there a way to just change the matrix pointer to pointer address? I've attempted the following codes:
I)
void swapMatrix(int** a, int** b){
int** tmp = a;
a = b;
b = tmp;
}
...
int main() {
...
print(M1);
swapMatrix(M1, M2);
print(M1);
...
It compiled but when I verified its elements, none of them really swapped
II)
void swapMatrix(int*** a, int*** b){
int*** tmp = a;
a = b;
b = tmp;
...
int main() {
...
print(M1);
swapMatrix(&M1, &M2);
print(M1);
...
Again, it compiled but when I verified its elements, none of them really swapped.
How should I implement the function in order to swap the entire matrix just changing a pointer to pointer address? Is it possible? Actually, I don't really understand why the I) attempt didn't work. I would really appreciate if you could explain the details.
No, that's not possible. You can't choose the addresses of variables or modify them either.
If M1 and M2 are pointers to your matrices then swapMatrix in option 1 would work except the pointer operations are a bit off. You need to swap the value pointed at by a and b (i.e. the address of each matrix) but you're just swapping the value of a and b(i.e. changing which pointer to matrix your local parameter is pointing to). Try something like:
void swapMatrix(int** a, int** b){
int* tmp = *a;
*a = *b;
*b = tmp;
Approach 3 is really close:
void swapMatrix(int*** a, int*** b){
int** tmp = *a;
*a = *b;
*b = tmp;
...
int main() {
...
print(M1);
swapMatrix(&M1, &M2);
print(M1);
...
But you need to dereference the pointer, or otherwise you are just swapping around the values of local variables in the swap function. I just explained it here.
Don't worry we all get confused with pointers from time to time.

Pointers and interchange with C

I have a general idea of how to do this, as you can see by the code I did below. The only problem I am having is finishing the interchange part. Basically, what I am trying to do is move the value of the lowest variable into the 1st variable, the second middle value to the 2nd variable, and the biggest to the 3rd variable.
I know I do this with the interchange and using temp somehow, but how would I complete that, because with three values temp would get overridden somehow. What am I missing? So basically a = 4.0, b = 7.0, c = 1.0, c (1.0) needs to go into a, a (4.0) needs to go into b, and b (7.0) needs to go into c.
Thanks!
#include <stdio.h>
void interchange(double * x, double * y, double * z);
int main(void)
{
double a = 4.0, b = 7.0, c = 1.0;
printf_s("Originally a = %d, b = %d, and c = %d.\n", a, b, c);
interchange(&a, &b, &c);
printf_s("Now, a = %d, b = %d, and c = %d.\n", a, b, c);
return 0;
}
void interchange(double * x, double * y, double * z)
{
double temp;
temp = *z;
*y = *z;
* = temp
// what am I missing here? I cant get my head around this above ^^^
}
Thanks for the guidance!
Something like:
void swap(double* first, double* last){
double temp = *first;
*first = *last;
*last = temp;
}
void interchange(double * x, double * y, double * z){
if(*x > *y) swap(x, y);
if(*y > *z) swap(y, z);
if(*x > *y) swap(x, y);
}
the simplest way is:
if (*x > *y) {
temp = *x; // store value of x
*x = *y; // overwrite x with y
*y = temp; // overwrite y with x (which is in temp)
}
// now we sure that x <= y
if (*y > *z) {
temp = *z;
*z = *y;
*y = temp;
}
// now we sure that x <= z and y <= z, but we don't know x/y relationships
if (*x > *y) {
temp = *x;
*x = *y;
*y = temp;
}
// now x <= y <= z

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