C - Assigning Values by Using a Pointer

Introduction

The *pointer operator works both ways.

You can also set a variable's value.

Demo

#include <stdio.h> 

int main() /*from   w  w w.  ja v  a  2 s .c  o  m*/
{ 
    char a,b,c; 
    char *p; 

    p = &a; 
    *p = 'A'; 
    p = &b; 
    *p = 'B'; 
    p = &c; 
    *p = 'C'; 
    printf("Know your %c%c%cs\n",a,b,c); 
    return(0); 
}

Result

The code declares three char variables.

These variables are never directly assigned values anywhere in the code.

The p variable is initialized three times to the memory locations of variables a, b, and c.

Then the *p variable assigns values to those variables.

Related Topic