1. memory address calculations

hi again,
I'm a little bit confused. How do we calculate memory address differences? Does it matter if it's from int or double type variable? Eventually, they are same type of numbers?
If I give an example like this?
Code:
```#include <stdio.h>

main() {
int a=16, b=22;
int diff_1, diff_2, diff_3, diff_4;
double c=10.0, d=234.52;
printf("\nAddress of  a = %u  ",&a);
printf("Value of a      = %i  ",a);
printf("\nAddress of  b = %u  ",&b);
printf("Value of b      = %i  ",b);
printf("\nAddress of  c = %u  ",&c);
printf("Value of c      = %f ",c);
printf("\nAddress of  d = %u  ",&d);
printf("Value of d      = %f ",d);

printf("\n\n");
diff_1 = &a-&b;
diff_2 = &b-&c;
diff_3 = &c-&d;
diff_4 = &a-&d;

printf("\nAddress difference a-b = %u", diff_1);
printf("\nAddress difference b-c = %u", diff_2);
printf("\nAddress difference c-d = %u", diff_3);
printf("\nAddress difference a-d = %u", diff_4);
return 0;
}```
Thanks.

2. You can't meaningfully compare the addresses of independent variables.

Knowing what &a is tells you nothing about what &b should be. The compiler is free to arrange and order independent variables as it sees fit.

You can however compare
- pointers to different elements within the same array
- pointers to different members within the same struct.

3. Actually, I expected something different like *ptr = 2001 *ptr = 2002 *ptr = 2002

Code:
```#include <stdio.h>

main() {
int x=2001;
int *ptr;
ptr = &x;

printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
ptr++;
printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
ptr++;
printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
}```
Output
Code:
```&ptr = 931504240
*ptr = 2001
ptr = 931504252
&ptr = 931504240
*ptr = 4198992
ptr = 931504256
&ptr = 931504240
*ptr = 0
ptr = 931504260%```

4. I would suggest that you increase your compiler warning level, and fix all the warnings and errors in the code.

Here is what my compiler is saying about the code in the OP:

Code:
```||=== Build: Debug in chomework (compiler: GNU GCC Compiler) ===|
main.c|3|error: return type defaults to ‘int’ [-Wimplicit-int]|
main.c||In function ‘main’:|
main.c|7|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]|
main.c|9|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]|
main.c|11|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘double *’ [-Wformat=]|
main.c|13|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘double *’ [-Wformat=]|
main.c|18|error: invalid operands to binary - (have ‘int *’ and ‘double *’)|
main.c|20|error: invalid operands to binary - (have ‘int *’ and ‘double *’)|
||=== Build failed: 3 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|```

5. I don't know how to do that, but I don't have any errors. I have some warnings.
It's also working here https://www.programiz.com/c-programm...line-compiler/
Code:
```#include <stdio.h>

int main() {
int x=2001;
int *ptr;
ptr = &x;

printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
ptr++;
printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
ptr++;
printf("\n&ptr = %u",&ptr);
printf("\n*ptr = %i",*ptr);
printf("\nptr = %u",ptr);
return 0;
}```

6. Originally Posted by whynot
Actually, I expected something different like *ptr = 2001 *ptr = 2002 *ptr = 2002
Why would you expect *ptr = 2002? Nothing increments the value that ptr points at. What your latest code is doing is incrementing the pointer ptr itself so that it points at some other, invalid location (it's invalid because ptr does not point at an array or struct, which Salem hinted at in #2).

7. Maybe you should use GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++
It lets you specify extra compiler flags

Popular pages Recent additions