When I code like this and run, the console shows nothing:
#include<stdio.h>
int main()
{
int n2= 10;
int n1= 6;
int n0= 5;
int* p = &n1;
//printf("Address: %p\t%p\t%p\n", &n2, &n1, &n0);
*p=9;
p ;
*p=15;
p--;
p--;
*p=-3;
printf("n2=%d, n1=%d, n0=%d\n", n2, n1, n0);
getchar();
return 0;
}
But when I add
printf("Address: %p\t%p\t%p\n", &n2, &n1, &n0);
into my code, like this
#include <stdio.h>
int main()
{
int n2= 10;
int n1= 6;
int n0= 5;
int* p = &n1;
printf("Address: %p\t%p\t%p\n", &n2, &n1, &n0);
*p=9;
p ;
*p=15;
p--;
p--;
*p=-3;
printf("n2=%d, n1=%d, n0=%d\n", n2, n1, n0);
getchar();
return 0;
}
It runs in the way I expected (the result of n2 and n0 changed). I don't understand why, can anyone explain it for me?
CodePudding user response:
Variables n0
, n1
, and n2
in your code do not form an array, so you have no guarantee they will occupy a contiguous area in memory. Compilers are not required to allocate them close to each other or in any specific order, they even needn't allocate them in memory - for example a variable may be kept in a processor's register, or even discarded completely if unused.[1]
Hence pointer arithmetics is not defined to bring you from one of them to another.
It is an undefined behavior what happens then:
- You may hit some vital data of your process and cause the program to crash.
- Or you may hit an inaccessible memory and get your program terminated by the operating system.
- You may be overwriting some unrelated data, important for your code, and thus destroy the order of execution of the program. The result is the program apparently 'working', but doing something else than you designed.
- Or you may hit unused memory and experience completely no change in the program's behavior.
And each of those results may depend on the compiler you used, the machine you run your program on, the kind and version of the operating system. Any change in the program itself or its environment may result in different behavior.
[1]: Except if an address of a variable is explicitly fetched with the &
operator, which is the case of n1
in the first version of your code, and all three after you add another printf()
.
CodePudding user response:
p is meaning to change pointer to next address in memory.
This method is incorrect because you are not using arrays.
In some cases, p ;
may not point to n0.
CodePudding user response:
Sure. You are using printf with print format specifiers.
%p
says you want to print a pointer variable. It works because the variable in your case is &n2
. In this case, n2
is an int
, but you take the address of it, making it a pointer with the &
operator.
%d
would specify printing a decimal or integer variable. Don't confuse with d
for double as in double width floating point number. See below.
%f
for a float variable
%s
a string variable
%c
a character variable.
See here https://linux.die.net/man/3/sprintf for more info
In conclusion you could print the value as an int:
printf("n1 is %d \n", n1);
printf("the address of n1 is %p \n", &n1);