why address difference is 1 between two int in C program.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
*/
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.
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.
Last edited by sundialsvcs; 05-25-2022 at 01:35 PM.
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)?
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
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.
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???
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.