














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to pointers in c++ programming language. It explains the common uses of pointers, such as accessing array elements, passing arguments to functions, and creating data structures. The document also covers the concept of addresses and pointer variables, and demonstrates how to access the variable pointed to by a pointer. Examples and exercises are included.
Typology: Exercises
1 / 22
This page cannot be seen from the preview
Don't miss anything!















๎ 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
๎ 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
#include
๎ 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.
๎ 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
๎ 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
#include
#include
#include