Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!!!
Today, I started using pointers in C language and I have a problem in the output.

I have this
[^]error[^]

I tried to change %d to %ls as it says ,but in the output it doesn't show me the addresses .
Where the problem is?
Thanks in advance !!!

What I have tried:

Code:
C++
#include <stdio.h>

int main()
{
    int x;
    int *p;
    p = &x;

    *p = 75;
    printf("price of x = %d ,\t price of pointer p = %d ,\t address of x => %d ,\t address of pointer p = %d \n" , x , *p , &x , p);

    x = 999;
    printf("price of x = %d ,\t price of pointer p = %d ,\t address of x => %d ,\t address of pointer p = %d \n" , x , *p , &x , p);

    *p++;
    printf("price of x = %d ,\t price of pointer p = %d ,\t address of x => %d  ,\t address of pointer p = %d \n" , x , *p , &x , p);

    return 0;
}
Posted
Updated 20-Jul-20 20:45pm
v2

1 solution

Try this:
C++
printf("Price of x = %d\nPrice via pointer p = %d\nAddress of x = %p\nAddress pointed to by p = %p\n", x, *p, &x, p);
 
Share this answer
 
Comments
Nick_is_asking 20-Jul-20 14:45pm    
It works. Thank you!!!
Nick_is_asking 20-Jul-20 14:52pm    
One more question.If x = 8 and I write this: *p++; then I recieve this:
price of x = 9 , price of pointer p = -971355120 , address of x => 0x7ffec61a4c0c , address of pointer p = 0x7ffec61a4c10
=============================
.but I expected this:
price of x = 9 , price of pointer p = 9 , address of x => 0x7ffec61a4c0c , address of pointer p = 0x7ffec61a4c10

or am I wrong?
OriginalGriff 20-Jul-20 15:00pm    
You are wrong: it's down to operator precedence. The "postincrement operator" (p++) is higher than the "dereference operator" (*p) so *p++ increments the pointer, not the content of the pointer, then returns the value that now points to. If you want to increment x via p, then you need brackets to force the order you want: (*p)++
Rick York 20-Jul-20 16:34pm    
you should avoid writing expressions like that because they can be prone to error. It is better to first dereference the pointer (*p) and use that value. Then increment the pointer when you are done with its value.
Nick_is_asking 20-Jul-20 15:19pm    
ok I understand, thanks!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900