#include <stdio.h>
void swap1(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
return;
}
int main()
{
int x = 1, y = 2;
swap1(x,y);
printf("%d %d",x,y);
return 0;
}
I don't understand why without Pointer these will not work but with Pointer it will work normally. I have search about it on Google and saw a few question about it but I still don't understand it after reading all the answers.
but this code work normally without using Pointer
#include <stdio.h>
int padovan(int n);
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
printf("Result : %d",padovan(n));
}
int padovan(int x)
{
if(x == 0 || x == 1 || x == 2)
return 1;
else if(x > 2)
return padovan(x-2) padovan(x-3);
}
CodePudding user response:
void swap1(int a, int b)
{
int tmp
tmp = a;
a = b;
b = tmp;
return;
}
You're passing a
and b
by value, which means, when you pass a
and b
, the compiler makes a copy of a
and b
, not changing the actual value of a
and b
If you want to make this to work, you have to use some pointers:
void swap(int *a, int *b) {
int tmp;
tmp = *a; // save the value at address x
*a = *b; // put y into x
*b = tmp ; // put tmp into y
return;
}
int padovan(int x)
{
if(x == 0 || x == 1 || x == 2)
return 1;
else if(x > 2)
return padovan(x-2) padovan(x-3);
}
This code is fine. padovan()
returns an int
and you're printing that. You're not modifying any arguments in padovan()
.