#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;
}
Related
int* a[2];
int x = 5;
int y = 7;
int *b = x;
int *c = y;
a[0] = b;
a[1] = c;
printf("%d", *a[1]);
Why isn't this working?
This is an Array of pointers and I'm trying to get the value of y - a[1] is the address of y so I added a * to get the value of it..
Thanks everyone!
int *b = x;
int *c = y;
The problem here is that your are assigning the values of the objects x and y instead of their address.
You can get the address of an object using the address-of operator &.
int *b = x;
int *c = y;
Why isn't this working?
Because you assign the value held by the integer to the pointer, not the reference of the variable x or y
You need to:
int *b = &x;
int *c = &y;
A slight change to your code to pass the address of the integer x and y to pointers b and c will make your code work.
int *b = &x;
int *c = &y;
I am finding it difficult to visualize this piece of code. I cannot seem to find the ans to this.
I did get the ans for
printf("**r = %d\n",**r);
printf("**s = %d\n",**s);
but other variables are not matching the ans. Please help.
int f(int ** r, int ** s) {
int temp = ** r;
int temp2 = **s;
int * z = *r;
*r = *s;
*s = z;
printf("**r = %d\n",**r);
printf("**s = %d\n",**s);
*z += 3;
**s -= 8;
**r -= 19;
return temp + temp2;
}
int main(void) {
int a = 80;
int b = 12;
int * p = &a;
int * q = &b;
int x = f(&p, &q);
printf("x = %d\n", x);
printf("*p = %d\n", *p);
printf("*q = %d\n", *q);
printf("a = %d\n", a);
printf("b = %d\n", b);
return EXIT_SUCCESS;
}
Expected output:
**r = 12
**s=80
x=92
*p=-7
*q=75
a=75
b=-7
In this declaration the pointers p and q are passed by reference to the function f.
int x = f(&p, &q);
The pointer p points to the variable a and the pointer q points to the variable b.
int a = 80;
int b = 12;
int * p = &a;
int * q = &b;
Within the function f declared like
int f(int ** r, int ** s);
the pointer r points to the pointer p and the pointer s points to the pointer q.
Correspondingly in these declarations
int temp = ** r;
int temp2 = **s;
the variable temp is initialized by the value of variable a and the variable temp2 is initialized by the value of the variable b.
You van imaging these declaration like
int temp = a;
int temp2 = b;
In this code snippet
int * z = *r;
*r = *s;
*s = z;
there are in fact swapped the the pointers p and q pointed to by the pointers r and s. That is after this code snippet the pointer r now points to the pointer q and the pointer s points to the pointer p.
You can imaging this like
*r = q;
*s = p;
The intermediate variable z
int * z = *r;
gets the value of the pointer p.
You can imaging this like
int * z = p;
This statement
*s = z;
did not change the value pointed to by s because before this statement the variable s already pointed to p due to preceding swapping the pointed values of the pointer r and the pointer s.
So these calls of printf
printf("**r = %d\n",**r);
printf("**s = %d\n",**s);
correspondingly will output the value of b and the value of a.
That is the output will be
**r = 12
**s = 80
As the pointer z has the value of the pointer p then after this statement
*z += 3;
the variable a will be increased by 3 and becomes equal to 83.
In these statements
**s -= 8;
**r -= 19;
the variable a will be decrease by 8 and becomes equal to 75 And the variable b is decreased by 19 and becomes equal to -7.
At last the function returns the sum of the initial values of the variables a and b
return temp + temp2;
that is 92.
In these statements in main
printf("x = %d\n", x);
printf("*p = %d\n", *p);
printf("*q = %d\n", *q);
printf("a = %d\n", a);
printf("b = %d\n", b);
there is outputted the value returned by the function f
printf("x = %d\n", x);
that is 92.
As the pointers p and q were swapped in the function then now the pointer p points to b and the pointer q points to a.
printf("*p = %d\n", *p);
printf("*q = %d\n", *q);
So these statements output
*p = -7
*q = 75
And these statements
printf("a = %d\n", a);
printf("b = %d\n", b);
outputs the new values of a and b themselves that is
a = 75
b = -7
As for this statements
printf("**r = %d\n",**r);
printf("**s = %d\n",**s);
then for example the expression *r points to the pointer p. So dereferencing the pointer p like *p that is the same as *( *r ) you will get the lvalue of the variable a.
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.
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 want to understand the nature of pointers work. And have a simple example of swap function. First is working as expected, the second fails.
I can't understand the second function swap2(). I'm swapping the addresses successfully but after the function exit the values stay the same...
Why?
void swap1(int *x, int *y);
void swap2(int *x, int *y);
void startSwapExample() {
int a = 10;
int b = 20;
printf("a = %i, b = %i\n", a, b);
swap1(&a,&b);
printf("a = %i, b = %i\n", a, b);
printf("\n================\n");
int c = 10;
int d = 20;
printf("c = %i, d = %i\n", c, d);
swap2(&c,&d);
printf("c = %i, d = %i\n", c, d);
}
void swap1(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int temp = * x;
* x = * y;
* y = temp;
printf("x = %i, y = %i\n", *x, *y);
}
void swap2(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int * temp = x;
x = y;
y = temp;
printf("x = %i, y = %i\n", *x, *y);
}
The output of the program:
a = 10, b = 20
x = 10, y = 20
x = 20, y = 10
a = 20, b = 10
================
c = 10, d = 20
x = 10, y = 20
x = 20, y = 10
c = 10, d = 20
Process finished with exit code 0
In swap2, you are swapping the pointer x and y, both of which only lives within the scope of the swap2 function.
When the function returns, pointer x and y no longer exist and whatever operation you did on the pointer (as opposed to the value pointed by the pointer) has no effect.
swap2() function is not working according to your expectation, reason is below.
void swap2(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int * temp = x;
x = y;// x holding y
y = temp;// y holding x but not in main() function, only in swap2()
printf("x = %i, y = %i\n", *x, *y); // here you are getting expected output but not in main() function
}
in swap2() function it's swapping but modification is not affecting in main() function because temp as well as x & y are local pointers & and you are not de-referencing anything , so in main() it will be same.