LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why address difference is 1 between two int in C program. (https://www.linuxquestions.org/questions/programming-9/why-address-difference-is-1-between-two-int-in-c-program-4175712588/)

rahulvishwakarma 05-25-2022 04:31 AM

why address difference is 1 between two int in C program.
 
i've centos 7.5 in VM and practicing in c with IDE CodeBlocks. I am trying with following code. Here I am trying to subtract address of two int variables, subtraction is 1, even when manually seeing difference is 4 bytes. same with char variable address. why there is address difference is one(i.e. 1).
Code:

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int a = 10, b = 20, diff;

    char ch1 = 'Z', ch2 = 'A';

    printf("Address of a = %u , Address of b = %u\n", &a, &b);
    diff = &a - &b;

    printf("subtraction of addresses : &a - &b = %d\n", diff );

    diff = &ch1 - &ch2;

    printf("Address of ch1 = %u , Address of ch2 = %u\n", &ch1, &ch2);

    printf("subtraction of addresses : &ch1 - &ch2 = %d\n", diff );



    return 0;
}

/*
O/P :-
------
[rahul@C-Client Debug]$ ./ex-4
Address of a = 4239130824 , Address of b = 4239130820
subtraction of addresses : &a - &b = 1
Address of ch1 = 4239130819 , Address of ch2 = 4239130818
subtraction of addresses : &ch1 - &ch2 = 1

*/


pan64 05-25-2022 04:35 AM

because the length of an int is 4 bytes - try to print sizeof(int).

EdGr 05-25-2022 04:59 AM

Be careful - pointer subtraction is defined only when both pointers point to the same array. It returns the difference in array indices.
Ed

smallpond 05-25-2022 06:19 AM

When you add 1 to a pointer, the address increases by size of the type. Subtraction works the same way. All pointer arithmetic is scaled by the type size. This is because *(p + 1) is the same as p[1].

To find the actual difference in addresses, you can cast the pointers to long integer types and then subtract.

sundialsvcs 05-25-2022 08:47 AM

Strictly speaking, and depending on the optimization options selected, the addresses might be different because of "slack bytes" that may be inserted for "memory-line alignment" in the CPU. But, pointer-arithmetic as described will still work in all cases.

If you specifically need for a struct to occupy the minimum number of bytes required ("packed"), you must request it.

TB0ne 05-25-2022 01:09 PM

Quote:

Originally Posted by rahulvishwakarma (Post 6356316)
i've centos 7.5 in VM and practicing in c with IDE CodeBlocks. I am trying with following code. Here I am trying to subtract address of two int variables, subtraction is 1, even when manually seeing difference is 4 bytes. same with char variable address. why there is address difference is one(i.e. 1).
Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 10, b = 20, diff;
    char ch1 = 'Z', ch2 = 'A';

    printf("Address of a = %u , Address of b = %u\n", &a, &b);
    diff = &a - &b;

    printf("subtraction of addresses : &a - &b = %d\n", diff );

    diff = &ch1 - &ch2;

    printf("Address of ch1 = %u , Address of ch2 = %u\n", &ch1, &ch2);
    printf("subtraction of addresses : &ch1 - &ch2 = %d\n", diff );

    return 0;
}

/*O/P :-
------
[rahul@C-Client Debug]$ ./ex-4
Address of a = 4239130824 , Address of b = 4239130820
subtraction of addresses : &a - &b = 1
Address of ch1 = 4239130819 , Address of ch2 = 4239130818
subtraction of addresses : &ch1 - &ch2 = 1*/


So after programming in C/C++/Codeblocks for *TWELVE YEARS* you're unable to debug your own code??? Can you show us what steps you've taken to narrow down your problem(s)?

dugan 05-25-2022 09:39 PM

This will get you the 4:
Code:

diff = (void *)(&a) - (void *)(&b);
Or possibly more properly:
Code:

diff = (char *)(&a) - (char *)(&b);

EdGr 05-26-2022 03:16 AM

K&R says:

The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal.

The "same array" requirement is not just theoretical. I have seen pointer subtraction return bogus results when the pointers are in different arrays. I suspect this happens when the address difference is not an exact multiple of the array element size.

The right answer is "don't subtract pointers to different variables".
Ed

rahulvishwakarma 08-20-2022 04:35 AM

respected sir TB0ne, I didn't worked in any software company for more than 2-4 months. I had psychetric probelms. so that i could not worked in any company. I am doing my self study to teach others c and c++ in linux platform. I don't have any industrial experince. thank you.

TB0ne 08-20-2022 11:27 AM

Quote:

Originally Posted by rahulvishwakarma (Post 6374940)
respected sir TB0ne, I didn't worked in any software company for more than 2-4 months. I had psychetric probelms. so that i could not worked in any company. I am doing my self study to teach others c and c++ in linux platform. I don't have any industrial experince. thank you.

Sorry, doesn't make sense...again, you have been using C/C++ and Codeblocks for *TWELVE YEARS*. Not using it for 2-4 months isn't going to make you forget everything, and how exactly are you going to teach others when you don't know it yourself???

You have shown, and continue to show, no efforts of your own. You don't appear to be able to apply what you've been told, either. Have you gone back through any of your other threads???


All times are GMT -5. The time now is 05:53 AM.