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

  • Home
  • SEARCH
  • 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 7571531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:41:48+00:00 2026-05-30T15:41:48+00:00

struct Item { int i; char * x; }; Item all[] = { {0,

  • 0
struct Item {
    int i;
    char * x;
};
Item all[] = { {0, "0"},  {1, "1"},  {2, "2"}, };

int i = all - (Item *) &all[2].x ;
printf("i = %d\n", i);

in the above code, I cast &all[2].x to (Item*) and perform a pointer substract operation,
mingw g++(4.6.1) produces -2, and VC2010 produces -3.

Is such operation a well-defined behavior (then VC likely has a bug, anyway the distance between the two pointer is 2 elements) or not?

I’ve search the ANSI C standard(C99) 6.5.6 Semantics(9) says:

When two pointers are subtracted, both shall point to elements of the same array object,
or one past the last element of the array object; the result is the difference of the
subscripts of the two array elements.

It’s not very clear(what ‘shall’ imply) whether pointing to data member of the object is also ok.

I’ve also checked with <> Section 7.6.2 Additive operatives/Substraction, which says:

… The result is well defined and portable only if
the two pointers point to objects in the same array or point to one past the last object of the
array.

Looks like the pointer must point to the object itself, point to data member of the object is not well-defined. But the statement does not declare it explicitly.

There’s another resource which says:

 ( p2 - p1 ) == ( addr( p2 ) - addr( p1 ) ) / sizeof( T )

…

p2 and p1 need not point to valid elements in an array. The formula above still works even when p2 and p1 contain invalid addresses (because they contain some address).

Which confuse me further.

Anyone please clarify this subtle issue?

  • 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-30T15:41:49+00:00Added an answer on May 30, 2026 at 3:41 pm

    The part you have quoted from the standard is clear:

    When two pointers are subtracted, both shall point to elements of the
    same array object…

    &all[2].x does not point to an element of the array all. Pointing at a subobject of an element is not pointing at an element.

    This means that the behaviour is undefined.

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

Sidebar

Related Questions

My code is as follows: #include <stdio.h> struct MyData { int id; char msg[255];
#include <stdio.h> int main (int argc, const char * argv[]) { static struct item
struct { char a; int b; } x; Why would one define a struct
struct Div { int i; int j; }; class A { public: A(); Div&
struct ID3v2_header{ char tag[3]; char ver[2]; char flag; int size; }; ID3v2_header readID3v2_head(string file){
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
I have an issue with the following code. int main (int argc, const char
#include <iostream> using namespace std; struct Node { int item; // storage for the
typedef struct What_if { char price [2]; } what_if ; what_if what_if_var[100]; int format_input_records();

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.