We will discuss Pointers in C programming language
As we know, a memory unit is a collection of storage cells which can store any type of information.
Each memory cell has a unique value to identify each cell. It’s called address.
If a memory can store n- information, its address ranges from 0 to n-1. That is like an array index.
In the program we will demonsrate how to find address of memory unit in C language
Program
#include <stdio.h> #include <stdlib.h> int main() { int marks=67;//it is declare and initialized a variable as marks printf("Value of marks: %d\n",marks); printf("Address of marks: %u\n",&marks); getch(); return 0; }
Output
Value of marks: 67 Address of marks:2686748
In the above output, marks 67 is a value that is stored in memory location (address) 2686748.
All pointers variable must be declared before it is used in the program, like other variables in C.
The general form is,
var – it is a variable on your program in C language
&var – it is an address of your variable in the memory location
*var – this is a pointer of your memory location
We can declare a pointer in three methods
Syntax
data_type* var; // Declare a pointer variable called var as pointer of type
data_type *var;
data_type * var;
– data types are meaning, such as int, float, double and more
* (sign) – to identify the variable as a pointer
*ptr – this is a pointer variable
var– this is a normal variable
Here, ptr is a pointer which holds the address of variable var
Example :
int *a; // Declare a pointer variable called as pointing to an int
// it contains an address holds an int value
or
char* a; // Declare a pointer variable called a as pointing to a char
// it contains an address holds a char value
or
double * a; // Declare a pointer variable called a as pointer a double
// it contain an address holds a double value
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int a=24; int b=56; float c=34.65; printf("a value is: %d\n",a);//display value of a printf("b value is: %d\n",b);//display value of b printf("c value is: %.2f\n",c);//display value of c printf(".....................\n"); printf("address of value a is: %u\n",&a); //displays address in memory location of a value printf("address of value b is: %u\n",&b); //displays address in memory location of b value printf("address of value c is: %u\n",&c); //displays address in memory location of c value getch(); return 0; }
Output
a value is: 24 b value is: 56 c value is: 34.65 ...................... address of value a is: 2686748 address of value b is: 2686744 address of value c is: 2686740
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char a='A'; int b=34; float c=43.23; printf("a value is: %c\n",a);//display value of a printf("b value is: %d\n",b);//display value of b printf("c value is: %.2f\n",c);//display value of c printf(".....................\n"); printf("address of value a is: %u\n",&a); //displays address in memory location of a value printf("address of value b is: %u\n",&b); //displays address in memory location of b value printf("address of value c is: %u\n",&c); //displays address in memory location of c value printf(".....................\n"); int *p1;//pointer declaration p1=&a; int *p2=&b; int *p3=&c; printf("\n"); printf("address of a is: %u\n",p1); printf("address of b is: %u\n",p2); printf("address of c is: %u\n",p3); printf(".....................\n"); printf("a value is: %c\n",*p1); printf("b value is: %d\n",*p2); printf("c value is: %.2f\n",*p3); getch(); return 0; }
Output
a value is: A b value is: 34 c value is: 43.23 ............................ address of value a is : 2686739 address of value a is : 2686732 address of value a is : 2686728 ............................ address of a is : 2686739 address of a is : 2686732 address of a is : 2686728 ............................ a value is: A b value is: 34 c value is: 43.23
You may see also
pointer in C++
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
View Comments
Perfectly pent content material, Really enjoyed studying.