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 7089309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:54:22+00:00 2026-05-28T07:54:22+00:00

Difference of two pointers of the same type is always one. #include<stdio.h> #include<string.h> int

  • 0

Difference of two pointers of the same type is always one.

#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}

Output is 1.

I dont get the reasoning behind it

  • 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-28T07:54:22+00:00Added an answer on May 28, 2026 at 7:54 am

    The behavior is undefined.

    C99 6.5.6 paragraph 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.

    Paragraph 7 in the same section says:

    For the purposes of these operators, a pointer to an object that is
    not an element of an array behaves the same as a pointer to the first
    element of an array of length one with the type of the object as its
    element type.

    Section 4 paragraph 2 says:

    If a “shall” or “shall not” requirement that appears outside of a
    constraint is violated, the behavior is undefined. Undefined behavior
    is otherwise indicated in this International Standard by the words
    “undefined behavior” or by the omission of any explicit definition of
    behavior. There is no difference in emphasis among these three; they
    all describe “behavior that is undefined”.

    3.4.3 defines the term “undefined behavior” as:

    behavior, upon use of a nonportable or erroneous program construct or
    of erroneous data, for which this International imposes no
    requirements

    NOTE Possible undefined behavior ranges from ignoring the situation
    completely with unpredictable results, to behaving during translation
    or program execution in a documented manner characteristic of the
    environment (with or without the issuance of a diagnostic message), to
    terminating a translation or execution (with the issuance of a
    diagnostic message).

    Given the declaration:

    int a = 5, b = 10, c;
    

    it’s likely that evaluating &b - &a will yield a result that seems reasonable, such as 1 or -1. (Reasonable results are always a possible symptom of undefined behavior; it’s undefined, not required to crash.) But the compiler is under no obligation to place a and b at any particular locations in memory relative to each other, and even if it does so, the subtraction is not guaranteed to be meaningful. An optimizing compiler is free to transform your program in ways that assume that its behavior is well defined, resulting in code that can behave in arbitrarily bad ways if that assumption is violated.

    By writing &b - &a, you are in effect promising the compiler that that’s a meaningful operation. As Henry Spencer famously said, “If you lie to the compiler, it will get is revenge.”

    Note that it’s not just the result of the subtraction that’s undefined, it’s the behavior of the program that evaluates it.


    Oh, did I mention that the behavior is undefined?

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

Sidebar

Related Questions

What is the difference between two ways to obtain a string from resources: setPositiveButton(R.string.some_string);
I want to calculate the difference between two times, one of which is the
Is there any difference between these two declarations? int x[10]; vs. int* x =
To check that two variables have the same structure type I use a macro
Sending the same pointer into two different shared_ptr 's is bad, it causes double
Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get
I want to get difference between two times. I calculate this by using following.
How would I work out the difference for two Date() objects in JavaScript, while
I'd like to vectorise the difference of two M x N arrays across different
Possible Duplicate: How to calculate the difference between two dates using PHP? I have

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.