Chapter 2: Pointer in C++ - PDF
Document Details
![InvaluableFauvism6628](https://quizgecko.com/images/avatars/avatar-12.webp)
Uploaded by InvaluableFauvism6628
Assosa University
Tags
Summary
This document is an introduction to pointers in the C++ programming language. It explains pointer basics, syntax, and use cases such as accessing memory addresses and working with arrays. It uses C++ code examples to show how to define, declare, and use pointers.
Full Transcript
CHAPTER TWO Pointer Outline Address and pointer Pointer and array Pointer and function Pointer and string 11/29/2024 2 What is pointer in C++? Pointer is a variable in C++ that holds the address of another...
CHAPTER TWO Pointer Outline Address and pointer Pointer and array Pointer and function Pointer and string 11/29/2024 2 What is pointer in C++? Pointer is a variable in C++ that holds the address of another variable. A Pointer is a variable that stores or points to the memory address of another variable. In C++ programming, when we declare a variable, it allocates some space in the heap memory. – Each allocated space has a unique memory address which is used by the compiler to access the values stored in them. With the help of pointers, we can directly work with the memory address of variables. They have data type just like variables, – for example an integer type pointer can hold the address of an integer variable and a character type pointer can hold the address of char variable. 11/29/2024 3 What is pointer in C++…. Some C++ tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them. As you know every variable has a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. 11/29/2024 4 Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of pointers. The address of the variable you’re working with is assigned to the pointer variable that points to the same data type (such as an int or string). Syntax: datatype *var_name; For example: int *ptr; // ptr can point to an address which holds int data 11/29/2024 5 How to use a pointer? Define a pointer variable Assigning the address of a variable to a pointer using the unary operator (&) which returns the address of that variable. Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand. The reason we associate data type with a pointer is that it knows how many bytes the data is stored in. When we increment a pointer, we increase the pointer by the size of the data type to which it points. 11/29/2024 6 What is pointer in C++…. When the above code is compiled and executed, it produces the following result Output: Address of var1 variable: 0xbfebd5c0 Address of var2 variable: 0xbfebd5b6 11/29/2024 7 What is pointer in C++…. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is: type *varName; Here, type is the pointer's base type; it must be a valid C++ type and varName is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. 11/29/2024 8 Following are the valid pointer declaration: The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. int *p; is equivalent to the statement: int* p; which is equivalent to the statement: int * p; Thus, the character * can appear anywhere between the data type name and the variable name. 11/29/2024 9 Using Pointers in C++ There are few important operations, which we will do with the pointers very frequently. (a) We define a pointer variable. (b) Assign the address of a variable to a pointer. (c) Finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. 11/29/2024 10 Let's see the simple example of using pointers printing the address and value. Output: Address of number variable is:0x7ffccc8724c4 Address of p variable is:0x7ffccc8724c4 Value of p variable is:30 11/29/2024 11 Address of Operator (&) and Dereferencing Operator (*) In C++, the ampersand, &, called the address of operator, is a unary operator that returns the address of its operand. For example, given the statements: int x; int *p; Address of Operator (&) the statement: p = &x; assigns the address of x to p. That is, x and the value of p refer to the same memory location. C++ also uses * as a unary operator. When used as a unary operator, *, commonly referred to as the dereferencing operator or indirection operator, refers to the object to which its operand (that is, the pointer) points. For example, given the statements: the statement: cout