Pointers in C – Basics
In this article, you will learn about the pointers in C programming and the usage of them in your programs.
Pointers – Introduction
The pointer in C language is a variable which stores the address of another variable. i.e., direct address of the memory location. This variable can be of type int, char, array, function, or even other pointers.
General syntax for a pointer
datatype *var_name;
Initializing of a pointer in C:
Every variable is a memory location and every memory location has its address defined (defaultly) which can be accessed using the ampersand (&) operator, which denotes the address in specified memory.
Sample program for Initializing of a pointer in C
[maxbutton id=”1″ url=”https://coderseditor.com/?id=228″ ]
#include<stdio.h> int main() { int num = 10; int *ptr; ptr = # }
Sample program using a pointer in C
[maxbutton id=”1″ url=”https://coderseditor.com/?id=229″ ]
#include <stdio.h> int main() { int x, *ptr; x = 10; ptr = &x; printf("Value of x is : %d\n", *ptr); printf("Value of x is : %d\n", *&x); printf("Address of x is: %u\n", &x); printf("Address of x is: %u\n", ptr); printf("Address of ptr is: %u\n", &ptr); return 0; }
Output
Value of x is : 10
Value of x is : 10
Address of x is: 2799768924
Address of x is: 2799768924
Address of ptr is: 2799768912