Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:51:44+00:00 2026-05-27T04:51:44+00:00

I have the following program: #include <iostream> struct X { int a; float b;

  • 0

I have the following program:

#include <iostream>

struct X
{
    int a;
    float b;
} x[10], *p1, *p2;

int main(int argc, char *argv[])
{
    p1 = &x[1];
    p2 = &x[5];

    int i = p2 - p1;

    std::cout << i << std::endl;
}

I can visualize X‘s layout in memory, 10 boxes containing an int and a float, p1 will point at the beginning of the second box (x[1]) and p2 pointing at the beginning of the 6th box (x[5]):

   X   0   1  2  3  4  5  6  7  8  9
       _______________________________
    b  |__|__|__|__|__|__|__|__|__|__|
    a  |__|__|__|__|__|__|__|__|__|__|
          |           |    
          |           | 
          p1          p2

Is my drawing correct ? If so why is the result of i 4 ?
Having some difficulties understanding subtraction of two addresses ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:51:45+00:00Added an answer on May 27, 2026 at 4:51 am

    This is how pointer arithmetic works. Consider:

    p1 = (x*)100;   // invalid memory address, just an example!
    p2 = p1 + 1; 
    

    At this point, p2 will not have the value 101, but rather 100 + sizeof(x) (which let’s say is 8, so 108). It has been incremented not by one, but by one multiple of sizeof(x)! Conversely, subtracting an integer from a pointer actually subtracts multiples of sizeof(the pointed to type).

    So now if you do int diff = p2 - p1, you would certainly expect to get 1 back, not 8! Otherwise, subtracting the number you just added would not yield the original value. Therefore, subtracting one pointer from another yields not the difference in memory addresses but the number of elements between the two pointers.

    Moreover, the standard mandates that pointer subtraction is meaningless unless the two pointers point to elements in the same array (more correctly, it’s undefined behavior and you are also allowed to use a pointer to “one past the last element” even if there is no such object there).

    Finally, what if the compiler does not know the size of the pointed to type (i.e. the pointers are void*)? In this case, pointer arithmetic is not allowed at all. For example:

    void* p = 100;
    void* x = p + 1; // does not compile¹
    

    ¹ Some compilers may provide pointer arithmetic on void* as an extension to the language specification. In this case this statement can indeed compile and the result would depend on the specification of said extension (e.g. gcc would end up with the value 101).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this C++ file: #include <iostream> int main(int argc, char *argv[]) { std::cout
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
In C++ I have this program: #include <iostream> using namespace std; int main(){ string
I have the following program test.cc: #include <iostream> unsigned char bogus1[] = { //
I have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
I have the following code: #include <iostream> #include <string> #include sqlite3.h int main() {
I have a very simple C++ application. #include <stdio.h> #include <iostream> int main(int argc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.