11. 範例
int i,i,j;j;
int
int **p;
int p; /* pointer to `int' */
/* pointer to `int' */
i i= 6;
= 6;
pp= &i;
= &i; /* set `p' to address of `i' */
/* set `p' to address of `i' */
j j= *p;
= *p; /* set `j' to 66(value of `i') */
/* set `j' to (value of `i') */
*p = 5;
*p = 5; /* set `i' to 55*/
/* set `i' to */
i 0xba04
6 5
j 0xba08 6
*p
p 0xba18 0xba04
11
25. 指標的指標
int **p2; // a pointer to a pointer
int **p2; // a pointer to a pointer
int *p1;
int *p1;
int arr[3]={3,6,9};
int arr[3]={3,6,9};
p1=arr+2;
p1=arr+2;
p2=&p1;
p2=&p1;
**p2= 100;
**p2= 100;
25
27. 範例 -2D 陣列
#include <stdio.h>
#include <stdio.h>
#define ROW 33
#define ROW
#define COL 22
#define COL
int main()
int main()
{{
int i,j;
int i,j;
int m[ROW][COL]={{100,200},{300,400},{500,600}};
int m[ROW][COL]={{100,200},{300,400},{500,600}};
m[1][1]=900;
m[1][1]=900;
for (i=0;i<ROW;j=0,i++)
for (i=0;i<ROW;j=0,i++)
for (j=0;j<COL;j++)
for (j=0;j<COL;j++)
printf("m[%d][%d]=%-5d <--%pn",i,j,m[i][j],&m[i][j]);
printf("m[%d][%d]=%-5d <--%pn",i,j,m[i][j],&m[i][j]);
return 0;
return 0;
}}
27
28. 多維陣列 (cont.)
多維陣列即為多個”陣列的陣列” (of arrays of arrays...)
宣告一個二維陣列如 float m[3][2]; //array of 3 array of 2
floats
1. m 是一個 float 型態的陣列其可儲存 3 項資料而每一項
又具有 2 個元素 (2-int 資料型態單位的陣列 )
2. float m[3][2]; // m is an array of 3 somethings
3. float m[3][2]; // an array of 2 floats (*m)[2]
4. m[3][2] 是一個以 2-int 資料型態為單位的陣列
m[0] m[0][0] m[0][0]
m[1] m[0][0] m[0][0]
pointer array m[2] m[0][0] m[0][0]
28
29. An array of arrays
zippo the address of the first 2-int element
zippo+2 the address of the third 2-int element
*(zippo+2) the third element, a 2-int array, hence the address of its first
element, an int
*(zippo+2) + 1 the address of the second element of the 2-int array, also an int
*(*(zippo+2) + 1) the value of the second int in the third row (zippo[2][1])
zippo[m][n] == *(*(zippo + m) + n)
29
30. Pointers to Multi-Dimensional Arrays
int (* pz)[2]; // pz points to an array of 2 ints
pz=zippo;
pz[m][n] == *(*(pz + m) + n)
30
32. Pointer to const data
int * p1;
const int * p2;
const int ** pp2;
p1 = p2; // not valid -- assigning const to non-const
p2 = p1; // valid -- assigning non-const to const
pp2 = &p1; // not valid -- assigning non-const to const
const int **pp2;
int *p1;
const int n = 13;
pp2 = &p1; // not allowed, but suppose it were
*pp2 = &n; // valid, both const, but sets p1 to point at n *p1
*p1= 10; // valid, but changes const n
32
33. Constant pointer
ch6/const_ptr.c
#include <stdio.h>
int main()
{
const int a=10;
int b=20;
const int * const ptr=&a;
*ptr=100;
ptr=&b;
return 0;
}
33
34. Functions and Multidimensional Arrays
#define ROWS 3
#define ROWS 3
#define COLS 4
#define COLS 4
void sum_rows(int ar[][COLS], int rows);
void sum_rows(int ar[][COLS], int rows);
int main() {{
int main()
int junk[3][4] = {{
int junk[3][4] = {2,4,5,8} ,,
{2,4,5,8}
{3,5,6,9} ,,
{3,5,6,9}
{12,10,8,6}
{12,10,8,6}
};
};
sum_rows(junk, ROWS);
sum_rows(junk, ROWS);
return 0;
return 0;
}}
34
35. void sum_rows(int ar[][COLS], int rows)
void sum_rows(int ar[][COLS], int rows)
{{
int r;
int r;
int c;
int c;
int tot;
int tot;
for (r = 0; rr < rows; r++)
for (r = 0; < rows; r++)
{{
tot = 0;
tot = 0;
for (c = 0; c < COLS; c++)
for (c = 0; c < COLS; c++)
tot += ar[r][c];
tot += ar[r][c];
printf("row %d: sum = %dn", r, tot);
printf("row %d: sum = %dn", r, tot);
}}
}}
35
38. Bubble Sort
#include <stdio.h> /* Bubble Sort */
void main(void)
{
int data[50];
int i,j,n,temp;
printf("Please input integer number you want to sort:");
scanf("%d" ,&n);
printf("n");
if (n > 49)
{
printf("number should be less than 49n");
return;
}
for (i = 1;i <= n;i++)
{
printf("input data[%d]=", i);
scanf("%d", &data[i]);
}
38
38
41. 矩陣相乘 (1)
#include <stdio.h>
// Matrix A : m x n (row = m, column = n)
// Matrix B : n x p
// A x B = C (C should be m x p)
//
#define m 3
#define n 2
#define p 2
41
42. 矩陣相乘 (2)
void main()
{
int A[m+1][n+1], B[n+1][p+1], C[m+1][p+1];
int i, j, k;
printf("Please input Matrix A:");
for (i =1 ; i<= m; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);
}/*end for*/
printf("Please input Matrix B:");
for (i =1 ; i<= n; i++)
{
for(j=1;j<=p;j++)
scanf("%d",&B[i][j]);
}/*end for*/
42
50. volatile unsigned char data ;
data = *((volatile unsigned char *)0x300021) ;
data = data | 0x10 ; // set bit 4 as 1
*((volatile unsi gned char *)0x300021 = data ;
50
51. IF FPSHIFT-mask-enable
set to 16bpp and color mode
ELSE
set LCD-panel-data-width to 4-bit mode
51