Last active
June 2, 2016 09:56
-
-
Save hunandy14/078ba4de451e8cbfa3051ab0481549aa to your computer and use it in GitHub Desktop.
指標範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************** | |
Name : 指標範例 | |
Date : 2016/06/02 | |
By : CharlotteHonG | |
Final: 2016/06/02 | |
**********************************************************/ | |
/* 指標記住兩句話[*找數值][&找地址] */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
void modify(int* n1, int n2){ | |
*n1 = n2; // 直接修改[記憶體的位置]的[數值] | |
// n1=n2; // 這是錯誤的 n1 是用來存地址不是數值 | |
// 所以要加 * 找到他的數值,再修改 | |
} | |
int main(int argc, char const *argv[]){ | |
int a=10; | |
int* p; // 宣告一個可以儲存(int)地址的容器 | |
p = &a; // 讓這個容器存入 a 的記憶體位置 | |
printf("%d\n", a); | |
modify(p, 100); // 由於是直接修改記憶體位置的數值所以會跟著變 | |
printf("%d\n", a); | |
modify(&a, 1000); // 你也可以這樣做 | |
printf("%d\n", a); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment