A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.

Slides:



Advertisements
Similar presentations
Class operators. class polynomial class polynomialpolynomial class polynomial { protected: int n; double *a; public: polynomial(){}; …..; // constructor.
Advertisements

指標 Pointers.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
T-SQL 運算子介紹 11/29. 運算子的總類 指定運算子 算術運算子 比較運算子 邏輯運算子 位元運算子 字串連結運算子 單一運算元運算子.
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章 CS340100, NTHU Yoshi.
Main 的參數 無參數時 void main(void) { … } 有參數時 void main(int argc, char **argv) { … } 參數哪裡來? Console 下,例: dir /? 兩個參數 argc=2 參數字串 argv[0]= “ dir ” argv[1]= “
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
Multidimensional Array Outline Two-Dimensional Arrays Lengths of Two-Dimensional Arrays Multidimension Arrays [Sample code]
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
資料結構實習-四.
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
第五章 函數與儲存類別. 函數 函數為完成某一次特定任務或工作的小程式 函數的類型 庫存函數 (library functions) 如: scanf() 、 printf() 函數 … 等 此函數為系統所提供可以直接呼叫使用之 自定函數 (user-define functions) 此函數是使用者自行定義之函數.
第九章 結構. 結構的宣告 結構 它是由許多不同 ( 或相同 ) 資料型態的變數所組成的 集合,通常利用結構標籤稱呼此集合 struct student { char *name; int score; }; struct 為保留字,表示結構的宣告開始 結構項目需定義於大括號「 { } 」內 結尾需加上分號.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
Chapter 12 Pointers and Dynamic Arrays. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Pointers (12.1) Dynamic Arrays.
Introduction to C Language ─ C 語言資料型態. 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework.
資料結構實習-一 參數傳遞.
第六章 陣列.
參考書籍:古頤榛, Visual C++ 6教學範本 , 碁峰資訊股份有限公司。
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
: Efficient Solutions ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11020: Efficient Solutions 解題者:陳宜佐 解題日期: 2007 年 4 月 24 日 題意:給定 M 個 case.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
資料結構實習-二.
Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
資料型態名稱用途儲存空間儲存值範圍 short 短整數儲存較小的 整數 2 bytes-32,768~32,767 Int 整數儲存整數 16 位元 : 2 bytes 32 位元 : 4 bytes 16 位元 : 32,768~32, 位元 : -2,147,483,648 ~ 2,147,483,647.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
Chapter 11 Strings and Vectors. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview An Array Type for Strings (11.1) The Standard.
本章重點 2-1 有序串列(Ordered List) 2-2 介紹陣列(array) 2-3 矩陣(matrix)的應用
時間序列 Chap7 1 Chap 7 Decomposition method 4 components : Trend (TR t ) :長期向上或向下的移動趨勢 Seasonal variation (SN t ) :以年為基礎的變動原型 Cycle (CL t ) :在 2 到 10 年中向上或向下的改變.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
Chapter 6 Arrays, Pointers, and Strings
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3
VHDL 的物件 (Objects) 宣告 物件種類 (1) 訊號 (2) 變數 (3) 常數 VHDL 的物件 (Objects) 宣告語法 : [ := ] ;
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
Miscellanea Switch Bitwise operations ?,. break; continue; (example) (example) do { a = getc(stdin); if (a=='q') break; else if (a=='c') continue; n++;
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
Visual C++重點複習.
資料結構實習-六.
I/O of C and C++. printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
C 語言練習題 2010/12/2. C 程式的格式 一、 C 程式的格式 (1). /* …. */ 是 C 程式的 ______ main() 的內容是由敘述構成的 (2). 敘述要以 __ 為結束符號 (3). 變數、函數都要做 ____ 的宣告 (4). ‘=’ 是 ____ 用的算符 (5).
第8章 字串與陣列 8-1一維陣列的處理 8-2 字串處理 8-3 多維陣列的處理 8-4 動態陣列與參數傳遞 8-5 陣列排序與搜尋.
牽涉兩個變數的 Data Table 汪群超 11/1/98. Z=-X 2 +4X-Y 2 +6Y-7 觀察 Z 值變化的 X 範圍 觀察 Z 值變化的 Y 範圍.
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
6 使用者函數 6.1 函數定義 宣告函數 呼叫函數 呼叫多個函數 6-6
第 8 章 記憶體指標. 8.0 變數、數值、位址 8.1 指標與變數 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。
Java Tutorial /10/21. Java Resource Java SDK –Download from –Install Jdk windows-i586.exe –
Data Management 連賢明 政大財政. 2 統計軟體  一般通用 STATA SAS  個體計量 LIMDEP  高階軟體 MATLAB GAUSS.
Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory.
Chapter 3 Visual Basic.Net Visual Basic, like most programming languages, uses variables for storing values. Variables have a name (the word you use to.
Data Structures (1st Exam). 1.[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for.
> Database 實驗室專用程式語言 Java. Language - 2 Outline Programming language chaos 一小時精通 Java 不是夢 Java 環境設定.
STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel namecard : char name[10]; int age; char tel[10] char [60]
C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)
Other Thread Synchronization Functions 井民全製作. Introduction.
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Speaker: Liu Yu-Jiun Date: 2009/4/15
Presentation transcript:

a[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte

動態配置記憶體 pointer 與陣列的比較 int a[5]; // 宣告五個整數的陣列 int *p; //p 為指向整數的指標,並無宣告其他空間 動態配置記憶體 #include p=malloc(5*sizeof(int)); // memory allocation free(p); // free memory p *p *(p+1) … *(p+4)

Example #include main() { unsigned char *get_block, *pointer; pointer="TEST!"; // allocate space to pointer? no get_block = (unsigned char *)malloc(sizeof(char)*6); strcpy(get_block, pointer); *get_block = 't'; get_block[2] = 's'; printf("%s\n", pointer); printf("%s\n", get_block); free(get_block); } cast (unsigned char*)

Quiz about your homework Modify your homework such that it dynamically allocate a 2-d array

pointer to pointer array 用來儲存不定長度的二維陣列資料 Ex. 文字資料 a b c \ \0 X \0 … 預先宣告一固定大小陣列太浪費記憶體空間

Example #include main() { char **pa; pa = (char **)malloc(3*sizeof(char *)); *(pa) = "abc"; *(pa+1) = (char *)malloc(6*sizeof(char)); *(*(pa+1)+0)='0'; *(*(pa+1)+1)='1'; *(*(pa+1)+2)='2'; *(*(pa+1)+3)='\0'; } pa *pa *(pa+1) *(pa+2) “ abc ” “ 123 ”

Two ways to dynamically allocate multi-dim arrays (1) 1. char *s; s = (char *)malloc(sizeof(char)*nrow*ncol); *(s+i*ncol+j) = ‘ a ’ ; /* s[i][j] = ‘ a ’ ; */ *(s+i+j*nrow) = ‘ a ’ ; nrow ncol s s[i][j] s[0][0] s[0][1]

Two ways to dynamically allocate multi-dim arrays (2) 1. char **s; s = (char **)malloc(sizeof(char *)*nrow); for (i=0; i<nrow; i++) s[i] = (char *)malloc(sizeof(char)*ncol); s[i][j]= ‘ a ’ ; /* access data */ nrow ncol nrow ncol … s s[i][j]

Why Functions? 副程式、函數 Ex. printf(), scanf(), getchar(), … Break large program into smaller ones Call functions that others have done Hide details of operations

Format of functions Function, subroutine, procedure printf, getchar, putchar, … input output … body … (hidden from user)

Example: Power Function: Power(2,3) -> 2 3 #include int power(int m, int n); main() { int i; for(i=0; i<10; ++i) printf("%d %d\n", i, power(2,i)); } function prototype c.f. function definition int power(int, int); 函數原型宣告

int power(int base, int n) { int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p; } Return-type function-name(parameter declarations … ) { Declarations statements } These variable names are local to this function

Functions (cont.) power(int base, int n) return p; … body … (hidden from user) power(2,i) arguments(formal arguments) parameters(actual arguments)

Function definitions return-type function-name (argument declarations) { declarations and statements … return expression } * If return-type is omitted, int is assumed * Functions can occur in any order in the source file

Example: prog7-1.c #include double half(double); void main(void) { double r; int i; for(i=0; i<5; i++){ r = half( (double) i); printf("full=%d half=%f\n", i, r); } double half(double s) { return s/2; }

Call by value Store-program concept Program is data Data segment Program segment … … memory Data segment Program segment main power … address

2i2i Program segment … … memory Program segment main power … address power(2,i) base n Call by value 傳值過去 base=2; n=i;

Call by value - example int power(int base, int n) { int p; for(p=1; n>0; --n) p = p * base; return p; } int power(int base, int n) { int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p; }

swap 寫一函數,會交換兩個輸入變數的值 #include void swap(int, int); main() { int a=5, b=6; pirntf( “ a=%d, b=%d\n ”, a, b); swap(a, b); printf( “ a=%d, b=%d\n ”, a, b); } void swap(int c, int d) { int i; i=c; c=d; d=i; }

swap #include void swap(int *, int *); main() { int a=5, b=6; pirntf( “ a=%d, b=%d\n ”, a, b); swap(&a, &b); printf( “ a=%d, b=%d\n ”, a, b); } void swap(int *c, int *d) { int i; i = *c; *c = *d; *d = i; }