Simple Pointer Program

CCBeginner
Practice Now

Introduction

This lab will teach you the basics of pointers in C language. You will learn how to create a pointer, access the address and value of a variable using pointers, and print the values using format specifiers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123334{{"`Simple Pointer Program`"}} c/comments -.-> lab-123334{{"`Simple Pointer Program`"}} c/variables -.-> lab-123334{{"`Simple Pointer Program`"}} c/data_types -.-> lab-123334{{"`Simple Pointer Program`"}} c/operators -.-> lab-123334{{"`Simple Pointer Program`"}} c/memory_address -.-> lab-123334{{"`Simple Pointer Program`"}} c/pointers -.-> lab-123334{{"`Simple Pointer Program`"}} c/function_declaration -.-> lab-123334{{"`Simple Pointer Program`"}} end

Declare a variable

Begin by declaring an integer variable named var and set its value to 24. This variable will later be accessed using a pointer.

int var = 24;

Declare a Pointer

Declare a pointer variable named p that points to an integer value.

int *p;

Assign Address to Pointer

Point the pointer variable p to the address of the variable var using the reference operator &.

p = &var;

Output Address of Variable

To output the address of the variable var, use the format specifier %x.

printf("\n\nAddress of var variable is: %x \n\n", &var);

Output Address Stored in Pointer

To output the address stored in the pointer variable p, use the format specifier %x.

printf("\n\nAddress stored in pointer variable p is: %x", p);

Access and Output Value of Variable

To access the value of the variable var using the pointer variable p, use the dereference operator *.

printf("\n\nValue of var variable or the value stored at address p is %d ", *p);

Complete Code

Write the complete code in the main.c file in the ~/project/ directory.

#include <stdio.h>

int main()
{
    int var = 24;   // actual variable declaration
    int *p;

    p = &var;   // storing address of int variable var in pointer p

    printf("\n\nAddress of var variable is: %x \n\n", &var);

    // address stored in pointer variable
    printf("\n\nAddress stored in pointer variable p is: %x", p);

    // access the value using the pointer variable
    printf("\n\nValue of var variable or the value stored at address p is   %d ", *p);

    return 0;
}

Summary

Pointers are very powerful in C programming for their ability to access and manipulate memory. By using pointers, you can access and manipulate variables by their addresses directly, which can greatly enhance program efficiency and flexibility. With the knowledge gained in this lab, you can now begin to explore more advanced applications of pointers in your programming endeavors.

Other C Tutorials you may like