Categories
Derived Data Type

Pointers

A pointer can be defined as a variable that stores the address of other variables. This address signifies where that variable is located in the memory. If a is storing the address of b, then a is pointing to b. The data type of a pointer must be the same as the variable whose address it is storing. 

Syntax

type *pointer_name;

Description of the syntax

  • type: This is the data type that specifies the type of value to which the pointer is pointing.
  • pointer_name: This is the name of the pointer. To specify the name of a pointer, you must follow the same rules which are applicable while declaring a usual variable in C. Apart from these rules, a pointer must always be preceded by an asterisk(*).

Example

The following example illustrates pointers in C.

#include <stdio.h>

int main()

{

      // array declaration and initialization.

int arr[4] = {50, 100, 150, 200}; 


    // int type pointer variable declaration.


int *ptr; 

    // Assign the address of arr[0] to ptr.

ptr = arr;

for (int i = 0; i < 4; i++)

{

printf("Value of *ptr = %d\n", *ptr);

printf("Value of ptr = %p\n\n", ptr); 

        // increment pointer ptr by 1.

ptr++;

}

}



Data_Types_in_C_8.

In the above example, we have declared a pointer ptr that is holding the address of the array arr. We have looped through the array and printed the value at each index along with the address of the element.

Those were the derived data types in C. Moving forward, let us understand the user-defined data types in C.

Leave a Reply

Your email address will not be published. Required fields are marked *