Simple example to understand how pointer works
Syntax:
Datatype *pointer_name;
Example:
- #include <stdio.h>
- int main ()
- {
- int var = 20; // actual variable declaration
- int *ip;
// pointer variable declaration
- ip = &var; // store address of var in pointer variable
- printf("Address of var variable: %x\n", &var );
-
- /* address stored
in pointer variable */
- printf("Address stored in ip variable: %x\n", ip );
-
- /* access the value
using the pointer */
- printf("Value
of *ip variable: %d\n", *ip );
- return 0;
- }
Output:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
Description:
This topic seems little bit difficult but its very simple. Key point to remember in pointers
- Declaring a pointer: datatype *pointer_name;
- Initialize a pointer : pointer_name = &variable_name
- Get the address of pointer: use pointer_name(ex: line 10)
- Get value stored in address of pointer: *pointer_name(ex:line 13)
Line 4:Declares and initializes variable.
Line 5: This is how we declare a pointer.Now what pointer does is , it actually stores address of variable.
Line 6: This is how we store address of variable. '&' sign is used to get address of the variable.
Line 10: Shows how to print address of variable (or pointer value) on console.Here "%x" displays the hexadecimal value of the address.
Line 13:This line shows how to display the value stored in the address present in the pointer.
Suppose pointer carries address as 1001(for example). Then the value in 1001 will be displayed by line 13. NOTE: Not necessary address can be 1001 only.
рдорд░ाрдаीрдд рд╡рд░्рдгрди :
рдУрд│ рек: рд╡рд░िрдПрдм्рд▓рдЪी рдЬाрдЧा рдоेрдорд░ी рдордз्рдпे рдмрдирддे рд╡ рдд्рдпा рд╡рд░िрдПрдм्рд▓рдордз्рдпे рдХिंрдордд рдаेрд╡рд▓ी рдЬाрддे .
рдУрд│ рел:рдкोрдЗंрддрд░ рд╣ा рд╕ुрдж्рдзा рдПрдХ рд╡рд░िрдПрдм्рд▓ рдЖрд╣े рдЬो рдоेрдоोрд░ी рдЪा рдкрдд्рддा рдаेрд╡рдг्рдп рдХрд░िрддा рд╡ाрдкाрд░рд▓ा рдЬाрддो .
рдУрд│ рем:рд╣्рдпा рдУрд│ рдордз्рдпे рджिрд▓ेрд▓्рдпा рдкрдж्рдзрддीрдиे рд╡рд░िрдПрдм्рд▓рдЪा рдкрдд्рддा рдкोрдЗंрддрд░ рдордз्рдпे рдаेрд╡рд▓ा рдЬाрддो. рдд्рдпा рд╕ाрдаी рд╣ा '&' рдЪिрди्рд╣ рд╡ाрдкрд░рд▓ा рдЬाрддो,
рдУрд│ резреж: рд╣्рдпा рдУрд│ рдордз्рдпे рдЖрдкрдг рдмрдШू рд╢рдХрддो рдХि рдХрд╢्рдпा рдк्рд░рдХाрд░े рдЖрдкрдг рдкोрдЗंрддрд░ рдордзрд▓ा рдкрдд्рддा рдХрди्рд╕ोрд▓рд╡рд░ рд▓िрд╣ूрди рдмрдШू рд╢рдХ्рддो.
рдУрд│ резрей:рд╣्рдпा рдУрд│ рдордз्рдпे рдкोрдЗрди्рддेрд░ рдордз्рдпे рджेрд▓ेрд▓्рдпा рдкрдд्рддेрдд рдХाрдп рдХिंрдордд рдЖрд╣े рддे рдХाрдбू рд╢рдХ्рддो. рдУ рдХрди्рд╕ोрд▓ рд╡рд░ рд▓िрд╣ू рд╢рдХ्рддो.
Comments