DICT 113 Lecture 9- Pointers in C Programming (1).pptx

Document Details

RefreshedPythagoras2203

Uploaded by RefreshedPythagoras2203

Daystar University

Tags

C programming pointers data structures

Full Transcript

DICT 113 STRUCTURED PROGRAMMING Lecture 9: Pointers in C Programming Recall: Passing parameters to a function Methods of parameters passing: 1.) Call by reference. 2.) Call by value. Recall: Passing parameters to a function 1.) Call by reference: In this method of para...

DICT 113 STRUCTURED PROGRAMMING Lecture 9: Pointers in C Programming Recall: Passing parameters to a function Methods of parameters passing: 1.) Call by reference. 2.) Call by value. Recall: Passing parameters to a function 1.) Call by reference: In this method of parameter passing , original values of variables are passed from calling program to function. Thus, Any change made in the function can be reflected back to the calling program. 2.) Call by value. In this method of parameter passing, duplicate values of parameters are passed from calling program to function definition. Thus, Any change made in function would not be reflected back to the calling program. // C program to illustrate Pointers #include void geeks() { int var = 10; // declare pointer variable int* ptr; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf("Value at ptr = %p \n", ptr); printf("Value at var = %d \n", var); printf("Value at *ptr = %d \n", *ptr); } // Driver program int main() { geeks(); return 0; } Output Value at ptr = 0x7fff1038675c Value at var = 10 Value at *ptr = 10 Introduction to 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. Introduction 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. Syntax The of C isPointers syntax of pointers 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 1. 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. How to Use Pointers 2. Pointer Initialization Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( & ) address of operator to get the memory address of a variable and then store it in the pointer variable. Example int var = 10; int * ptr; ptr = &var; We can also declare and initialize the pointer in a single step. This method is called pointer definition as the pointer is declared and initialized at the same time. Example int *ptr = &var; Note: It is recommended that the pointers should always be initialized to some value before starting using it. Otherwise, it may lead to number of errors. How to Use Pointers 3. Pointer Dereferencing Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration. How to Use Pointers 3. Pointer Dereferencing Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration. Types of Pointers in C Pointers in C can be classified into many different types based on the parameter on which we are defining their types. If we consider the type of variable stored in the memory location pointed by the pointer, then the pointers can be classified into the following types: Types of Pointers in C 1. Integer Pointers As the name suggests, these are the pointers that point to the integer values. Syntax int *ptr; These pointers are pronounced as Pointer to Integer. Similarly, a pointer can point to any primitive data type. It can point also point to derived data types such as arrays and user- defined data types such as structures. Types of Pointers in C 2. Array Pointer Pointers and Array are closely related to each other. Even the array name is the pointer to its first element. They are also known as Pointer to Arrays. We can create a pointer to an array using the given syntax. Syntax char *ptr = &array_name; Pointer to Arrays exhibits some interesting properties which we discussed later in this article. Types of Pointers in C 3. Structure Pointer The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. It can be declared in the same way as we declare the other primitive data types. Syntax struct struct_name *ptr; In C, structure pointers are used in data structures such as linked lists, trees, etc. Types of Pointers in C 4. Function Pointers Function pointers point to the functions. They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code. Let’s consider a function prototype – int func (int, char), the function pointer for this function will be Syntax int (*ptr)(int, char); Note: The syntax of the function pointers changes according to the function prototype. Types of Pointers in C 5. Double Pointers In C language, we can define a pointer that stores the memory address of another pointer. Such pointers are called double-pointers or pointers-to- pointer. Instead of pointing to a data value, they point to another pointer. Syntax datatype ** pointer_name; Dereferencing Double Pointer *pointer_name; // get the address stored in the inner level pointer **pointer_name; // get the value pointed by inner level pointer Note: In C, we can create multi-level pointers with any number of levels such as – ***ptr3, ****ptr4, ******ptr5 and so on. Uses of Pointers  To return multiple values from a function in an indirect manner, call by reference is used  To access or manipulate elements of an array or string  Passing address of large objects  For dynamic allocation of memory for an object when the program is executing  Helps in easy storage and accessibility of data in memory  Function pointers facilitates implementation of callback mechanisms through which a library function can call our function  Storage of parameters in specific and pre- #include int main() { int num; int *pNum; pNum=& num; num=100; //access value and address using variable num printf("Using variable num:\n"); printf("value of num: %d\naddress of num: %u\n",num,&num); //access value and address using pointer variable num printf("Using pointer variable:\n"); printf("value of num: %d\naddress of num: %u\n",*pNum,pNum); return 0; } Output Using variable num: value of num: 100 address of num: 2199496132 Using pointer variable: value of num: 100 address of num: 2199496132

Use Quizgecko on...
Browser
Browser