Skip to main content

POINTERS EXAMPLE IN C PROGRAMMING

Simple example to understand how pointer works

Syntax:

Datatype *pointer_name;

Example:

  1. #include <stdio.h> 
  2. int main ()
  3. {
  4.    int  var = 20;   // actual variable declaration 
  5.    int  *ip;        // pointer variable declaration 
  6.    ip = &var;  // store address of var in pointer variable
  7.    printf("Address of var variable: %x\n", &var  );
  8.  
  9.    /* address stored in pointer variable */
  10.    printf("Address stored in ip variable: %x\n", ip );
  11.  
  12.    /* access the value using the pointer */
  13.    printf("Value of *ip variable: %d\n", *ip );
  14.    return 0;
  15. }

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

Popular posts from this blog

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ЁЯзк ЁЯФз Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ЁЯФМ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ЁЯТ╗ Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Water Flow Sensor with Raspberry Pi 3 ЁЯЪ┐

Interfacing Water Flow Sensor with Raspberry Pi 3 ЁЯЪ┐ ЁЯОп Objective To measure the flow rate of water using a Water Flow Sensor (YF-S201) and Raspberry Pi 3. Useful in smart irrigation and water management systems. ЁЯз░ Components Required Component Quantity Raspberry Pi 3 1 YF-S201 Water Flow Sensor 1 10K Pull-down Resistor 1 Jumper Wires As required Breadboard 1 ⚡ Circuit Connections Sensor Pin Connect To Red (VCC) 5V (Raspberry Pi) Black (GND) GND (Raspberry Pi) Yellow (Pulse Out) GPIO18 (Pin 12) with pull-down resistor ЁЯза Python Code import RPi.GPIO as GPIO import time FLOW_SENSOR = 18 pulse_count = 0 def countPulse(channel): global pulse_count pulse_count += 1 GPIO.setmode(GPIO.BCM) GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse) try: while True: pulse_count = 0 time.sleep(1) flow_rate = (pulse_count / 7.5) ...

Interfacing Sound Sensor with Raspberry Pi 3

ЁЯФ╣ Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.