Pointer Constants in C with Examples

This C Tutorial explains Pointer Constants in C and Define their Purposes with Examples.

Every location, specifically every byte, in memory has an address. This address itself is a constant, so is a pointer constant. Remember that left side of assignment operator ‘=’ must be a Modifiable LValue meaning that expression on the left side must represent some location in memory. Let’s take few examples of pointer constants below:

/*
 * ptr_const1.c -- Program shows if we can assign pointer constant some
 * value
 */
#include <stdio.h>
 
int main(void)
{
    int a = 50;
 
    printf("Address of a is %p and value of a is %3d\n", &a, a);
 
    &a = 100;  /* &a is a pointer constant*/
    printf("After &a = 100; Address of a is %p and value of a is %3d\n",
            &a, a);
 
    return 0;
}

Observe the output below:

ptr_const1.c: In function ‘main’:
ptr_const1.c:10:12: error: lvalue required as left operand of assignment

Notice the Type of ERROR occured! “lvalue required as left operand of assignment” because address of integer ‘a’ (&a) is a pointer constant, doesn’t point to any location/object in memory, so can’t be assigned with any value. O key, now, we turn next to see if we can convert pointer constant to modifiable lvalue? Consider the examples below:

advertisement
advertisement
/* 
 * ptr_const2.c -- Program shows if we can convert pointer constant to
 * lvalue
 */
#include <stdio.h>
 
int main(void)
{
    int a = 50;
 
    printf("\t\tAddress of a is %p and value of a is %3d\n", &a, a);
 
    *(&a) = 100;  /* &a is a pointer constant*/
    printf("After *(&a) = 100; Address of a is %p and value of a is %3d\n",
            &a, a);
 
    return 0;
}

Observe the output below:

		Address of a is 0x7fff8bf1054c and value of a is  50
After *(&a) = 100; Address of a is 0x7fff8bf1054c and value of a is 100

Notice that Indirection operator (*) converted the pointer constant ‘&a’ to a modifiable lvalue. See how expression *(&a) is interpreted as, go to the address ‘&a’ and access the location/value over there.

Note: Join free Sanfoundry classes at Telegram or Youtube
/*
 * ptr_const3.c -- Program shows how integer literal is type cast to
 * pointer constant to modifiable lvalue
 */
#include <stdio.h>
 
int main(void)
{
    printf("Program shows accessing the location by its address...\n");
 
    *(int *)10000 = 100;  
    /* integer literal 10000 type cast to constant pointer (int *)10000 */
    /*
     * constant pointer (int *)10000 then converted to pointer-to-integer
     * *(int *)10000
     */
    /* accessing address 10000, assigning 100 there */
 
    printf("After *(int *)10000 = 1000; value at address (int *)10000 is "
           "%d\n", *(int *)10000);
    return 0;
}

Output as:

advertisement
Program shows accessing the location by its address...
Segmentation fault (core dumped)

But this time, fortunately, even after integer literal converted to modifiable lvalue, program aborted! Because we tried to access the address beyond the storage allocated to our program. What if we could have been successful in accessing that address i.e. *(int *)10000? Simple, that location should have had rewritten with new value 100 and older contents if any, address, some value, address to some function etc.. were lost. This might even cause system unstable.

Uses of Pointer Constants

advertisement

Operating system uses Pointer Constants to interact with Hardware directly. For example, OS needs to communicate with input/output device controllers to start i/o operations and to obtain the results of prior operations. On most machines, interaction with input/output disk controllers is accomplished through reading and writing values at specific memory addresses. Instead of accessing the memory, however, these operations access the device controller interface, the addresses of which are known in advance.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.