Skip to content

ProfoundQa

Idea changes the world

Menu
  • Home
  • Guidelines
  • Popular articles
  • Useful tips
  • Life
  • Users’ questions
  • Blog
  • Contacts
Menu

What are the rules for subtracting two pointers in C?

Posted on September 26, 2022 by Author

Table of Contents

  • 1 What are the rules for subtracting two pointers in C?
  • 2 Can we do subtraction of two pointers?
  • 3 How null pointer is defined?
  • 4 What happens when you subtract pointers?
  • 5 IS null always defined as 0 zero )?
  • 6 What is null defined as in C?
  • 7 WHAT IS null pointer and void pointer?
  • 8 Is it possible to subtract a pointer from an object?
  • 9 Why does my program crash when I call f(0) instead of 0?
  • 10 How to find the distance between two pointers in an array?

What are the rules for subtracting two pointers in C?

When two pointers are subtracted, both must point to elements of the same array object or just one past the last element of the array object (C Standard, 6.5. 6 [ISO/IEC 9899:2011]); the result is the difference of the subscripts of the two array elements. Otherwise, the operation is undefined behavior.

Can we do subtraction of two pointers?

The subtraction of two pointers is possible only when they have the same data type. The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type.

Can you subtract pointers from each other in C?

Two pointers can also be subtracted from each other if the following conditions are satisfied: Both pointers will point to elements of same array; or one past the last element of same array. The result of the subtraction must be representable in ptrdiff_t data type, which is defined in stddef.

READ:   How long does it take to create a Minecraft mod?

How null pointer is defined?

A null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.

What happens when you subtract pointers?

Pointer Subtraction The result is the distance (in array elements) between the two elements. This can result in negative values if p2 has a smaller address than p1. p2 and p1 need not point to valid elements in an array.

Can two pointers be compared?

Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9. 2).

IS null always defined as 0 zero )?

The null pointer constant is always 0. The NULL macro may be defined by the implementation as a naked 0 , or a cast expression like (void *) 0 , or some other zero-valued integer expression (hence the “implementation defined” language in the standard). The null pointer value may be something other than 0.

READ:   Who is the golden pair for an INFJ male?

What is null defined as in C?

In computer programming, null is both a value and a pointer. Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.

What is value of null in C?

The value of NULL macro is 0. It is defined in C header files as below. #define NULL (void *) 0; NULL is used for pointers only as it is defined as (void *) 0. If NULL is assigned to a pointer, then pointer is pointing to nothing.

WHAT IS null pointer and void pointer?

A null pointer is basically a null value assigned to a pointer of any data type whereas a void pointer is a data type which remains void as long as an address of a data type is not assigned to it. Null pointer does not contain a reference of any variable/value.

Is it possible to subtract a pointer from an object?

The standard acknowledges that possibility, though it explicitly defines the behavior only for an equality comparison between a pointer past the end of one object and a pointer to an adjacent object. See N15706.5.9 paragraphs 6-7. For subtraction, you should just assume the behavior is undefined.

READ:   Does the check engine light go off after you fix the problem?

Is it possible to increment a null pointer?

Apart from the possibility that incrementing a null pointer might just crash on some hardware, what about this: Since incrementing a null pointer is undefined behavour, the compiler checks whether a pointer is null and only increments the pointer if it isn’t a null pointer, so p + 1 is again a null pointer.

Why does my program crash when I call f(0) instead of 0?

The compiler is therefore allowed to remove the x == 0 check in the return statement and just return 1000000 / x. Which means your program crashes when you call f (0) instead of returning 0. Another assumption made was that if you increment a null pointer and then decrement it again, the result is again a null pointer. Wrong again.

How to find the distance between two pointers in an array?

The subtraction of two pointers in array will give the distance between the two elements. Let the address of first element i.e., is 1000 then address of second element a+1 will be 1004. Hence p1 = 1000 and p2 =1004. Thanks for contributing an answer to Stack Overflow!

Popular

  • Why are there no good bands anymore?
  • Does iPhone have night vision?
  • Is Forex trading on OctaFX legal in India?
  • Can my 13 year old choose to live with me?
  • Is PHP better than Ruby?
  • What Egyptian god is on the dollar bill?
  • How do you summon no AI mobs in Minecraft?
  • Which is better Redux or context API?
  • What grade do you start looking at colleges?
  • How does Cdiscount work?

Pages

  • Contacts
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2026 ProfoundQa | Powered by Minimalist Blog WordPress Theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT