C Pointers

In this tutorial, you will learn about the pointers in C. Pointers are the most distinct and exciting feature of C language and they are also the beauty of C Language. It is the make C language more powerful and adds flexibility on it. Pointers are the special type of variable that will hold the address of another variable. It won’t directly hold the values like other variables.  We can use the pointers of any datatypes. Whenever you are defining pointers, you must tell the compiler exactly what kind of object the pointer will be pointing to. You must assign a valid address of the proper type object. We can check the memory address of the variable by using & symbol.

For example, If x is the name of the variable then&x will give its address.

Pointers might appear to be albeit complicated and confusing at first but I assure you that you will be able to do so many things with the Language after you understand the basic concept. Before you will learn to declare the pointer variable.

How to declare the pointer variable?

For declaring the pointer variable we need to use *(asterisk) operator and (indirection or dereferencing operator or value at address) before the variable. Pointers can only point to the variable of the same datatype. Following is the syntax of the pointer variable.

Syntax:

datatype * pointer_variable_name;

This will tell the compiler three things about the pointer_variable_name. They are:

  • The asterisk tells the compiler that the variable pointer_variable_name is a pointer variable.
  • pointer_variable_name needs a memory location. i.e. the address of another variable.
  • pointer_variable_name points to a variable of type datatype.

How to initialize the pointer variable?

Once you declare the pointer variable you can use such variable to hold the address of other variables using the assignment operator. Therefore, Pointer variable initialization is the process of assigning the address of a variable to a pointer variable. Your pointer variable can only contain the address of a variable. For determining the address of the variable you can use & operator. The & will return the address of the variable associated with it. Now, look at the syntax below to understand more about the initialization of the pointer variable.

Syntax:

pointer_variable_name = &normal_variable;

Following is a simple example using the pointer variable. I assure you that you will understand more about the pointers in C in detail.

In the example below, we have two normal variables and one pointer variable. The following example illustrates the different naming conventions denoting the same value. Later, when we assign the value ’25’ to the pointer variable the value of the normal variable ‘x’ also changes.

Code:

Output:

Value of x is 10;
10 is stored at address 6487580
10 is stored at address 6487580
10 is stored at address 6487580
10 is stored at address 6487580
10 is stored at address 6487580
10 is stored at address 6487580
Now x= 25
SHARE C Pointers

You may also like...

Leave a Reply

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

Share