C 指標與結構

  • 17843
  • 0
  • C

摘要:C 指標與結構

指標整理
錯誤: int *ptr; *ptr = 35;
正確: int *ptr, number = 10; ptr = &number;
 
型式一:
int *ptr;
int num = 20;
ptr = &20;
 
型式二:
int num =20;
int *ptr = #
 
 
 
ptri 位址為 0012ff84,ptri +1 位址改為  0012ff88
取得鄰近位址內的值 *(ptr+1)
 
++*ip,(*ip)++,需括號,因為單元運算子是由右向左做的
 
int* const p: 指向const的指標 可以改值, 不可以改位址
int const *p (相等於 const int* p); 可以改變位址,但不可以改值,即不能當左值(lvalue)使用
const int* const:指向const的const指標
 
EX1:
    int num1 = 10;
    int* const ptr = &num1;
    printf("%d\n", *ptr);
   
    int num2 = 20;
    ptr = &num2;  錯誤 不可以改位址
    *ptr += 20;  可以改值
Ex2:
    int num1 = 10;
    int const *ptr = &num1;
    printf("%d\n", *ptr);
   
    *ptr += 20;  不可以改值
    int num2 = 20;
    ptr = &num2; 可以改位址
 
 
陣列指定給指標:
指標 int *ptr;   陣列 int array[10];
ptr = array;  或者是  ptr = &array[0]; (寫array跟&array[0]是相同的)
===> a[i]就可寫為 *(a + i)
 
若指標ptr為指向陣列的第一個元素array[0]
則 *(ptr + 1)表示指向array[1],所以第i個元素 *(ptr+i) 指向 array[i]
 
char *array[] 指標陣列 ===> *array[i]指向文字列的第一個字元
char (*array)[] 指標指向陣列
 
函式返回指標 int *f(); f為函數,傳回值為指向整數的指標
函式指標     int (*f)(); f是個指標,指向一個會傳回整數的函數,因為*的優先權比括號低
 
char (*(*x( )) [] )( ):函數, 傳回一指標,該指標指向指標陣列, 陣列中各元素指向一個會傳回char值的函數
 
char (*(*x[3]) ( )) [5]: 指標陣列, 有3個元素, 各指向一個會傳回另一指標的函數, 該指標是指向含有5個元素的字元陣列
 
指標傳遞給函數
void address(int*); //函式原型
void address(int *ptr){ //函式定義
}
 
 
Callback function:
typedef int (*fp)(char a);
fp fp = fun;   //fun為一個指向函數的函數指標(參數為char值, 返回值為int)
int x = fun('x');
 
#include <stdio.h>
#include <stdlib.h>
typedef int (*Add)(int, int);
typedef int (*Sub)(int, int);
 
typedef struct cal{
    Add add;
    Sub sub;
}cal;
 
int fun_add(int, int);
int fun_sub(int, int);
 
int main(int argc, char *argv[])
{
    cal a =
    {
        .add = fun_add,
        .sub = fun_sub
    };
   
    printf("%d, %d", a.add(1, 1), a.sub(1, 1));
    system("PAUSE"); 
    return 0;
}
 
int fun_add(int a, int b){
    return a+b;
}
int fun_sub(int a, int b){
    return a-b;
}
 
 
 
記憶體配置:
配置一個浮點數指標
float *fp;
fp = (float *)malloc(sizeof(float));
free(fp);
 
 
int *num;
num = (int *)malloc(sizeof(int) * 10)
free(num)
 
int array[10][20]
 
int **array= (int **)malloc(sizeof(int*)*10);
for(i=0;i<10;i++)
   *(array+i) = (int *)malloc(sizeof(int) * 20);
 
 
Example:
struct grade
{
    int math;
    int english;
    int computer;
};
struct grade *student;
student = (struct grade *) malloc(num * sizeof(struct grade));
 
 
鏈結串列:
struct list
{
   int num; 
   char name[10];
   char address[50];
   struct list *next;
};
typedef struct list node;
typedef node *link;
 
ptr = ( link ) malloc(sizeof(node));
ptr->next = NULL;
 
 
#include <stdio.h>
#include <stdlib.h>
 
struct point{
    int x;
    int y;          
};
 
struct rect{
    struct point p1;
    struct point p2; 
};
 
int main(int argc, char *argv[]){
    struct rect screen;
    screen.p1.x = 10;
    screen.p1.y = 15;
    screen.p2.x = 20;
    screen.p2.y = 25;
   
    printf("%d, %d\n", screen.p1.x, screen.p1.y); //10, 15
    printf("%d, %d\n", screen.p2.x, screen.p2.y); //20, 25
    system("PAUSE"); 
    return 0;
}
 
struct point *pp
宣告pp為一個指標,指向一個point結構
(*pp).x, (*pp).y 取值 或寫為 pp->x, pp->y
 
typedef int Length 將Length定義成與int同義的字
typedef char *String 將String定義成指向文字的指標
typedef char Bullow[4]; Bullow x; 定義x為char x[4]
 
Struct status{
    unsigned sex:1;
    unsigned marriage:1;
    unsigned age:7;
}
:1表示佔1位元
:7表示佔7位元
 
 
#define B "b"
char a[] = "a"B"c";
printf("%s", a);  //abc
 
換行使用\
 
 
#include <stdlib.h>
 
struct point{
    int x;
    int y;          
};
 
struct rect{
    struct point p1;
    struct point p2; 
};
 
int main(int argc, char *argv[]){
    struct rect screen;
    screen.p1.x = 10;
    screen.p1.y = 15;
    screen.p2.x = 20;
    screen.p2.y = 25;
   
    printf("%d, %d\n", screen.p1.x, screen.p1.y); //10, 15
    printf("%d, %d\n", screen.p2.x, screen.p2.y); //20, 25
    system("PAUSE"); 
     return 0;
}
 
struct point *pp
宣告pp為一個指標,指向一個point結構
(*pp).x, (*pp).y 取值 或寫為 pp->x, pp->y
 
typedef int Length 將Length定義成與int同義的字
typedef char *String 將String定義成指向文字的指標