Please help me understand how the value of q is initially 1 and later changes to 0.
Code:
#include <stdio.h>
int main()
{
int i=10;
int *p, *q;
p = &i;
printf("%u\n%u\n%u\n",p,q,*q);
*q++=*p++;
printf("%u\n%u\n%u\n%u",p,q,*p,*q);
return 0;
}
Output:
1527896876
1527897120
1
1527896880
1527897124
1527897124
0
int *p, *q;
p = &i;
printf("%u\n%u\n%u\n",p,q,*q);
int* is a wrong type for the format specifier %u. If you pass an argument of wrong type, then the behaviour of your program is undefined.
printf("%u\n%u\n%u\n",p,q,*q);
*q++=*p++;
Here, q has an indeterminate value. It doesn't point to any object. You indirect through the pointer to access an object that it points to. Which is a contradiction because it doesn't point to any object.
Indirecting through an indeterminate pointer results in undefined behaviour.
The behaviour of your program is undefined. That explains everything about the behaviour of the program.
I think what happens is that here
*q++=*p++;
the post-increment operator of q is executed after the assignment.
So, you basically set the value of q to p, and then you increment it.
Also, i don't think the increment operator of p does anything.
For a better understanding look at the following example
#include <stdio.h>
int main() {
int i = 10;
int *p;
int *q = new int[2];
q[0] = 9;
q[1] = 99;
p = &i;
printf("%u\n", *q);
*q++ = *p++;
printf("%u\n", *q);
printf("%u", *--q);
return 0;
}
9
99
10
Notice how, in the end q points to the next element, thanks to the post-increment operator.
Anyway, i have no idea what you are trying to do.
Related
I was studying for a test with the following question with the given output :
#include <stdio.h>
int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);
}
Output : 11 11
I understand that the address of *p pointing to int i is passed to the function foo. Here, **p is a pointer pointing to *p, where *p points to int i.
In the function, **p pointer changes and points to int j, and the first printf is called and 11 is printed.
What I don't understand is the output from the 2nd printf function. Why is it printing 11, when it should be 10? I've checked and the value of int i and it did not change, so shouldn't dereferencing *p give 10 and not 11.
Can someone explain to me the logic behind what is happening and why is it happening?
First you asign p the address of i and you input the address of p to the function foo and inside the foo function the the value of p becomes what every the value in j (*p = &j). When the memory address changes in the foo function you are not changing it back.
NOTE: There the variable p is not passed by value, It is passed to the function by reference. So any change you do to the p variable inside the foo function will affect the p variable inside the main function because they have the same memory address
The line:
*p = &j;
makes the original variable
int *const p
point to the address of the local variable
int j
After foo is called, that local variable j has since been deallocated from the stack, but p is still pointing to that same stack location which still has that value of 11. So you are illegally accessing deallocated stack memory but it just happens to remain the value of 11. So it is printed a second time.
This question already has answers here:
Pointer expressions: *ptr++, *++ptr and ++*ptr
(11 answers)
Closed 5 years ago.
Why the below is difference?
program001.c:
int main (void)
{
int a=3,*p,x;
p=&a;
*p++;
x=*p
printf("a=%d, *p=%d, x=%d\n",a, *p, x);
return 0;
}
result: a=3,*p=21974,x=21974
Program002.c:
int main (void)
{
int a=3,*p,x;
p=&a;
x=*p++;
printf("a=%d,*p=%d,x=%d\n",a,*p,x);
return 0;
}
result:a=3,*p=3,x=3
for program001's result, it can be understand: *p++ is point to undefined value, so it is unreasonable result.
for program002's result, why it is not equal to program001?
From example 1:
*p++;
x=*p;
can be rewritten as
*p; // dereference p and ignore the value. Valid as p points to a
p++; // increment p
x=*p; // dereference p and assign the value to x. Invalid as p no longer
// points to an int object
From example 2:
x = *p++;
can be rewritten as
x = *p; // dereference p and assign the value to x. Valid as p points to a
p++; // increment p
So in example 1 you assign to x after p is incremented. In example 2 you assign to x before p is incremented.
Both examples has undefined behaviour, e.g. due to *p in the print statement where p is dereferenced even though it doesn't point to a int object anymore as p was incremented. In example 1 the undefined behavior already happens at x=*p;
From the point of the behavior of the two programs, there is no difference. Both programs have Undefined Behavior on all control paths. So both programs can crash, can halt, can run infinitely, can print anything or not print anything at all.
Why does the pointer p always point to its own memory address as an integer in the following example. I can't see where it is initialized and would guess that it would be a garbage value. Can someone show me why it is not a garbage value. By the way I am compiling this in gcc with -std set to c99.
#include <stdio.h>
int main() {
int *p; int a = 4;
p = &a;
*p++;
printf("%d %u\n", *p, p);
}
Your problem (as the other answers point out) is with *p++;. What that says to do is dereference p then increment the address in p.
From what you are seeing, we can assume p comes directly after a in memory
_________________________________________
|something | a | p | something else |
-----------------------------------------
So what ends up happening is p points to a, then is incremented so it points to itself (or more specifically: p stores the address that p is at).
First you need to print a pointer value with %p, and your code has undefined behavior. You move the pointer one place after a and dereference it.
Your code doesn't illustrate the point you (it seems) wanted, the following will:
#include <stdio.h>
int main() {
int *p; int a = 4;
p = &a;
printf("%d %p %p\n", *p, p, &p);
}
It produces something like:
4 0x7fff5c17da44 0x7fff5c17da48
p points to a then *p is the value of a. The value of p is 0x7fff5c17da44 which is the adresse of a and the address of p (&p) is 0x7fff5c17da48.
int main(void)
{
int n1 = 2, n2 = 5;
int *p = &n1, *q = &n2;
*p = *(q++);
printf("%d,%d", *p, *q);
return 0;
}
output= 5,5
Why the value of *q is 5 it should have some garbage value?
int main(void)
{
int n1 = 2, n2 = 5;
int *p = &n1, *q = &n2;
*p = *(++q);
printf("%d,%d", *p, *q);
return 0;
}
output= 2,2
And how is this happening?
Can anyone explain how precedence rule works in pointers?
*p = *(q++); is (more or less) equivalent to *p = *q; q++;, so p is fine. q++ will be evaluated, yielding the old value of q (i.e. the value pre-increment). What you're seeing there is the expected behavior.
You do have undefined behavior in the deference of q in the printf call though since q no longer points at memory you own at that point. A million different things could be causing that (e.g. last time the memory was allocated, maybe a 5 was there, the compiler is being too nice and trying to help you, etc), but you cannot and should not depend on this behavior. Doing so is dangerous, and this program would likely crash or output nonsense on many compilers/operating systems/hardware.
Why the value of *q is 5 it should have some garbage value?
It is due to the postfix incrementation in *(q++) which acts after the pointer dereference and the assignment to *p.
So, the current value of the address pointed by *q is assigned to *p, and then q is incremented to a "garbage value".
This strange result in printing 5,5 is undefined behaviour.
I have a program question, here is the code.
int main()
{
int *p,*q;
p=(int*)1000;
printf("%d ",p);
q=(int*)2000;
printf("%d",q);
printf("%d",(p-q));
return 0;
}
But answer coming as
1000
2000
-250
I am unable to understand what happen with p-q and why answer came as -250?
Correct but probably useless answer: p - q is equal to (1000 - 2000) / (sizeof int). For most C compiles, sizeof int is 4.
Potentially more useful answer: the effect of typecasts like (int*) 1000 is undefined. That code creates a pointer to an int at address 1000. That address is probably invalid. To create a pointer to an int with value 1000, write this:
int i = 1000;
int *p = &i;
Now p points to i, and *p, the value pointed to by p, is 1000.
Here is some correct code that may say what you meant:
int main() {
int i = 1000;
int j = 2000;
int *p = &i;
int *q = &j;
printf("i = %d *p = %d\n", i, *p);
printf("j = %d *q = %d\n", j, *q);
printf("*p - *q = %d\n", *p - *q);
}
When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them.
On your platform, an int is 4 bytes. There are -250 elements between address 2000 and address 1000.
Since p and q don't both point to the same array, the result is undefined. You could get any result, including the one you expect.
So much undefined behaviour in this program, the values printed are really quite immaterial.
p is a pointer variable, which can store only address of a int variable. But you are storing 1000 as an address, which is invalid. And then you are storing 2000 to the variable q.
Now you are doing pointer arithmatic p-q. Always pointer arithmatic will gives the output based on the size of the type of address. Which will work like (p - q)/sizeof(type).
Consider if p and q are char * variable, then p-q will gives you the output as -1000
In your case p and q are int * variable, then p-q will gives you the output as 250 if size of int is 4 bytes. Execute this on the compiler where size of int is 2 bytes, you will get a result as -500.