91-9990449935 0120-4256464 |
C PointersThe pointer in C language is a variable, it is also known as locator or indicator that points to an address of a value. Advantage of pointer1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions. 2) We can return multiple values from function using pointer. 3) It makes you able to access any memory location in the computer's memory. Usage of pointerThere are many usage of pointers in c language. 1) Dynamic memory allocationIn c language, we can dynamically allocate memory using malloc() and calloc() functions where pointer is used. 2) Arrays, Functions and StructuresPointers in c language are widely used in arrays, functions and structures. It reduces the code and improves the performance. Symbols used in pointer
Address Of OperatorThe address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable. Outputvalue of number is 50, address of number is fff4 Declaring a pointerThe pointer in c language can be declared using * (asterisk symbol). Pointer exampleAn example of using pointers printing the address and value is given below. As you can see in the above figure, pointer variable stores the address of number variable i.e. fff4. The value of number variable is 50. But the address of pointer variable p is aaa3. By the help of * (indirection operator), we can print the value of pointer variable p. Let's see the pointer example as explained for above figure. OutputAddress of number variable is fff4 Address of p variable is fff4 Value of p variable is 50 NULL PointerA pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value. It will a better approach. int *p=NULL; In most the libraries, the value of pointer is 0 (zero).
Next TopicC Pointer to Pointer
|