Partial preview of the text
Download Basics of C programming and more Study notes Computer Science in PDF only on Docsity!
UNIT -5 POINTERS 1. INTRODUCTION & UNDERSTANDING POINTERS Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. What is a Pointer in C? A pointer is defined as a derived data type that can store the address of other C variables or a memory location. We can access and manipulate the data stored in that memory location using pointers. As the pointers store the memory addresses, their size is independent of the type of data they are pointing to. This size of pointers only depends on the system architecture. Syntax The syntax of pointers is similar to the variable declaration in C, but we use the ( * ) dereferencing operator in the pointer declaration. datatype * ptr; where ¢ ptr is the name of the pointer. ¢ datatype is the type of data it is pointing to. The above syntax is used to define a pointer to a variable. We can also define pointers to functions, structures, etc. How to Use Pointers? The use of pointers can be divided into three steps: 1. Pointer Declaration 2. Pointer Initialization 3. Dereferencing Pointer Declaration In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use the ( * ) dereference operator before its name. Example int *ptr; The pointer declared here will point to some random memory address as it is not initialized. Such pointers are called wild pointers.