Created
July 18, 2016 09:33
-
-
Save KentaYamada/aa69f6675d21a194cd98a30289f1450a to your computer and use it in GitHub Desktop.
ポインタの使い方いろいろ
This file contains hidden or 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
| #include <stdio.h> | |
| static void func(const int *val); | |
| static void func2(const char *val); | |
| static void func3(const char val[]); | |
| static void func4(int *val); | |
| static void func5(const char *val); | |
| int main(void) | |
| { | |
| int a = 10; | |
| char b = 'a'; | |
| char *c = "Hello, world."; | |
| int d = 100; | |
| //変数のアドレスを渡す場合 | |
| func(&a); | |
| func2(&b); | |
| func3(c); | |
| printf("before val is %d\n", d); //100 | |
| func4(&d); | |
| printf("after val is %d\n", d); //expected 222 | |
| func5(c); | |
| return 0; | |
| } | |
| //変数のアドレスを受け取る場合は「*引数名」で定義する | |
| static void func(const int *val) | |
| { | |
| //ポインタが指す実データを参照する | |
| printf("val is %d\n", *val); | |
| } | |
| static void func2(const char *val) | |
| { | |
| printf("val is %c\n", *val); | |
| } | |
| static void func3(const char val[]) | |
| { | |
| printf("val is %s\n", val); | |
| } | |
| static void func4(int *val) | |
| { | |
| *val = 222; | |
| } | |
| static void func5(const char *val) | |
| { | |
| printf("val is %s\n", val); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment