---
# System prepended metadata

title: C語言的指標範例
tags: [' pointer', C]

---

---
tags: C, pointer
---


C語言的指標範例
===
+ [執行程式](https://repl.it/join/vqtdrrju-aidanlu)
___

### Case#1
```c=
#include <stdio.h>
#include <stlib.h>

int main()
{
    int* a = malloc(sizeof(int));

    *a = 10;
    printf("*a = %d\n", *a);
}
```
:::spoiler 結果
:::success
> *a = 10[color=green]
:::

### Case#2
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* a[3];

    *a[0] = 10;
    printf("*a[0] = %d\n*a[1] = %d\n*a[2] = %d\n", *a[0], *a[1], *a[2]);
}
```
:::spoiler 結果
:::danger
可通過編譯，但是<font color="#00f">**執行時會錯誤</font>**
:::


### Case#3
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* a[3];
    int a0 = 10;
    int a1 = 20;
    int a2 = 30;

    a[0] = &a0;
    a[1] = &a1;
    a[2] = &a2;
    printf("*a[0] = %d\n*a[1] = %d\n*a[2] = %d\n", *a[0], *a[1], *a[2]);
}
```
:::spoiler 結果
:::success
> *a[0] = 10[color=green]
*a[1] = 20
*a[2] = 30
:::


### Case#4
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* a[3];
    for (int i = 0; i < 3; i++)
    {
        a[i] = malloc(sizeof(int));
    }

    *a[0] = 10;
    *a[1] = 20;
    *a[2] = 30;
    printf("*a[0] = %d\n*a[1] = %d\n*a[2] = %d\n\n", *a[0], *a[1], *a[2]);

    a[0][0] = 100;
    a[1][0] = 200;
    a[2][0] = 300;
    printf("a[0][0] = %d\na[1][0] = %d\na[2][0] = %d\n\n", a[0][0], a[1][0], a[2][0]);

    a[0][0] = 1000;
    a[0][1] = 2000;
    a[1][2] = 3000;
    printf("a[0][0] = %d\na[0][1] = %d\na[0][1] = %d\n", a[0][1], a[0][1], a[1][2]);
}
```
:::spoiler 結果
:::success
> *a[0] = 10[color=green]
> *a[1] = 20
> *a[2] = 30
> 
> a[0][0] = 100
> a[1][0] = 200
> a[2][0] = 300
> 
> a[0][0] = 2000
> a[0][1] = 2000
> a[0][1] = 3000
:::


### Case#5
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 10;
    int* b = &a;
    int** c = &b;
    
    printf("Dump Value :\n");
    printf("a = %d, *b = %d, **c = %d\n", a, *b, **c);
    **c = 100;
    printf("a = %d, *b = %d, **c = %d\n", a, *b, **c);

    printf("Dump Address :\n");
    printf("&a = 0x%X\n", (int)&a);
    printf("&b = 0x%X, b = 0x%X\n", (int)&b, (int)b);
    printf("&c = 0x%X, c = 0x%X, *c = 0x%X\n", (int)&c, (int)c, (int)*c);
}
```
:::spoiler 結果
![](https://i.imgur.com/iCBkqo7.png)

:::success
> Dump Value :
a = 10, *b = 10, **c = 10
a = 100, *b = 100, **c = 100
Dump Address :
&a = 0xC2FDFC
&b = 0xC2FDF0, b = 0xC2FDFC
&c = 0xC2FDE4, c = 0xC2FDF0, *c = 0xC2FDFC[color=green]
:::


### Case#6
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* b = malloc(sizeof(int));
    int** c = &b;

    printf("Dump Value :\n");
    *b = 10;
    printf("*b  = %d, **c = %d\n", *b, **c);
    **c = 100;
    printf("*b  = %d, **c = %d\n", *b, **c);

    printf("Dump Address :\n");
    printf("&b = 0x%X, b = 0x%X\n", (int)&b, (int)b);
    printf("&c = 0x%X, c = 0x%X, *c = 0x%X\n", (int)&c, (int)c, (int)*c);
}
```
:::spoiler 結果
![](https://i.imgur.com/0OQP9FP.png)


:::success
> Dump Value :
*b  = 10, **c = 10
*b  = 100, **c = 100
Dump Address :
&b = 0x6FF80C, b = 0x8A4F68
&c = 0x6FF800, c = 0x6FF80C, *c = 0x8A4F68[color=green]
:::



### Case#7
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 0x12345678;
    unsigned char* b = (unsigned char*)&a;

    printf("Dump Value :\n");
    printf("a = 0x%X, b[0] = 0x%X, b[1] = 0x%X, b[2] = 0x%X, b[3] = 0x%X\n", a, b[0], b[1], b[2], b[3]);
    b[3] = 0x87; b[2] = 0x65; b[1] = 0x43; b[0] = 0x21;
    printf("a = 0x%X, b[0] = 0x%X, b[1] = 0x%X, b[2] = 0x%X, b[3] = 0x%X\n", a, b[0], b[1], b[2], b[3]);

    printf("Dump Address :\n");
    printf("&a = 0x%X, &b =  0x%X, b =  0x%X\n", (int)&a, (int)&b, (int)b);
    printf("&b[0] = 0x%X, &b[1] = 0x%X, &b[2] = 0x%X, &b[3] = 0x%X\n", (int)&b[0], (int)&b[1], (int)&b[2], (int)&b[3]);

}
```
:::spoiler 結果
![](https://i.imgur.com/OOQPLmo.png)
:::success
> Dump Value :
a = 0x12345678, b[0] = 0x78, b[1] = 0x56, b[2] = 0x34, b[3] = 0x12
a = 0x87654321, b[0] = 0x21, b[1] = 0x43, b[2] = 0x65, b[3] = 0x87
Dump Address :
&a = 0x19FA70, &b =  0x19FA64, b =  0x19FA70
&b[0] = 0x19FA70, &b[1] = 0x19FA71, &b[2] = 0x19FA72, &b[3] = 0x19FA73 [color=green]

:::


### Case#8
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* a;
    unsigned char b[] = {0x12, 0x34, 0x56, 0x78};
    a = (int*)b;

    printf("Dump Value :\n");
    printf("a = 0x%X, b[0] = 0x%X, b[1] = 0x%X, b[2] = 0x%X, b[3] = 0x%X\n", (int)a, b[0], b[1], b[2], b[3]);
    b[3] = 0x87; b[2] = 0x65; b[1] = 0x43; b[0] = 0x21;
    printf("a = 0x%X, b[0] = 0x%X, b[1] = 0x%X, b[2] = 0x%X, b[3] = 0x%X\n", (int)a, b[0], b[1], b[2], b[3]);

    printf("Dump Address :\n");
    printf("&a = 0x%X\n", (int)&a);
    printf("&b[0] = 0x%X, &b[1] = 0x%X, &b[2] = 0x%X, &b[3] = 0x%X\n", (int)&b[0], (int)&b[1], (int)&b[2], (int)&b[3]);
}
```


:::spoiler 結果
![](https://i.imgur.com/JpU6J50.png)
:::success
> Dump Value :[color=green]
a = 0x12345678, b[0] = 0x78, b[1] = 0x56, b[2] = 0x34, b[3] = 0x12
a = 0x87654321, b[0] = 0x21, b[1] = 0x43, b[2] = 0x65, b[3] = 0x87
Dump Address :
&a = 0xE2E0C84C
&b[0] = 0xE2E0C84C, &b[1] = 0xE2E0C84D, &b[2] = 0xE2E0C84E, &b[3] = 0xE2E0C84F
:::