Pointer Definition in CWhat are Pointers?A pointer is a type of variable in the C language that is used to store the address of another variable. The pointers can be of any data type, such as int, float, char, string, array, or function. In C language, pointers are represented using the asterisk symbol (*). The size of the pointers depends on the architecture. For example, the size of the pointer will be 4 bytes for a 32-bit machine or 8 bytes for a 64-bit machine. Features of pointers in C language
Syntax for declaring a pointer in C language:
Array of pointersArray of pointers in C language is the collection of pointers and is represented as follows:
In C language, arrays and pointers are related as an array name behaves like a pointer constant. Pointer constant's value will be the address of the first element. For example, let's take an array named ptr then ptr and &ptr[0] represents the same meaning and we can use both of them. Assigning this value to a non-constant pointer to the array, we can access the elements of the array using this pointer. Let's see an example for the array of pointers with int data type Output: Elements of the array are 1 2 3 4 5 Here is another example for array of pointers with string data type Output: The locations of the strings are: 4310680 4310684 4310688 4310692 4310696 '&' and '*' Operators:'&' operator- When you assign a value to a variable, it is stored at a specific memory address. To access that memory address, we use the '&' operator. Thus, we can say that the '&' operator that is also known as the 'address of' operator or the reference operator, is used to give the memory address of a variable. Output: 0x77fee2365f099 // memory address of the variable 'marks'. (Note: Output varies from computer to computer as memory address assigned by your computer will be different.) Note: %p is a format specifier that is used to print the value of a pointer. Here &marks is a pointer. '*' operator- '*' operator or the dereference operator is used to get the value of a variable at a given address. '*' operator basically has two functions. Using '*' while declaration, it is used to create a pointer, else it is used as a dereference operator. Here is an example that shows both the functions of '*' operator: Output: 75 Types of Pointers in C language:NULL Pointers:Null Pointers are the pointers that have no value assigned to it. NULL pointers do not point to any function or object. It means that the pointer has not been assigned with any valid memory address. Output: The value inside the pointer ptr is: 0 Wild Pointers:When you don't initialize anything to the pointer, the pointer is called as a wild pointer. This type of pointer may lead to the crashing of the program because the pointer will assign to a random memory address. Void Pointers:Void pointers are the pointers that do not have any specific data type. It is created by using the void keyword. Dangling Pointers:When you de-allocate the memory of a pointer using the free() function, then that pointer is known as dangling pointer. Arithmetic Operations for Pointers in C language:Not all but only few set of operations can be performed on pointers. The Pointer Arithmetic refers to the set of operations that can be performed on the pointers. It is different from the ones that we use for regular mathematical expressions. The operations are as follows:
It is represented as ptr++; Output: Value of *ptr = 20 Value of *ptr = 30 Value of *ptr = 40
It is represented as ptr--; Output: Value of *ptr = 40 Value of *ptr = 30 Value of *ptr = 20
Let's see an example: Let's assume the memory address of val be 200, then the address of newval will be 208 instead of 202 that is, ptr+2* sizeof(int) where size of int is 4 bytes
Output: val2 is greater than val1 Advantages of Pointers:
Disadvantages of Pointers:
Next TopicStaff Definition |