I'm trying my luck at this swap function but I'm having problems.
My hope is that the "new num1" should be swapped for the value of num2, and vice versa.
Can anyone shove me in the right direction?
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
a = *b;
b = temp;
printf("Just checking if this badboy gets to the swapfunction.\n");
}
int main()
{
int num1 = 33;
int num2 = 45;
swap(&num1, &num2);
printf("A: %d\n", num1);
printf("B: %d\n", num2);
getchar();
return 0;
}
You need to deference the pointers:
int temp = *a;
*a = *b;
*b = temp;
Didn't your compiler warn you?
Some important things you should know about is:
int temp = *a; // Correct , temp stores value at the address of a
a = *b; //Incorrect, a is pointer that is used to hold address NOT values
Correction required is:
*a = *b; //Correct, now value at address of a is = value at address of b
Same mistake here also:
b = temp;//Incorrect, cause b is pointer used to hold address and NOT values
Correction required is
*b = temp;
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 see a code which says the following.
typedef struct dummy
{
int a;
int b[100];
} dummy_t;
typedef struct dummya
{
int a;
int b;
} dummya_t;
void * getptr(){
// return a pointer of a memory
}
void function(){
dummya_t*dstptr = getptr();
dummy_t *srcpt = (dummy_t*)(dstptr->b);
}
I can see dummy_t *srcpt = (dummy_t*)(dstptr->b); meaning as a pointer is pointing to another pointer but then *srcpt should be a double pointer right? Like **srcpt
Your code is not correct, you can not cast nor assign using pointers to objects of different types, for example:
struct a { int a, b; };
struct b { int a, b; };
are different objects even having the same members.
struct a x = {.a = 1, .b = 2};
struct b *y = &x; // wrong
I can see dummy_t srcpt = (dummy_t)(dstptr->b); meaning as a pointer
is pointing to another pointer but then *srcpt should be a double
pointer right? Like **srcpt
Your assumptions are not correct, a basic example using plain pointers to int:
#include <stdio.h>
int main(void)
{
int arr[] = {1, 2};
int *a = &arr[0];
int *b = &arr[1];
printf("a = %d b = %d\n", *a, *b);
int **ptr;
ptr = &a;
*ptr = &arr[1];
ptr = &b;
*ptr = &arr[0];
printf("a = %d b = %d\n", *a, *b);
return 0;
}
The output is:
a = 1 b = 2
a = 2 b = 1
As you can see, you use a double pointer (**ptr) when you want to change the contents of a or b (the address where it points to), a more useful example:
#include <stdio.h>
void swap(int **a, int **b)
{
int *temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int arr[] = {1, 2};
int *a = &arr[0];
int *b = &arr[1];
printf("a = %d b = %d\n", *a, *b);
swap(&a, &b);
printf("a = %d b = %d\n", *a, *b);
return 0;
}
Same output:
a = 1 b = 2
a = 2 b = 1
I can see dummy_t *srcpt = (dummy_t*)(dstptr->b); meaning as a pointer is pointing to another pointer but then *srcpt should be a double pointer right? Like **srcpt
No. That line means "treat the value of dstptr->b as though it were a value of type dummy_t *, and assign it to srcpt". It's not a pointer to a pointer.
The expression dstptr->b has type int, which cannot be directly assigned to a pointer type. (dummy_t *) is a cast, which tells the compiler to convert the type.
Like Vlad says in his comment, this code doesn't make any sense. It may compile, but just because it compiles doesn't mean it's right.
I started learning C this week and here is the first difficulty I'm encountering with pointers.
I would like to test this function:
void ft_ultimate_div_mod(int *a, int *b)
{
int *tmp;
if (b != 0)
{
*tmp = *a % *b;
*a = *a / *b;
*b = *tmp;
}
}
But I can't figure out the main using pointers. My program is not printing anything with:
int main(void)
{
int *a;
int *b;
*a = 5;
*b = 3;
ft_ultimate_div_mod(a, b);
printf("%d", *a);
printf("%d", *b);
return (0);
}
What can I do? Thx!
For starters it does not make sense to declare the variable tmp as having the type int *. The variable is used to store an object of the type int so it has to have the type int. Otherwise you are using an uninitialized pointer with an indeterminate value that results in undefined behavior of the program.
Also as the variable is used in the compound statement of the if statement then it should be declared in the blco scope of the if statement.
These declarations in main
int *a;
int *b;
als do not make sense.
What you mean is the following.
#include <stdio.h>
void ft_ultimate_div_mod(int *a, int *b)
{
if ( *b != 0 )
{
int tmp = *a % *b;
*a = *a / *b;
*b = tmp;
}
}
int main(void)
{
int a = 10;
int b = 3;
ft_ultimate_div_mod( &a, &b );
printf( "%d %d\n", a, b );
return 0;
}
The function ft_ultimate_div_mod excepts its arguments by reference because it tries to change their original values.
You declare int *tmp in your function. That is a pointer to an int sized bit of memory that can store an int. You never point it anywhere however so *tmp = *a % *b; will do a mod of a an b and then crash when it stores it using an uninitialized pointer.
Change the *tmp to just tmp so then your code becomes:
void ft_ultimate_div_mod(int *a, int *b)
{
int tmp;
if (a && b)
{
tmp = *a % *b;
*a = *a / *b;
*b = tmp;
}
}
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;
}
This question already has answers here:
Why does this code not swap the numbers? [duplicate]
(2 answers)
Closed 6 years ago.
I am asked to make a swap between 2 integers.
#include <stdio.h>
#include <stdlib.h>
void swaplol(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf("After the swap : a= %d, b=%d\n",a,b);
}
I dont see where the problems lies. Everything seems good synthax wise...
int main(void)
{
int a,b;
a = 666;
b = 998;
printf("Before the swap a = %d, b = %d\n",a,b);
swaplol(a,b);
return 0;
}
Thanks
You need to pass the address of the integer to the swap function, so that when the value is swapped, you get swapped a and b values back in main. You therefore have to change the swaplol function to accept pointers instead of ints.
#include <stdio.h>
#include <stdlib.h>
void swaplol(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("After the swap : a= %d, b=%d\n",*a,*b);
}
int main(void) {
// your code goes here
int a,b;
a = 666;
b = 998;
printf("Before the swap a = %d, b = %d\n",a,b);
swaplol(&a,&b);
printf("After the swap : a= %d, b=%d\n",a,b);
return 0;
}
The swap function should swap values of the original variables declared in main.
To do this the function has to accept the variables by reference.
And you should remove the output statement from the function and place it in main to demonstrate that the original variables were swapped.
For example
#include <stdio.h>
void swaplol( int *a, int *b )
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main( void )
{
int a, b;
a = 666;
b = 998;
printf( "Before the swap a = %d, b = %d\n", a, b );
swaplol( &a, &b );
printf( "After the swap : a = %d, b = %d\n", a, b );
return 0;
}
There is no need to include header <stdlib.h>
Use pointers:
void swaplol( int * a, int * b )
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int main(void)
{
int a, b;
a = 666;
b = 998;
printf( "Before the swap a = %d, b = %d\n", a ,b );
swaplol( &a, &b);
printf( "After the swap : a= %d, b=%d\n", a, b );
return 0;
}
Just add pointers.. i'm providing a code snippet without using a temp variable.
int main()
{
....
....
swaplol(&a,&b);
}
int swaplol(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
printf("a=%d,b=%d",a,b);
}
In C there is The call by value and call by address with help of the Pointers. There you can "simulate" a call by reference which is present in other Languages.
Any way by default, C uses call(pass) by value to pass arguments. This means that the code within a function cannot alter the arguments used to call the function.
Lets take the following example:
#include <stdio.h>
void foo( int x );
int main( void ){
int x = 5;
foo(x);
printf("X = %d\n", x);
}
void foo( int x ){
x = 10;
}
The output will be 5.
If you are using a good compiler with settings turned ON you will see this kind of warning:
program.c:14:15: error: parameter ‘x’ set but not used [-Werror=unused-but-set-parameter]
void foo( int x ){
^
Now what did just happen?
By definition, call(pass) by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter.
If you really need to modify a value you need to pass the address of that variable and use a pointer to point to it:
#include <stdio.h>
void foo( int *x );
int main( void ){
int x = 5;
foo(&x);
printf("X = %d\n", x);
}
void foo( int *x ){
*x = 10;
}
Now the Output will be 10.