💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Lecture PPT
  • For Numerical Programming
  • For Embedded Controller
  • Summary
  • Example Code
  • Exercise
  • Exercise 1
  • Exercise 2
  • Exercise 3 - for EC only

Was this helpful?

  1. C Programming
  2. C-Programming Lessons

Pointer

PreviousCreating Header LibNextArray

Last updated 8 months ago

Was this helpful?

Lecture PPT

For Numerical Programming

For Embedded Controller

Summary

코딩도장 요약 :

What are Pointers?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

(a) Define a pointer variable: int *ptr;

(b) Assign the address of a variable to a pointer: ptr = &var;

(c) Access the value at the address available in the pointer variable: int value = *ptr

For 32-Bit Word system (e.g. MCU)

32-CPU: 1 word = 4 bytes (32bit)

  • Memory: byte units

  • Int : 4 byte

  • Pointer variable: 4 byte (default)

Example Code

#include <stdio.h>
#include <stdlib.h>

int main() {
int  var = 20;   
double a[4] = { 2, 2, 3, 4 };

// Pointer Declaration
int  *ptr;
double *ptr2 = NULL;// good practice for not Addr. assgined pointer
double *ptr3 = NULL;

// Pointer Assignment
ptr = &var;// store address of var in pointer variable
ptr2 = a;
ptr3 = &a[0];

printf("Address of var: %x\n", &var);
printf("Address of a  : %x\n", &a);
printf("Address of a[0]: %x\n", &a[0]);

// Using Pointer - Access the values pointed by Ptr
printf("\nAddress stored in \n  ptr: %x \n ptr2: %x  \n ptr3: %x\n", ptr, ptr2, ptr3);
printf("Value of \n  *ptr: %d \n *ptr2: %.1f  \n *ptr3: %.1f\n", *ptr, *ptr2, *ptr3);

system("pause");
return 0;
}

Exercise

Exercise 1

다음 소스 코드를 완성하여 10과 20이 각 줄에 출력되게 만드세요.

#include <stdio.h>

int main()
{
    int *numPtr;
    int num1 = 10;
    int num2 = 20;

    ① ________________
    printf("%d\n", *numPtr);

    ②_________________
    printf("%d\n", *numPtr);

    return 0;
}

실행 결과

10
20
Solution
① numPtr = &num1;
② numPtr = &num2;

Exercise 2

int x =10;            
double y=2.5;
int *ptrX = &x;      
int *ptrY = &y;

/*
-Print the address of variable ‘x’
-Print the address of variable ‘y’
-Print the value of pointer ‘ptrX ‘
-Print the address of pointer ‘ptrX ‘
-Print the size of pointer ‘ptrX ‘
*/

/*
-Print the value of pointer ‘ptrY ‘
-Print the address of pointer ‘ptrY ‘
-Print the size of pointer ‘ptrY 
*/

Exercise 3 - for EC only

int x =10;            
double y=2.5;
int *ptrX = &x;      
int *ptrY = &y;


// Typecast pointer 'ptrY' to as (double *)

Online C Compiler
Exercise Code
포인터 사용하기 핵심요약
415KB
(C-program) Pointer_Array_2022.pdf
pdf
334KB
(C-program) Embedded_Pointer_Array_2023.pdf
pdf
TCP school.com