Docsity

Understanding Pointers in C++: Uses, Addresses, and Variables, Exercises of Computer Programming

Partial preview of the text

Download Understanding Pointers in C++: Uses, Addresses, and Variables and more Exercises Computer Programming in PDF only on Docsity!

POINTERS

๎€Š What are pointers for? Here are some common uses: ๎€Š Accessing array elements ๎€Š Passing arguments to a function when the function needs to modify the original argument ๎€Š Passing arrays and strings to functions ๎€Š Obtaining memory from the system ๎€Š Creating data structures such as linked lists

POINTER

๎€Š Pointers are much more commonly used in C++ (and C) than in many other languages ๎€Š In some situations pointers provide an essential tool for increasing the power of C++ ๎€Š Although you can do a lot of programming in C++ without using pointers, you will find them essential to obtaining the most from the language.

#include using namespace std; int main() { int var1 = 11; int var2 = 22; int var3 = 33; cout << &var1 << endl << &var2 << endl << &var3 << endl; Return 0; }

#include using namespace std; int main() { int var1 = 11; int var2 = 22; cout << &var1 << endl << &var2 << endl << endl; int* ptr; //pointer to integers ptr = &var1; //pointer points to var cout << ptr << endl; //print pointer value ptr = &var2; cout << ptr << endl; return 0; } docsity.com

POINTER VARIABLES

๎€Š Whatโ€™s wrong with the idea of a general- purpose pointer type that holds pointers to any data type? If we called it type pointer we could write declarations like ๎€Š pointer ptr; ๎€Š The problem is that the compiler needs to know what kind of variable the pointer points to. (Weโ€™ll see why when we talk about pointers and arrays.) The syntax used in C++ allows pointers to any type to be declared.

Accessing the Variable Pointed To

๎€Š Suppose that we donโ€™t know the name of a variable but we do know its address. Can we access the contents of the variable? ๎€Š There is a special syntax to access the value of a variable using its address instead of its name. Hereโ€™s an example program

#include using namespace std; int main() { int var1, var2; int* ptr; ptr = &var1; //set pointer to address of var *ptr = 37; //same as var1= var2 = *ptr; //same as var2=var cout << var2 << endl; return 0; }

๎€Š Remember that the asterisk used as the indirection operator has a different meaning than the asterisk used to declare pointer variables. The indirection operator precedes the variable and means value of the variable pointed to by. The asterisk used in a declaration means pointer to. ๎€Š int* ptr; //declaration: pointer to int ๎€Š (^) *ptr = 37; //indirection: value of variable pointed to by ptr

Pointers and Arrays

#include using namespace std; int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << intarray[j] << endl; return 0; }

#include using namespace std; int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << *(intarray+j) << endl; return 0; }

#include using namespace std; int main() { int intarray[] = { 31, 54, 77, 52, 93 }; //array int* ptrint; ptrint = intarray; for(int j=0; j<5; j++) cout << *(ptrint++) << endl; return 0; }

LAB TASK

Write a program to assign a value to a variable

using its pointer variable. Print out the value

using the variable name and also print out the

memory address of the variable using the pointer

variable?