POINTERS EXAMPLE IN C PROGRAMMING
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