1. 程式人生 > 其它 >結構體和指標

結構體和指標

1. 結構體地址:

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

struct son
{
    int money;
};

struct dah
{
    int money;
};

struct father
{
    struct son s;
    struct dah d;
    int money;
};

int main(void)
{
    struct father *fa;
    printf("the address of father %p  the address of son %p \n
",&fa,&fa->s); printf("the address of father %p the address of dah %p \n",&fa,&fa->d); return 0; }
struct son s; 作為一個結構體,在father 存放一個son結構體的整體記憶體大小資料也就是int 大小,4位元組。
當struct son *s,則作為一個指標,在32位計算機上以4位元組存現記憶體。
當然
struct son s的地址也就是father的首地址。
2. 指標函式和指標函式
指標函式:int* func(int a,int b);
返回int *型別資料
函式指標:int (*func)(int x,int y);
func可以作為指標進行匹配的引數的指向

void
running(); func = &running; (*func)(1,2);
同時當指標函式重新宣告則
結構體:
struct test{
    void (*func)(void);
};
set(struct test *t, void (*running))
{
    t->func = running;
    t->func();
}
同時在實現
void *running();
    func = (void*)&running;
    func();

&取該函式的地址,前提需要強轉型別