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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:20:04+00:00 2026-05-31T06:20:04+00:00

Is subtraction of pointers not pointing to different elements of same array valid in

  • 0

Is subtraction of pointers not pointing to different elements of same array valid in C?

Is something such as below guaranteed to work according to C Standards? I vaguely remember reading that this is not valid?

int * a;
int * b;
a = (int*) 100;
b = (int*) 200;
printf("%d\n", b-a);

Will this give me 25.

  • 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-31T06:20:05+00:00Added an answer on May 31, 2026 at 6:20 am

    From the C spec, Appendix J.2 Undefined behaviour:

    Pointers that do not point into, or just beyond, the same array object are subtracted (6.5.6).

    6.5.6 Additive operators, 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.

    There you have it – your example causes undefined behaviour. That said, on most systems it will work just fine. You probably do want to change your printf format to %td to indicate that you’re printing a ptrdiff_t type.

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

Sidebar

Related Questions

Its not really a subtraction I'm looking for. And I know its not a
I'm doing background subtraction using opencv. The problem is the foreground object is not
I'm building a Matrix class for all the operations such as addition, subtraction, multiplication,
What's a fastest way to implement array subtraction? For example: array a1 = [1,
I'm looking for tutorials which talk about bitwise arithmetic operations, such as addition, subtraction,
I've got background subtraction to work quite well. But the problem is, it marks
I was shocked to see such a basic subtraction bug in JS, I am
Why won't this simple subtraction work? int MyPageNumber = Convert.ToInt32(cboPageNumber.SelectedItem); MyPageNumber += (MyPageNumber -
Can I do something like this? Will this work? double *vec_subtraction (char *a, char
I'd like to do a subtraction of sets based on criteria. A pseudo-query would

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.