So, now let's begin with the Advanced Part of C Programming. The first concept, we are going to learn about is pointers. So what are pointers? A pointer provides access to the variable by using the address of the variable. A pointer variable stores the value of another variable. Why to use Pointers? # Pointers are used when we pass arguments in the functions. They are two methods to call the function, you will learn about them in next tutorial, 1. Call by Value and 2. Call by reference. The Call by reference works with pointers. Because in functions, the arguments we pass are formal arguments. So once the function returns the value or gets close, the variables are destroyed. To prevent it, we use Pointers. Example : Swapping of two variables using Pointers How to declare Pointer Variable? dataType *pointerName; That's all, and a variable named pointerName will be created, which capable to store the address of another variable. How to store address of the Variable in Pointer Variable? To store the address, first declare the variable whose address is to be stored. And then declare pointer variable and assign the address value of the variable to it using "&". Let's see how. int x = 10; int *ptr; ptr = &x; Explaination : Here, x is a variable in which 10 value is stored. Then pointer variable named ptr is declared. Then the address value of x is stored in variable ptr using "&".
Next
« Prev Post
« Prev Post
Previous
Next Post »
Next Post »
Subscribe to:
Post Comments (Atom)
0 Comment