One pointer variable be subtracted from another, Computer Engineering

Assignment Help:

Under what conditions can one pointer variable be subtracted from another?

Pointer subtraction isn't used very much, but can be handy to determine the distances between two array elements (i.e., the difference in the array indexes). You may not know exactly which element you're pointing to using pointer subtraction, but you can tell relative distances.

Pointer subtraction can subtract two pointers of the same type. The result is the distance (in array elements) between the two elements.  Both pointers must point to objects that are members of the same array. After the compiler subtracts the addresses of two pointers,  it  divides  the result  (in  bytes)  by  the  size  of  the  pointed-to  object.  Therefore, subtracting one pointer from another yields the number of elements between the two pointers. The formula used is rather simple. Assume that p1 and p2 are both pointers of type T *. Then, the value computed is:

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

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. The formula above still works even when p2 and p1 contain invalid addresses (because they contain some address).

The following program illustrates how pointer subtraction yields the number of elements between two pointers. short s_array[10], *s_ptr1 = &s_array[0], *s_ptr2 = &s_array[3];

main()

{

printf("The address in s_ptr1 is %u\n", s_ptr1);

printf("The address in s_ptr2 is %u\n", s_ptr2);

printf("\nSubtracting s_ptr2 from s_ptr1 yields %d\n", s_ptr1 - s_ptr2);

printf("Subtracting s_ptr1 from s_ptr2 yields %d\n", s_ptr2 - s_ptr1);

}

The output from the preceding program was as follows: The address in s_ptr1 is 14737480

The address in s_ptr2 is 14737486

Subtracting s_ptr2 from s_ptr1 yields -3

Subtracting s_ptr1 from s_ptr2 yields 3

Notice that, when pointer s_ptr2 is subtracted from pointer s_ptr1, the pointer subtraction yields a negative value because s_ptr2 locates an object with a higher address than the object pointed to by s_ptr1.

 


Related Discussions:- One pointer variable be subtracted from another

Explain post office protocol, Explain POP (Post Office Protocol). The P...

Explain POP (Post Office Protocol). The Post Office Protocol gives remote access to an electronic mail box. The protocol permits a user's mailbox to reside on a computer which

Linear model., what is linear model and its type

what is linear model and its type

Labels in the sub trees, A sorting method which uses the binary tree concep...

A sorting method which uses the binary tree concept such that label of any node is larger than all  the labels in the sub trees, is called A Sorting method which uses the binar

Explain about distributed systems, Q. Explain about distributed systems? ...

Q. Explain about distributed systems? A Distributed System in that Data, Process and Interface component of information System are distributed to many locations in a computer n

Prolog, Prolog: Still we can take our card game from the previous lect...

Prolog: Still we can take our card game from the previous lecture like a case study for the implementation of a logic-based expert system. So there the rules were: four cards

What is class and class diagram, What is class, class diagram? An objec...

What is class, class diagram? An object is an instance of a class. Class explains a group of objects with similar properties (attributes), behaviour (operations), kinds of rela

Explain in detail about the random scan display, Explain in detail about th...

Explain in detail about the Random Scan Display   This device using CRT directs the electron beam only to the parts of the screen where a picture is to be drawn. This kind of d

How much frequency is centered in typical human voice, Typical human voice ...

Typical human voice is centered around                   Hz. (A)  200-400                                     (B)  280-3000  (C)  400-600

Difference between search and search all, SEARCH is a sequential search fro...

SEARCH is a sequential search from the starting of the table. SEARCH ALL is a binary search, continually dividing the table in two halves until a match is found. SEARCH ALL is more

De morgan''s laws - artificial intelligence, De Morgan's Laws Continuin...

De Morgan's Laws Continuing with the relationship between  ∧  and  ∨ , we can also use De Morgan's Law to rearrange sentences involving negation in conjunction with these conne

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd