How to Declare and Initialize Pointers in C?

In this article, we will take a detailed look at pointers in the C programming language. We’ll cover what pointers are, how they are declared and initialized, and how they are used. By the end of this article, you should have a strong understanding of pointers and be able to use them in your own programs.

What are Pointers?

A pointer is a variable that stores the address of another variable. Pointers are commonly used in C for a variety of purposes, such as passing arguments to functions, working with dynamic memory, and manipulating data structures like strings and arrays.

Hero image with two people with a computer showing some code vector image

Pointers are declared using the * operator. For example, the following code declares a pointer to an int:

int *ptr;

The * operator is known as the “dereference” operator. When it is applied to a pointer, it returns the value of the variable that the pointer points to. In the example above, if ptr is assigned the address of an int variable, then *ptr will return the value of that int variable.

Pointers can be assigned the address of variables of any data type, including other pointers. For example, the following code declares a pointer to a pointer to an int:

int **ptr;

Declaring Pointers

A pointer can be declared by using the * operator. For example,

int *ptr; //pointer to an integer

Here, the ptr is a pointer variable that points to an integer variable.

The data type of a pointer must be the same as the data type of the variable to which it points. For example,

int *ptr; //pointer to an integer

float *ptr; //pointer to a float

char *ptr; //pointer to a character

Initializing Pointers

A pointer variable can be initialized in two ways:

  1. Static initialization
  2. Dynamic initialization

Static Initialization

In static initialization, the pointer variable is initialized at compile time. For example,

int x = 10;

int *ptr = &x; //static initialization

Here, the ptr is a pointer variable that points to the variable x. The value of ptr is the address of x.

Dynamic Initialization

In dynamic initialization, the pointer variable is initialized at run time. For example,

int *ptr;

ptr = (int *)malloc(sizeof(int)); //dynamic initialization

Here, the ptr is a pointer variable that is initialized at run time. The malloc() function is used to allocate the memory at run time.

Accessing Pointers

A pointer variable can be accessed in two ways:

  1. Direct Access
  2. Indirect Access

Direct Access

In direct access, the value of the pointer variable is accessed. For example,

int x = 10;

int *ptr = &x;

printf("%d", ptr); //direct access

The output of the above code is 10.

Indirect Access

In indirect access, the value of the variable to which the pointer points is accessed. For example,

int x = 10;

int *ptr = &x;

printf("%d", *ptr); //indirect access

The output of the above code is 10.


Pointers and Arrays

In C, an array is a contiguous block of memory that stores a fixed number of values of the same data type. Arrays are declared using the following syntax:

data_type array_name[array_size];

For example, the following code declares an array of 10 ints:

int arr[10];

The square brackets after the array name indicate the size of the array. The size of an array must be a constant expression. This means that it must be a literal value (e.g., 10) or an expression that can be evaluated at compile time (e.g., 5 * 2).

Array elements are accessed using their index, which is their position in the array. Array indices start at 0, so the first element in the array above is arr[0], the second element is arr[1], and so on.

It is also possible to declare pointers to arrays. For example, the following code declares a pointer to an array of 10 ints:

int (*ptr)[10];

This syntax may look a bit confusing, but it’s actually not too bad once you get used to it. The parentheses are necessary because the [] operator has higher precedence than the * operator. Without the parentheses, the declaration would be interpreted as a pointer to an int, followed by an array of 10 ints.

To initialize a pointer to an array, you simply need to assign it the address of the array. For example, the following code initializes ptr from the previous example with the address of arr:

ptr = &arr;

To access an element of an array through a pointer, you use the same square bracket notation that you would use for a regular array. For example, the following code will print the first element of arr:

printf("%d", (*ptr)[0]); // Prints the first element of arr

As you can see, the * operator is used to “dereference” the pointer and get the value of the variable it points to, which in this case is the array. The square brackets are then used to access the element of the array.

Pointers and Strings

In C, a string is a sequence of characters that is terminated by a null character (‘\0’). For example, the following string is terminated by the null character:

char str[] = "Hello, world!";

Strings can be declared using the following syntax:

char array_name[array_size];

For example, the following code declares a string of 10 characters:

char str[10];

It is also possible to declare pointers to strings. For example, the following code declares a pointer to a string of 10 characters:

char (*ptr)[10];

To initialize a pointer to a string, you simply need to assign it the address of the string. For example, the following code initializes ptr from the previous example with the address of str:

ptr = &str;

To access a character of a string through a pointer, you use the same square bracket notation that you would use for a regular array. For example, the following code will print the first character of str:

printf("%c", (*ptr)[0]); // Prints the first character of str

As you can see, the * operator is used to “dereference” the pointer and get the value of the variable it points to, which in this case is the string. The square brackets are then used to access the character of the string.

Conclusion

In this article, we have taken a detailed look at pointers in the C programming language. We have covered what pointers are, how they are declared and initialized, and how they are used. By the end of this article, you should have a strong understanding of pointers and be able to use them in your own programs.

Harshita Raj
Harshita Raj

Leave a Reply

Your email address will not be published. Required fields are marked *