Here's the function in question. Why does x not change, despite having ptr reference it's memory address? Similarly why doesn't y change?
#include <stdio.h>
int func (int a, int *b)
{
int *ptr = &a;
*ptr += 5;
ptr = b;
*ptr -= 3;
return a;
}
int main ()
{
int x = 2;
int y = 8;
int ret = func (x, &y);
printf ("x: %d\n", x);
printf ("y: %d\n", y);
printf ("ret: %d\n", ret);
return 0;
}
Edit: yes y does change. Sorry.
int func (int a, int *b)
'a' is passed by value. Inside func() a has its own memory allocated, so anything you do to it does not affect the variable that was passed in. 'b' is a pointer to an int so changing the data at that address is still visible outside the scope of func(). Hence, x doesn't change, but y does.
Related
void s(int* a, int* b) {
a=b;
}
int main(int argc, char* argv[]) {
int* a = malloc(sizeof(int));
int* b = malloc(sizeof(int));
int c = 10;
int d = 5
a = &c;
b = &d;
printf("%d %d\n",*a,*b);
s(a,b);
printf("%d %d\n",*a,*b);
}
I am so very confused. This is very simple code. I thought that this would result in a and b pointing to the same value. When i do a=b inside of the main function everything works as expected. When I use gdb it even shows that they point to the same place in memory and the function is not being optimized away!!! So what is happening?
Is the function creating its own local copy? Why don't these point to the same variable please help.
You want to change the pointers values. Pointers are passed by value, so you need a pointer to the pointer to change its value:
#include <stdio.h>
void s(int** foo, int** bar)
{
*foo = *bar;
}
int main(void)
{
int c = 10;
int d = 5;
int *a = &c;
int *b = &d;
printf("%d %d\n", *a, *b); // 10 5
s(&a, &b);
printf("%d %d\n", *a, *b); // 5 5 a points at d as well
}
With your version you only changed the parameters which are copies of the values passed to the function.
To help you better understand, consider this:
#include <stdio.h>
void value(int foo, int bar)
{
foo = bar; // changing local copies
}
void pointer(int *foo, int *bar)
{
*foo = *bar; // changing the value foo points to to the value bar points to
}
int main(void)
{
int a = 5;
int b = 7;
value(a, b);
printf("%d, %d\n", a, b); // 5, 7
pointer(&a, &b);
printf("%d, %d\n", a, b); // 7, 7
}
We did that with the type int. Now lets just replace int with int*:
#include <stdio.h>
void value(int *foo, int *bar)
{
foo = bar; // changing local copies
}
void pointer(int **foo, int **bar)
{
*foo = *bar; // changing the value foo points to to the value bar points to
}
int main(void)
{
int x = 5;
int y = 7;
int *a = &x;
int *b = &y;
value(a, b);
printf("%d, %d\n", *a, *b); // 5, 7
pointer(&a, &b);
printf("%d, %d\n", *a, *b); // 7, 7 now both point at y
}
So you see, it's the same concept both times. In the first example the values pointed to are ints and their values are numbers, in the second example the values pointed to are int*s and their values are pointer values (<~ standard terminology, "addresses"). But the mechanism is the same
You program is almost correct but you need to pass the address of the variables in the function as you using call by refrence and in the function take pointer to pointers.
#include <stdio.h>
int main(void){
int *x;
*x = 100;
printf("\n *x = %d \n &x = %p \n sizeof(x) = %lu \n",
*x,(void*)x,sizeof(x));
return 0;
}
I think i missed something in this program, can anyone help me ?
You should init x like
int a;
int* x = &a;
*x = 100; // a = 100
When you define int *x;, the actual value of x, which is supposed to be the address of an int variable, is uninitialized because you haven't assigned any value to it yet. *x = 100; means "set the value at the memory location contained in x to 100". However because you never set the value of x, for example by doing something like int y; x = &y, the memory location that x is pointing to is some random value.
#include <stdio.h>
int main(void){
int y = 0;
//int *x;
int *x = &y; //Need to assign x an address to point to, or else its stack garbage and could be anywhere...
*x = 100;
printf("\n *x = %d \n &x = %p \n sizeof(x) = %lu \n",
*x,(void*)x,sizeof(x));
return 0;
}
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.
int foo(int *p){
p = malloc(sizeof(int));
*p = 20;
}
int main(){
int a;
int *x;
x = &a;
foo(x);
printf("%d \n", a);
return 0;
}
So I'm trying to point at a and then set the value of what x is pointing to 20. However whenever I allocate the pointer in the function I get a random number being printed. Do you know why this happens compared to allocating it in main()?
Thanks
You don't want to allocate in the foo function at all. This is like doing:
void foo(int p) {
p = 2;
printf("%d\n", p);
}
int main() {
int a=1;
foo(a);
return 0;
}
And asking why it didn't print 1. The answer is, you're writing over the pointer to a, so you no longer have a pointer to a when you do *p = 20;.
So I'm trying to point at a and then set the value of what x is pointing to 20
This doesn't make sense given the context of your question. You are pointing x at a, and if you want to change the value of a, then why are you dynamically allocating anything at all? a is already automatically allocated in main(). If you want to dynamically allocate something, what you you need a for at all?
To change the value of a, just pass a pointer to it and forget about malloc():
void foo(int * p)
{
*p = 20;
}
int main(void)
{
int a;
foo(&a);
printf("%d\n", a);
return 0;
}
If you want to dynamically allocate, forget about a, and don't modify it through a passed pointer - return the pointer:
int * foo(void)
{
int * p = malloc(sizeof *p);
if ( !p ) {
perror("could not allocate memory");
exit(EXIT_FAILURE);
}
*p = 20;
return p
}
int main(void)
{
int * x = foo();
printf("%d\n", *x);
free(x);
return 0;
}