Thread: memory address calculations

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    7

    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.
    Last edited by whynot; 01-26-2022 at 08:58 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,154
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    7
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,622
    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. #5
    Registered User
    Join Date
    Jan 2021
    Posts
    7
    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. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    822
    Quote Originally Posted by whynot View Post
    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. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    145
    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 subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  3. memory address
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2005, 09:15 AM
  4. Memory address?
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:04 PM

Tags for this Thread