Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

C Tutor: How to Use Pointers in C

Contents

Do you have a difficult time understanding what a pointer is? Want to know how they work? Read through this article to learn how to use pointers in C.

Introduction

If you are new to pointers, you may find them difficult to understand. However, with a few good examples and explanations, you’re on your way to learn how to use pointers in C.

C language is a high-level programming language developed extensively at Bell Labs by Denise Ritchie. It has distinct advantages over other programming languages, and one of these advantages is easy memory access and manipulation through pointers.

What is a Pointer in C?

As its name suggests, a pointer points to a memory location in C. The pointer can point to any type of data, or even code. It can point to a variable, a function, an array, or a structure, to name a few. Because of this, pointers are really powerful tools to help you map tangible values to or from memory locations.

Above, you can see the difference between an integer variable versus a pointer variable. On the left, the integer variable X holds its numerical value 0x0A while on the right, an integer pointer variable y holds a numerical value of 0x1002. This value is a memory location. Incidentally, this memory location holds an integer value of 0x0C in RAM as can be seen on the left. Note that pointer variables declared as integers point to integer values while a pointer variable’s length can be as long as what the system architecture permits it to be; because it’s a memory location.

How to Declare and Initialize a Simple Pointer Variable

Here is an example of variable pointers C and B declared to hold integer and character values.

				
					int *C;
char *B;
				
			

However, it is not clear where they point to yet. With this, you have to initialize the pointer first. Doing this before doing anything with the pointers is important to not corrupt your system memory. To do this, you can assign a value to a variable and then map its value back to the pointer through the & – address of operator.

				
					int c = 10;
char b = 'A';

C = &c;
B = &b;
				
			

With this, you’ll have pointer C pointing to the memory address of integer c, which holds the value 10. Similarly, Pointer B points to the memory address of character b, which holds the value ‘A’.

Why Use Pointers

When using pointers, since memory locations are involved, you can dynamically allocate or retrieve data to and from memory elements without the constraints of an element’s data type size. As an example, you can comfortably assign pointers to variable-sized structures or arrays. You can also use pointer arithmethic that simplifies access to memory locations such as going through a list. These are the advantages gained in using pointers.

How to Use Pointers

The value in using a pointer is by dereferencing it. When you dereference a pointer, you get the actual value the memory location holds instead of the memory location address. To access this data, you’ll need the * dereferencing operator before the pointer variable. Below is an example code for printing the values of the memory locations of the pointers we previously declared and initialized.

				
					int *C;
char *B;

int c = 10;
char b = 'A';

void app_main(void)
{

    C = &c;     // initialize pointers
    B = &b;

    printf("Pointer C points to a value of %i\n", *C);
    printf("Pointer B points to a value of %c\n", *B);
    printf("\n");

    while(1);
}
				
			

The output is simply:

				
					Pointer C points to a value of 10
Pointer B points to a value of A
				
			

You can also operate on the values pointed to by the pointers:

				
					int *C;
char *B;

int c = 10;
char b = 'A';

void app_main(void)
{

    C = &c;     // initialize pointers
    B = &b;


    *C = *C * 10;
    *B = 'D';

    printf("Pointer C points to a value of %i\n", *C);
    printf("Pointer B points to a value of %c\n", *B);
    printf("\n");

    while(1);
}
				
			

The output would be:

				
					Pointer C points to a value of 100
Pointer B points to a value of D
				
			

Other Types of Pointers

There are other types of pointers  besides variable pointers and this will be discussed on the upcoming blogs.

<<<How To Use Functions in C

How to Use Function Parameters in C>>>

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top