#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);
Related
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;
}
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;
}
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.
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
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.