Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pointers in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 362
    Comment on it

    C has an interesting and powerful feature of pointers. It differs it from other languages. As the name suggests it points to something. Whenever we declare some data type it holds some memory. If we define a variable ptr then, &ptr is the address in memory.


    Example:


      #include 
      int main(){
        int ptr = 12;
        printf("Value: %d\n",ptr);
        printf("Address: %d",&ptr);
        return 0;
      }
      

    In above code, value 12 is stored in some memory location. ptr is the name for that location. The address of the memory can be presented by &ptr. When we hold the address in a variable we call it a pointer. Pointer variable are the special types of variables. They hold memory address not the data. In simple terms a variable that holds address value is called a pointer variable or simply a pointer. A pointer variable contains the address in memory of another variable, object, or function.


    How to declare Pointer:


    Pointer variables are declared using a data type followed by an asterisk(*) and then the pointer variables name.

    data_type* pointer_variable_name;

    int* ptr;

    Example:

    /* Source code*/
      #include 
      int main(){
         int* ptra;
         int a;
         a = 5;
    
         printf("Address of a:%d\n",&a);
         printf("Value of a:%d\n\n",a);
    
         ptra = &a;  # assign address of a in pointer variable
    
         printf("Address of pointer ptra:%d\n",ptra);
         printf("Content of pointer ptra:%d\n\n",*ptra);
    
         a = 8; 
         printf("Address of pointer ptra:%d\n",ptra);
         printf("Content of pointer ptra:%d\n\n",*ptra);
    
         *ptra = 2;
         printf("Address of ptra:%d\n",&ptra);
         printf("Value of ptra:%d\n\n",ptra);
         return 0;
      }
    

    *NOTE: Address location will be different when you run this program

    Output:

    Address of a:-845878428 Value of a:5

    Address of pointer ptra:-845878428 Content of pointer ptra:5

    Address of pointer ptra:-845878428 Content of pointer ptra:8

    Address of ptra:-845878424 Value of ptra:-845878428

    Advantages of pointers:

    • Provides direct access to memory
    • Pointers helps us building complex data structures like stack, queues, trees etc.
    • Allows resizing dynamically allocated memory block.
    • Pointers Reduce the execution time of program
    • Provides an alternate way to access array elements
    • Pointers allows dynamic memory allocation and deallocation

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: