Week 3- Pointers and Dynamic Arrays - Tagged.pdf

Full Transcript

PROBLEM SOLVING & PROGRAMMING II C++ PROGRAMMING Pointer Dynamic Arrays SOIT Gloren Sison-Fuentes, CoE-MEM POINTERS  A pointer is the memory address of a variable.  A pointer is a construct that gives you more control of the computer’s memory.  computer’s memory is...

PROBLEM SOLVING & PROGRAMMING II C++ PROGRAMMING Pointer Dynamic Arrays SOIT Gloren Sison-Fuentes, CoE-MEM POINTERS  A pointer is the memory address of a variable.  A pointer is a construct that gives you more control of the computer’s memory.  computer’s memory is divided into numbered memory locations (called bytes)  and that variables are implemented as a sequence of adjacent memory locations.  In the previous lectures, we have not given many methods to control the amount of memory used in a program.  In particular, in all of the programs we have looked at so far, a certain amount of memory is reserved for each declared variable at compilation time, and this memory is retained for the variable as long as the program or block in which the variable is defined is active. SOIT Gloren Sison-Fuentes, CoE-MEM C/C++ uses pointers a lot. Why?: It is the only way to express some computations. It produces compact and efficient code. It provides a very powerful tool. C uses pointers explicitly with: Arrays, Structures, Functions. What is a Pointer? A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the ``address of a variable''. The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''. Assignments with Pointers Using the Operators “*" and "&“ Given a particular data type, such as "int", we can write assignment statements involving both ordinary variables and pointer variables of this data type using dereference operator "*" the (complementary) address-of operator "&" NOTE: "*" means "the variable located at the address", "&" means "the address of the variable". POINTERS DECLARATION SYNTAX Type_Name *Variable_Name1, *Variable_Name2,...; EXAMPLE double *pointer1, *pointer2; Declaring Pointers A pointer is just the memory address of a variable, so that a pointer variable is just a variable in which we can store different memory addresses. Pointer variables are declared using a "*", and have data types like the other variables we have seen. For example, the declaration int *number_ptr; states that "number_ptr" is a pointer variable that can store addresses of variables of data type "int". A useful alternative way to declare pointers is using a "typedef" construct. For example, if we include the statement: typedef int *IntPtrType; we can then go on to declare several pointer variables in one line, without the need to prefix each with a "*": IntPtrType number_ptr1, number_ptr2, number_ptr3; Pointer Types There is a bit of an inconsistency (or at least a potential for confusion) in how C++ names pointer types. If you want a parameter whose type is, for example, a pointer to variables of type int, then the type is written int*, as in the following example: void manipulatePointer(int* p); If you want to declare a variable of the same pointer type, the * goes with the variable, as in the following example: int *p1, *p2; The compiler does not care whether the * is attached to the int or the variable name, so the following are also accepted by the compiler and have the same meanings: void manipulatePointer(int *p);//Accepted but not as nice. int* p1, *p2;//Accepted but dangerous. The first versions are much clearer. Note that when declaring variables there must be one * for each pointer variable. Consider the effect of the following code: int x = 1, y = 2; Assignments x = 1 and y = 2 obviously int *ip; load these values into the variables. ip is declared to be a pointer to an integer ip = &x; and is assigned to the address of x (&x). So ip gets loaded with the value 100. y = *ip; x 1 100 100 ip 1000 *ip = 3; y 2 200 y=*ip x=ip Next y gets assigned to the contents of ip. In this example ip x 100 currently points to memory location 100 100 ip 100 -- the location of x. So y gets assigned to the values of x. y 1 1000 x 1 100 100 ip 1000 y 1 ip=3 200 x 3 100 100 ip 1000 y 2 Another Example: Consider the following code: v1 = 0; p1 = &v1; *p1 = 42; cout

Use Quizgecko on...
Browser
Browser