Unit 3 - 1 - Functions and Pointers PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an introduction to functions and pointers in C++. It covers topics such as pointer declarations, common pointer declarations, operators with pointers, and different types of pointers. The document also includes examples of C++ code.
Full Transcript
Functions and pointers Introduction to pointers Pointer is a variable that stores address of another variable A pointer variable declaration is: type *variableName; int *a; Uses in the C++ language : To create dynamic data structures (e.g. linked lists) To access information sto...
Functions and pointers Introduction to pointers Pointer is a variable that stores address of another variable A pointer variable declaration is: type *variableName; int *a; Uses in the C++ language : To create dynamic data structures (e.g. linked lists) To access information stored in arrays To optimize programs to run faster and/or use less memory Direct Hardware Interaction: Pointers facilitate low-level interaction with hardware, which is essential for systems programming, device drivers You do not need to replicate the data again and again, Why can’t we use normal variables Incrementing a pointer means pointing to the next item, which is different than incrementing an ordinary int variable. a = 10; c = a + 2; On an x86-64 architecture, a address occupies 64 bits, but an int is only 32 bits wide. If you try to store a pointer in an int, truncation will occur Common pointer declarations int *p1; double *p2 char *p3 float *p4 char *ptr; Variable a should be initialized (mandatory) int a = 10; void main() { int *ptr; //pointer declaration float a = 2.0; ptr = &a; //pointer initialization ptr = &a; // ERROR, type mismatch } C Pointers – Operators that are used with Pointers Address of”(&) Operator /Ampersand sign, An Address & we can display the address of a variable using ampersand sign printf("Address of var is: %p", &num); // //%p is a format specifier which is used for displaying the address in hex format how to store that address in some other variable? That’s where pointers comes into picture. Value at Address”(*) Operator The * Operator is also known as Value at address operator. How to declare a pointer? int *p1 double *p2 char *p3 float *p4 ptr is pointer variable that stores the address of 10, which is 2000 Pointer variable also has its own address, which is 4000 https://www.studytonight.com/c/declaring-and-initializing-pointer.php The data type of pointer and the variable must match, an int pointer can hold the address of int variable, similarly a pointer declared with float data type can hold the address of a float variable Demo storeAddress= Prints the address of stored in *storeAddress storeAddress = Value that is in the address stored in p Quicktip: * is used in passwords to hide, so it holds the actual value Types of pointers in c++ 1. Null Pointer 2. Void Pointer 3. Wild Pointer A generic pointer type that can An uninitialized pointer that points to A pointer that does not an arbitrary memory location. point to any valid memory hold the address of any data type. Accessing or dereferencing a wild location. pointer leads to undefined behavior. Cannot be dereferenced It is initialized with nullptr in directly; it must be typecast to a modern C++ or NULL in specific type. int *ptr; // Uninitialized pointer// older C++. cout