review_pointers

review_pointers - Review of pointers in functions A pointer...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Review of pointers in functions A pointer is a data item whose value is the address in the memory of some other value. Pointers allow us to refer to a large data structure in a compact way. No matter how large the data structure grows, it will reside somewhere in the computer’s memory and therefore would have an address. We can use the address as a short hand for the complete value. All we need to store is an integer, irrespective of the size of the structure. So we may talk about a complete list with 5 nodes or 100 nodes with just one address. Pointers facilitate sharing data between different parts of the program. When we pass the address of some data value from one function to another, both functions have access to the same data. Pointers make it possible to reserve new memory during program execution (Dynamic allocation). We have already seen that pointers can also be used to record relationship among data items (such as nodes in a linked list). Suppose we want to point to an address where we have kept 45. *p=45;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
means that the object pointed to by p is assigned the value 45. Now consider q = &y; Here q is assigned the address of object y. So q points to object y. In the following statement r is assigned the value of the object pointed to by q. r= *q; Basically this means that r and q are equivalent and referring to the same object. The addresses of variables can be used as arguments to functions ( call-by- reference ). Instead of ordinary variables , now we can use pointers to form the parameter list in the functions definition.
Background image of page 2
235 a n M x 3.145 char a; int n; float x; p q r char *p; int *q; float *r;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A good example is to carry out swapping of values of two variables through a function call. int main()
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

review_pointers - Review of pointers in functions A pointer...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online