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

How can i model a bi-directional net, How can I model a bi-directional net ...

How can I model a bi-directional net with assignments influencing both source and destination? Assign statement constitutes a continuous assignment. Changes on the RHS of stat

Address translation - computer architecture, Address translation: ...

Address translation: Compiler time : If it is known in advance that a program will reside at a particular location of primary memory, and then the compiler can be told to

Explain subscriber access to strowger systems, What are the basic approache...

What are the basic approaches to the design of subscriber access to Strowger systems? Describe them. A step by step switching system has three main parts as demonstrated in fig

Human computer interaction, Preamble The owner of the local shopping mal...

Preamble The owner of the local shopping mall, MaxiMart, has contracted you to assist in the design of an interactive directory. The interactive directory is to be permanently l

What is micro programmed control, What is micro programmed control? Mic...

What is micro programmed control? Micro programmed control is a scheme in which control signals are formed by a program same to machine language program.

8085 programs, what is the theory used to check whether a number is negativ...

what is the theory used to check whether a number is negative or positive?

C pograming, why we use void main in c progrmeing

why we use void main in c progrmeing

Data bus is bidirectional, Why address bus is unidirectional and data bus i...

Why address bus is unidirectional and data bus is bidirectional? Ans) Because there is no require address transaction among processor and peripheral device but data bus is req

Analog to Digital convertor, The A/D converter whose conversion time is ind...

The A/D converter whose conversion time is independent of the number of bits is ? Ans. Parallel conversion is the A/D converter whose conversion time is independent of the Numbe

Explain about diodes, Q. Explain about Diodes? A single pn-junction wit...

Q. Explain about Diodes? A single pn-junction with appropriate contacts for connecting the junction to external circuits is called a semiconductor pn-junction diode. The fundam

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