LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2022, 04:31 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
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

*/
 
Old 05-25-2022, 04:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,945

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
because the length of an int is 4 bytes - try to print sizeof(int).
 
Old 05-25-2022, 04:59 AM   #3
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 999

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
Be careful - pointer subtraction is defined only when both pointers point to the same array. It returns the difference in array indices.
Ed
 
Old 05-25-2022, 06:19 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,150

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
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.

Last edited by smallpond; 05-25-2022 at 06:21 AM.
 
1 members found this post helpful.
Old 05-25-2022, 08:47 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 05-25-2022, 01:09 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,695

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by rahulvishwakarma View Post
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)?
 
Old 05-25-2022, 09:39 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
This will get you the 4:
Code:
diff = (void *)(&a) - (void *)(&b);
Or possibly more properly:
Code:
diff = (char *)(&a) - (char *)(&b);
 
Old 05-26-2022, 03:16 AM   #8
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 999

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
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
 
Old 08-20-2022, 04:35 AM   #9
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Original Poster
Rep: Reputation: 2
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.
 
Old 08-20-2022, 11:27 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,695

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by rahulvishwakarma View Post
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???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] specifying output int based on input int and source IP on multiple 802.1q, tacorama Linux - Networking 1 05-05-2011 07:30 AM
invalid conversion from 'int' to 'unsigned int*' tigerhp Programming 2 03-02-2008 04:21 PM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
Problem with sending a signed int to another signed int. Almost random number given. RHLinuxGUY Programming 8 08-15-2006 11:38 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration