In C, a pointer is a variable that stores the address of another variable. This variable can be of any type, including int, char, array, function, or pointer. The pointer’s size is determined by the architecture. However, in 32-bit architecture, a pointer is 2 bytes in size.
Consider the declaration,
int a=53
int *i=&a;
This declaration tells the c compiler to:
#include<stdio.h>
int main(){
int a=3;
int *i;
i=&a;//stores the address of number variable
printf("Address of i variable is %u \n",i); // i contains the address of the number therefore printing p gives the address of number.
printf("Value of i variable is %d \n",*i); // we will get the value stored at the address contained by i.
return 0;
}
Output:
Address of i variable is 571578428
Value of i variable is 3
The * is used to dereference a pointer.
We can create pointers for various data types. For instance,
int a[10];
int *p[10]=&a; // Variable p of type pointer is pointing to the address of an integer array a.
void show (int);
void(*p)(int) = &display; // Pointer p is pointing to the address of a function
struct t {
int i;
float f;
}ref;
struct t *p = &ref;
In the C programming language, pointers can be used in a variety of ways.
1) Allocation of dynamic memory
We can use the pointer to dynamically allocate memory in C using the malloc() and calloc() functions.
2) Structures, Functions, and Arrays
Pointers are commonly used in arrays, functions, and structures in the C programming language. It shortens the code and boosts performance.
Note: also read about the Unions in C
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…