Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Address arithmetic

Address arithmetic

Mar 31 2013

Address arithmetic is a method of calculating the address of an object with the help of arithmetic operations on pointers and use of pointers in comparison operations. Address arithmetic is also called pointer arithmetic.

According to C and C++ language standards, the result address must remain strictly within the bounds of a single array object (or just after it). Adding or subtracting from a pointer moves it by a multiple of the size of the data type it points to. For example, assume we have a pointer to an array of 4-byte integers. Incrementing this pointer will increment its value by 4 (the size of the element). This effect is often used to increment a pointer to point at the next element in a contiguous array of integers.

Pointer arithmetic cannot be performed on void pointers because the void type has no size, and thus the pointed address cannot be added to. However, there are non-standard compiler extensions allowing byte arithmetic on void* pointers.

Pointers and integer variables are not interchangeable. Null constant is the only exception from this rule: it can be assigned to the pointer, and the pointer can be compared to null constant. As a rule, to show that null is a special value for the pointer, NULL constant is written instead of number "0".

Pointer arithmetic provides the programmer with a single way of dealing with different types: adding and subtracting the number of elements required instead of the actual offset in bytes. In particular, the C definition explicitly declares that the syntax A[i], which is the i-th element of the array A, is equivalent to *(A+i), which is the content of the element pointed by A+i. This also implies that i[A] is equivalent to A[i].

At the same time, powerful pointer arithmetic can be a source of errors. Address arithmetic is a restrictive stage in the process of mastering 64-bit systems for the old C/C++ program code can contain a lot of errors relating to the use of pointers. To learn more about this problem see the article "20 issues of porting C++ code on the 64-bit platform".

Because of the challenges of using pointers, many modern high level computer languages (for example, Java or C#) do not permit direct access to memory using addresses.

References

Popular related articles


Comments (0)

Next comments next comments
close comment form