Skip to main content

Posts

Multilevel pointers in c

A pointer can store the address of another pointer variable.  We need to do multiple dereference operation to get the original value. Consider the below example, int num = 100; int *ptr, **dptr;                                     addr: 1000                                 +-----------------+                                 |     val: 100    |                                 +-----------------+                                     name: num Address of num is 1000 Value of num is 100 Let us assign the address of num to ptr. ptr = #   ...

How to declare pointers in c?

Below is the general form of pointer declaration. datatype  * var_name; datatype   - type of data to which the pointer points to  *             - indirection operator(used to represent pointer) var_name - name of the pointer variable. The following are few pointer declarations: char  * char_ptr;  /* pointer to character */ int     *integer_ptr; /* pointer to integer */ float  *float_ptr;  /* pointer to float */ How to assign values to pointers in c? Below program illustrates how to assign values to pointers.   #include <stdio.h>   #include <string.h>   int main() {         char *char_ptr, str[32];         int *int_ptr, int_val;         float *float_ptr, float_val;         /* assigning values to non-pointer variables */         strcpy(str, "String");         int_val = 1...

Difference between call by value and call by reference

Difference between pass by value and pass by reference First Difference: Call by value:  Value of the variable is passed as argument Call by reference:  Address of the variable is passed as argument   /* call by value vs call by reference */   #include <stdio.h>   void callByVal(int num) {         num = 100;         return;   }   void callByRef(int *num) {         *num = 200;         return;   }   int main() {         int num = 10;         printf("Call by value:\n");         printf("Value of num before function call: %d\n", num);         /* passing the value of the variable as argument */         callByVal(num);         printf("Value of num after function call: %d\n", num);         printf("Call by reference:\n"); ...

How to pass a pointer as an argument to function in c?

If we pass arguments to function by reference, then the called function can alter the value of variable in the calling function. Basically, we pass address of the variables as arguments to the called function.  So, any modification to the value of the arguments of called function is carried out to the calling function. Below program explains how to pass pointer as argument to function.   #include <stdio.h>   void swap(int *num1, int *num2) {         int tmp;         /* printing the pointer(arguments) value */         printf("\nAddresses inside called function:\n");         printf("Pointer value of num1: 0x%x\n", (int)num1);         printf("Pointer value of num2: 0x%x\n", (int)num2);         /* swapping two numbers */         tmp = *num1;         *num1 = *num2;         *num2 = tmp;   ...

How to interpret complex pointer declarations in c?

The following is the order of precedence, operators and its associativity.    Precedence Order                   Operators                      Associativity      1 ()   []  ->  Left To Right 2   ++  --  *  &  Identifier   Right to Left We have a package named cdecl(compose C type declarations)  available in linux which can be used to interpret complex pointer declarations. How to install cdecl package?  jp@jp-VirtualBox:~/$ sudo apt-get install cdecl  The explain statement in cdecl decodes any given C declarations.  Below are the complex C pointer declarations. int  *num char  **str int ( *arr ) [ 15 ] int  *arr [ 15 ] int *fun ( ) int ( *fun ) ( int, int ) int ( *arr[] ) ( ) char ( *( *fun ( ) ) [ ] ) ( ) char ( *( *arr[ 5 ] ) ( ) )[ 7 ] Let us deco...

How pointers work in c?

A pointer is declared by putting an asterisk '*' before the variable name. int var;     /* variable declaration */ int *ptr;   /* pointer declaration */ Asterisk and ampersand are the frequently used pointer operators. & - used to get the address of the variable *  - used to get the value to which the pointer points to Let us understand the purpose of ampersand and asterisk operators. var    - variable of type integer &var  - pointer to an integer variable var ptr     - pointer to an integer *ptr   - integer Consider the following example, int var = 10;   name: var +-------------+ | value: 10  | +-------------+   addr: 3000 The above statement/diagram indicates that, var is the name of the variable and its address is 3000. The value at the address 3000 is 10 . int *ptr1, *ptr2; ptr1 = &var;   name: ptr1 +---------------+ | value: 3000 | +---------------+   addr: 6000 We have assigned the address of th...

Advantages and disadvantages of pointers in c

Benefits(use) of pointers in c: Pointers provide direct access to memory Pointers provide a way to return more than one value to the functions Reduces the storage space and complexity of the program Reduces the execution time of the program Provides an alternate way to access array elements Pointers can be used to pass information back and forth between the calling function and called function. Pointers allows us to perform dynamic memory allocation and deallocation. Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc. Pointers allows us to resize the dynamically allocated memory block. Addresses of objects can be extracted using pointers Drawbacks of pointers in c: Uninitialized pointers might cause segmentation fault. Dynamically allocated block needs to be freed explicitly.  Otherwise, it would lead to memory leak. Pointers are slower than normal variables. If pointers are updated with incorrect values, it might lead to memory co...