Pointers
In C programming language, pointers are variables that store or point to the address of another variable. It is used to allocate memory at run time dynamically. A pointer variable can be of any data type like int, char, float, short, double, etc.
What you should always remember about pointers in C
- Pointers store the address of a variable
- A C pointer is always a whole number i.e., the address
- The value of a pointer in C is always initialized to NULL: Int *p = NULL. A null pointer has a value of 0.
- To get the address of the variable, we use the symbol &.
- The asterisk symbol is used to get the value of the variable that the pointer is pointing to.
- In C language, a pointer assigned to NULL is pointing to nothing.
- We can subtract two pointers to know the elements available between them
- You cannot perform pointer addition, multiplication or division
- For a 16 bit compiler, the size of the pointer is 2 bytes.
The general syntax of a pointer is:
Datatype *var_name;
For example, int *ptr;
This is an example of a pointer “ptr” that holds the address of an integer variable. Also, the syntax can refer to the address of a memory whose value(s) can be accessed as integer values using “ptr.”
Pointer operators
Before you use pointers, you must understand the two operators mentioned below:
A unary operator &, also known as the ampersand, is used to access the address of a variable to a pointer. It returns the address of the variable. For example, &w gives us the address of the variable w.
Example 1
//A program that prints the address of a variable
#include
Int main ( )
{
Int w;
// use the printf function to print the address of our variable w
Printf (“%p”, &w);
Return 0;
}
Please note that a variable can be assigned different addresses in different runs.
The unary * symbol or the asterisk can be used for two things:
A pointer variable declaration. A pointer variable must have an asterisk before its name when it is declared on C and C++
Example 2
// A C program that shows how pointer variables are declared
#include
Int main ( )
{
Int d = 15;
//ptr is a pointer variable since there is an asterisk in the declaration. In simple terms, it is a variable //that stores the value of another variable.
//The int before the * denotes that ptr is a pointer to ab integer types of variable
Int *ptr;
//The ampersand operator before d gets the address of d
//The address of d is assigned to ptr
Ptr = &d;
Return 0;
}
The next use of the asterisk is to access the value stored in the address. We can use the unary operator to return the value of the variable located at the address specified by its operand
Example 3
// A program in C that shows the use of * in pointers
#include
Int main ( )
{
// our variable is a normal integer
IntVar = 20;
//Declare a pointer variable that will hold the address of Var.
Int *ptr = &Var;
//Print the value at the address stored in ptr.
//The stored value is the value of Var.
Printf (“Value of Var = %d\n”, *ptr);
//The value of this next line may vary from machine to machine
Printf (“Address of Var = %p\n”, ptr);
Return 0;
}
The output of this program will be:
Value of Var = 20
Address of Var = 0x7ffb057d4
Expressions and arithmetic of pointers
Pointers have a limited set of arithmetic operations. Our C programming assignment help experts have listed them below:
++ : incremented
– : decremented
+ or += : adding an integer to a pointer
– or – = : subtracting an integer from a pointer
It is recommended that you perform pointer arithmetic in an array.
Array Name as pointers
We can use an array name as a pointer constant. The address of the first element in the array is the value of this pointer constant. This means that if we have an array named mks, then mks and &mks[0] can be used interchangeably
Avail our help with C programming assignment if you need assistance with your pointers assignment. Programmingassignmenthelper.com is here to cater to all your programming needs.