Pointers in C Language – How to Create and use pointers

pointers-in-c-programming-language-with-example-programs

Pointers in C – Introduction:

We have looked at the Strings in C language in earlier articles, In today’s article, We will learn about the Pointers in C programming. We will look at how to declare, initialize, and use pointers in C language with example programs. This article is going to be a long one with a detailed explanation. So let’s get started.

📢 This Program is part of the C Programming Language tutorials series.

What is a Pointer variable in C?

A Pointer is a special variable that holds the memory address of another variable.

📢 The pointer variable points to a memory address of another variable.

Before going dive deep into the pointers in C language it is important to know the basics of memory allocation.

We need to know

  • How the variables are stored in memory and What is meant by the memory address.
  • How to get the memory address of variables

How are Variables stored in Memory?

The main memory of a computer is called Random Access Memory (RAM), and it is used to store variables and other program-related information.

The computer RAM is made up of bytes that are ordered sequentially. Each byte has an index number, which is referred to as its address ( memory address ).

RAM-visualization

Let’s look at how a variable is stored in the memory.

If we create any variable, The C compiler needs to reverse memory for the variable in the primary memory(RAM).

In the C programming language, we normally specify the variable name together with the variable’s data type during the variable creation. So that the compiler knows how much memory(how many bytes) it should allocate for the said variable.

Let’s look at an example to make our understanding concrete.

int score = 300;

  • We have created a variable called score with the integer( int) data type.
  • As we already know the default Integer size in C is 4 Bytes. So the C compiler will go ahead and reserves the 4 consecutive Bytes in the memory
  • As specified earlier, This memory location will have an address. Let’s say the compiler allocated the address 0x7ffc4550e084 for the score variable.
  • Finally, It will update the score variable memory address (i.e 0x7ffc4550e084) with the value 300.
  • Now, We have created a variable named score , which is stored in the memory address 0x7ffc4550e084 and has a value 300.

Here is the graphical representation of how variables are stored in the C programming language.

how-variables-stored-in-RAM-in-C-programming-language

ℹ️ The Integer size varies depending on your system architecture.

Address of Operator(&) in C – How to get the memory address of variables :

We can get the memory address of any variables using the address of operator or reference operator.

The address of operator is denoted by the & ampersand symbol. This is a unary operator and can be used with a single operand.

Syntax of Address of Operator (&):

In order to get the memory address of the variable, Place the address of operator(&) before the variable name.

We can get the memory address of the score variable by using the &score ( address of operator)

📢We have used the address of times many times so far, If you recall, We always passed the variables with the memory address to the scanf functions.

scanf(“%d”, &num);

Let’s look at a program to get the address of a variable in c language.

We used the %p format specifier to print the memory address. You can check all available format specifiers in the following article.

Program Output:

Let’s compile and Run the program using GCC compiler or your favorite compiler.

memory-address-of-variable-in-c

The scorevariable is stored at the memory address 0x7fff0034a9f4in RAM.

📢If you run the above program, You might get a different value for the memory address.

Pointers in C Programming Language:

A pointer variable is a variable that stores the address of another variable.

Similar to other variables, The pointer variable also has the datatype, and variable name, and also occupies space in the memory.

Here is the pictorial representation of the pointer storing another variable memory address.

how-pointers-works-in-c-programming-language

Declaration of Pointer variables in C Language:

We can declare a pointer variable by using the * (Asterisk symbol). Here is the syntax of the pointer declaration.

Syntax of Pointer declaration:

Here

  • pointerName is the pointer name. This can be any valid C identifier.
  • * (Asterisk) specifies the pointerName is a pointer variable.
  • dataype is the datatype of the pointer. Do note that, This pointer points to datatype variable.

Let’s look at a few examples of the pointer variables.

We created a pointer variable called intPtr which is an integer pointer. The intPtr points to the integer variable.

Similarly, the chPtr is a character pointer and points to the character data.

So we need to create the pointer with the datatype the same as the data it is going to point.

Size of the Pointer variables in C:

The Pointer variables are also variables. So the pointers will also take up some space in the memory.

But unlike other normal variables, All Pointers(either it is integer pointers, character pointers, float pointers, etc.) store the memory address of another variable.

As the pointer variable stores only the memory address, The size of the pointer variable is 4 Bytes ( The pointer size will change based on your compiler and OS architecture).

📢All pointers store only the memory address, So the size of all pointers is the same. ( Generally, The size of the pointer variable is 4 bytes)

Initializing the Pointer variables:

By default, Pointer variables will have garbage values. So it is always recommended to initialize the pointer variables.

Here is the syntax to initialize the pointer variables

Here

  • pointerName is the name of the pointer
  • datatype is the data type of the pointer and variableName.
  • * (Asterisk) denotes we are creating the pointer variable
  • variableName is the variable, The address pointer( pointerName) is going to store. This variable should be the type of datatype.
  • & (Address of operator) used to get the address of the variableName.

Let’s look at an example.

We have created an integer pointer variable named intPtr and initialized it with the address of the score integer variable.

Now, The intPtr is storing the address of score variable. ( Pointer storing the address of another variable).

Here is the graphical representation of how pointers work in the c programming language.

pointer-initialization-and-memory-allocation-in-programming

Let’s look at a program to demonstrate how pointers work in the C.

Program to demonstrate how pointers work in C programming:

Program Output:

Compile and Run the program.

program-to-understand-pointers-in-c-programming-language

Program Explanation – How pointers works in C:

  • We started the program by creating an integer variable called score and initialized with the value 300.
  • Then we created an integer pointer named intPtr and initialized it with the address of the score integer variable.
    • A couple of things to note here, We used the Asterisk ( *) to create the pointer variable. ( i.e int * intPtr)
    • We also used the address of operator (&) to get the address of the score integer variable.( i.e &score)
  • Print the Value of the score variable using the printf("Value of score: %d\n", score);. This prints the 300
  • Next, we displayed the address of the score variable using the address of & operator – This prints the address of the score variable which in this case is equal to 0x7ffc08f4a00c
    • printf(“Address of score: %p\n”, &score);
  • Then we printed the value of the pointer variable intPtr using another printf function. Here we used the %p format specifier as the intPtr is holding the address of the score. If you notice, The value of the intPtr and the address of the score variable are the same. i.e :0x7ffc08f4a00c
    • printf("Value of intPtr(pointer) :%p\n", intPtr);
  • Finally, The pointer variable intPtr will also have a memory address, To print the address of the intPtr pointer variable, We used the &intPtr . which is equal to 0x7ffc08f4a010
    • printf(“Address of intPtr(pointer): %p\n”, &intPtr);

📢Note that, If you run the above program, the memory address values might be different for you.

Indirection operator or Dereference Operator ( *) in C Language:

So far we looked at how to store the address of a variable using the pointer variable. We can access the value of the variable pointed by the pointer variable using the dereference operator ( *).

The dereference operator also called as the indirection operator in c programming language.

Program to understand the dereference operator in c:

Let’s look at an example to understand the dereference operator.

Program Output:

Compile the program.

$ gcc dereference.c

Run the program.

dereference-operator-in-c-language-program-output

Pointer dereferencing Program Explanation:

As we can see from the above output, We have defined an integer variable num with the value 100 and a pointer variable with ptr and initialized it with the address of the num

Now, The pointer variable is storing the address of the num variable and we can also access the value of the num variable by using the dereference operator( *).

So we used the first three printf functions to print the value of num and address of num and the value of ptr (i.e address of num)variable respectively.

Finally, We used the dereference operator( *) with the pointer( ptr) variable to get the value of the num variable.

printf("Value of variable pointed by ptr(*ptr): %d\n", *ptr);

which prints the value of the num variable i.e 100

Changing the variable value through the pointer variable in C:

Let’s change the above program to change the variable value through the pointer. We can update the variable value by using the dereference operator like below.

Program Output:

Compile and Run the C Program.

change-variable-value-using-pointer-variable

As you can see from the above output, The value of the variable num is changed by using the pointer variable ptr.

To change the variable, We used the *ptr(i.e *ptr = 50000) to update the integer variable num via the pointer variable ptr. We can see it from the above output and the value of the num variable before and after the change.

Things to remember while using the Pointers in C:

1) Make sure to assign the memory address of a variable to a pointer variable.

The above example is the correct way to assign the memory address of a variable to a pointer variable.

If you note, we missed the asterisk symbol(*) before the p2. So the p2 variable is not a pointer variable. It is just another integer variable, so the variable p2 can’t store the address of another variable.

2) If you declare and assign the pointer variables. make sure to use the pointer while assigning the address.

We declared the ptr3 pointer and then assigned the address of the variable n3 to ptr3. Now the ptr3 stores the memory address of n3.

We used the *ptr4 to update the address of &n4. This is not correct. We don’t use dereference operator( *) to update addresses.

We should use dereference operator( *) only to update the value of the variable pointed by the pointer.

In the above case, The variable n4 will now have a value that is equal to its own memory address. ( something like 130295644). This is not intended.

What are the uses of Pointers in C Programming Language:

There are many uses for pointers. few of them are,

  • Pointers are used for Dynamic memory allocation ( malloc, free)
  • We can pass the variables as references( addresses) to functions. – Learn more about the Call by value and call by reference
  • Accessing, Modifying, and Traversing the Array, String, and Structure elements.
  • Returning more than one value from the functions.
  • Pointers can also be used to implement the data structures like linked lists, trees, etc.

Conclusion:

We have learned about how variables are stored in the computer memory, What are pointers in C, and how to declare, Initialize and update pointers with example programs. We also looked at the address of operator(&) and the dereference operator(*).

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

16 Responses

  1. […] have looked at the introduction to pointers in an earlier post. In today’s article, We will look at the address of operator in c programming language with […]

  2. […] is recommended to go through the Introduction to Pointers tutorial to better understand this […]

  3. […] have discussed about the pointers in c, address of operator, and dereference operator in earlier posts, In today’s post we are going to […]

  4. […] is recommended to go through the introduction to the pointers in C and how to use the address of operator and dereference operator in c to better understand the […]

  5. […] Pointers in C Programming Language – Declare, Initialize, and use pointers […]

  6. […] Pointers in C Programming Language – Declare, Initialize, and use pointers […]

  7. […] have looked at the introduction to pointers, and pointer arithmetics in earlier articles. In today’s article, We will look at the pointer to […]

  8. […] have looked at the Introduction to pointers and pointer to pointer variable in earlier posts. In today’s article, We will look at the […]

  9. […] a function returning pointer in c programming language. As we have discussed earlier in the introduction to pointer and pointers and array articles, We can return a pointer variable from a function in C language. We […]

  10. […] previous articles, we looked at pointers in C and Double pointers in C with example programs. In this article, we will look at function pointers […]

  11. […] Pointers in C Language – How to Create and use pointers […]

  12. […] have covered the C Pointers, Pointer Arithmetic, and Pointer to pointers in earlier articles. In today’s article, We will […]

  13. […] Pointers in C Language – How to Create and use pointers […]

  14. […] our previous article, We have looked at the pointers and pointer programs. In today’s article, We will look at what is Static Memory allocation and […]

  15. […] Pointers in C Language – How to Create and use pointers […]

  16. […] call to the malloc() function is successful, Then the malloc() function will allocate 40 Bytes of memory in the HEAP section and The starting address of the allocated memory will be stored in the iPtr […]

Leave a Reply