C Pointers: The Complete Guide for Beginners 2021

C Pointers: The Complete Guide for Beginners 2021

C pointers are simple and enjoyable to learn. Some C programming tasks are easier to complete with pointers, while others, such as dynamic memory allocation, are impossible to complete without them. To become a proficient C programmer, it is therefore vital to learn pointers. Let’s begin by studying them in little steps.

Every variable, as you may know, is a memory location, and each memory location has an address that can be accessed using the ampersand (&) operator, which denotes a memory address. Take a look at the sample below, which prints the addresses of the variables defined.

With the help of examples, you’ll learn about pointers, including what they are, how to use them, and common mistakes you could make when working with them.

In C and C++ programming, pointers are extremely useful. Let’s start with addresses in C programming before moving on to pointers.

Lets look the example below

When the preceding code is compiled and run, the following result is obtained:

What are Pointers?

The pointer is a variable whose value is the address of another variable, i.e. the memory location’s direct address. A pointer, like any other variable or constant, must be declared before it may be used to store any variable address. This pointer variable declaration takes the following general form:

The pointer’s base type is type, which must be a valid C data type, and the pointer variable’s name is var-name. The asterisk * that is used to declare a pointer is the same as the asterisk that is used to multiply. The asterisk, on the other hand, is used to indicate a variable as a pointer in this sentence. Take a look at a few examples of proper pointer declarations.

All pointer values, whether integer, float, character, or other, have the same data type: a lengthy hexadecimal number that denotes a memory address. The data type of the variable or constant to which the pointer points is the only difference between pointers of different data kinds.

How to Use Pointers?

Integer, float, character, and other pointer values all share the same data type: a long hexadecimal number that specifies a memory address. The sole distinction between pointers of different data types is the data type of the variable or constant to which the pointer points.

Lets Look the example below:

When the preceding code is compiled and run, the following result is obtained:

NULL Pointers

If you don’t have an exact location to assign to a pointer variable, it’s always a good idea to assign a NULL value to it. This is done when the variable is declared. A null pointer is a pointer that has been assigned the value NULL.

The NULL pointer is a zero-valued constant found in numerous standard libraries. Take a look at the following program:

When the preceding code is compiled and run, the following is the result:

Because the operating system reserves memory at address 0, most operating systems do not allow programs to access memory at that address. The memory address 0 has unique importance; it indicates that the pointer is not intended to point to a memory location that is accessible. However, a pointer with the null (zero) value is assumed to point to nothing by convention.

You can use a ‘if’ statement to check for a null pointer, as seen below:

Pointers in Detail

Pointers have a lot of notions, but they’re all simple, and they’re highly fundamental in C programming. Any C programmer should be familiar with the following key pointer concepts:

Sr.No.Concept & Description
1Pointer arithmetic
There are four arithmetic operators that can be used in pointers: ++, –, +, –
2Array of pointers
You can define arrays to hold a number of pointers.
3Pointer to pointer
C allows you to have pointer on a pointer and so on.
4Passing pointers to functions in C
Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.
5Return pointer from functions in C
C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

C Pointers and Arrays

You’ll learn about the relationship between arrays and pointers in C programming in this course. You’ll also learn how to use pointers to access array elements.

Check out these two topics before learning about the link between arrays and pointers:

  • C Arrays
  • C Pointers

Relationship Between Arrays and Pointers

An array is a collection of data that is arranged in a logical order. Let’s develop a program to print array element addresses.

Output

Between two successive items of array x, there is a difference of 4 bytes. It’s because int has a size of 4 bytes (on our compiler).

It’s worth noting that the addresses of &x[0] and x are identical. It’s because the variable x refers to the array’s first element.

It is evident from the preceding example that &x[0] is equivalent to x. Also, x[0] is the same as *x.

Similarly,

  • &x[1] is the same as x+1, and x[1] is the same as *(x+1).
  • &x[2] is the same as x+2 and x[2] is the same as *(x+2).
  • Essentially, &x[i] is the same as x+i, and x[i] is the same as *(x+i).

Example of Pointers and Arrays

When you run the code, you’ll get the following results:

We’ve declared an array x with six items. We utilized pointers to access the array’s elements.

In the vast majority of cases, array names devolve into pointers. Array names are transformed to pointers in simple terms. It is for this reason that pointers can be used to access array elements. However, keep in mind that pointers and arrays are not the same thing.

There are a few exceptions to the rule that array names do not devolve into pointers. For further information, see When does an array name not decay into a pointer?

Another Example of Arrays and Pointers

When you run the code, you’ll get the following results:

The third element’s address, &x[2, is allocated to the ptr pointer in this example. As a result, when we printed *ptr, we got 3.

The fourth element is obtained by printing *(ptr+1). Similarly, we may get the second element by writing *(ptr-1).

C Pass Addresses and Pointers

With the help of examples, you’ll learn how to give addresses and pointers as parameters to functions in this article.

Addresses can also be passed as arguments to functions in C programming.

We can use pointers in the function definition to accept these addresses. Because pointers are used to hold addresses, this is the case. Consider the following scenario:

Example of Pass Addresses to Functions

When you run the code, you’ll get the following results:

swap(&num1, &num2); is used to pass the addresses of num1 and num2 to the swap() function.

In the function definition, pointers n1 and n2 accept these parameters.

When *n1 and *n2 are modified in the swap() function, the main() function’s num1 and num2 are likewise changed.

*n1 and *n2 were switched inside the swap() procedure. As a result, num1 and num2 have been swapped.

Swap() does not return anything because its return type is void.

C Dynamic Memory Allocation

In this article, you’ll learn how to use standard library functions like malloc(), calloc(), free(), and realloc() to dynamically allocate memory in your C application ().

An array is a collection of a fixed number of values, as you may know. You can’t adjust the size of an array once it’s been declared.

It’s possible that the array you declared isn’t big enough. You can manually allocate RAM during runtime to remedy this problem. In C programming, this is referred to as dynamic memory allocation.

Malloc(), calloc(), realloc(), and free() are library functions that are used to dynamically allocate memory. The header file stdlib.h> defines these functions.

C malloc()

Memory allocation is referred to as “malloc.”

The malloc() method sets aside a memory block of the specified size. It also returns a void pointer that can be cast into any type of pointer.

The Syntax of malloc()

Example

The preceding statement sets aside 400 bytes of RAM. The reason for this is because float has a size of 4 bytes. The pointer ptr holds the address of the allocated memory’s initial byte.

If the memory cannot be allocated, the expression returns a NULL pointer.

C calloc()

Contiguous allocation is represented by the term “calloc.”

The malloc() function allocates memory but does not initialize it, whereas the calloc() function allocates memory but does not initialize it.

The Syntax of calloc()

Example:

The above statement allocates contiguous memory space for 25 float elements.

C free()

Memory that has been dynamically allocated with calloc() or malloc() does not get released on its own. To release the space, you must use free() directly.

The Syntax of free()

The space allocated in the memory pointed by ptr is freed with this statement.

The Example of malloc() and free()

Output

We’ve dynamically allocated RAM for n ints in this case.

C realloc()

You can use the realloc() function to alter the size of previously allocated memory if the dynamically allocated memory is insufficient or more than required.

Ptr is reallocated with a new size x in this case.

Example of realloc()

Output

Summary

To become a proficient C programmer, it is vital to learn pointers. A pointer is a variable whose value is the address of another variable’s direct address. Every C variable is a memory location, and each memory location has an address that can be accessed using the ampersand (&) operator.

Related Articles

Inquiries

If you have any questions or suggestions about C Pointers: The Complete Guide for Beginners 2021, please feel free to leave a comment below.

Leave a Comment