About Lesson
In C programming, a pointer is a variable that stores the memory address of another variable. Pointers are powerful features of C and C++ programming languages, providing the ability to directly interact with memory and manipulate data in a more flexible way.
Key Concepts of Pointers:
- Memory Address:
- Every variable in a program is stored in the computer’s memory, and each memory location has a unique address. A pointer holds the address of a variable.
- Declaration:
- A pointer is declared with an asterisk (
*
) before its name. The data type of the pointer must match the data type of the variable it points to. For example,int* ptr;
declares a pointer to an integer.
- A pointer is declared with an asterisk (
- Initialization:
- Pointers are usually initialized with the address of a variable, which is done using the address-of operator (
&
). For example,ptr = &var;
assigns the address ofvar
toptr
.
- Pointers are usually initialized with the address of a variable, which is done using the address-of operator (
- Dereferencing:
- Dereferencing a pointer means accessing the value at the address stored in the pointer. This is done using the dereference operator (
*
). For example,*ptr
gives the value of the variable whose address is stored inptr
.
- Dereferencing a pointer means accessing the value at the address stored in the pointer. This is done using the dereference operator (
C
#include<stdio.h>
#include<conio.h>
void main()
{
float x=10;
float *ptr;
clrscr();
printf("\nX=%f",x);
printf("\nAddress of X=%u",&x); //& address of operator
//if you know the address then how you can get value on that address
//Ans. We use Value At Address operator to get address (*)
printf("\nX=%f",*&x);
//pointer variable - it is a special variable that can hold address of another variable
ptr=&x; //address of x stored into ptr
printf("\nAddress of X=%u",ptr);
printf("\nValue of X=%f",*ptr);
*ptr=100;
printf("\nValue of X=%f",x);
getch();
}