Created
October 13, 2019 03:19
-
-
Save BMuscle/defa7be16058dba17e144a22e321d2f9 to your computer and use it in GitHub Desktop.
ポインタ説明6
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> | |
| #include <stdlib.h> | |
| int *func1() {//このような書き方はしてはならない。 aが保証されないため | |
| int a = 10; | |
| return &a; | |
| } | |
| int *func2() {//これはmallocで確保しているので自動変数の領域には入らない | |
| int *a = (int*)malloc(sizeof(int) * 10); | |
| a[0] = 100; | |
| a[1] = 200; | |
| a[2] = 300; | |
| return a; | |
| } | |
| void func3() {//自動変数を上書きする | |
| int a = 10000; | |
| int b = 10000; | |
| int c = 10000; | |
| } | |
| int main() | |
| { | |
| int* a; | |
| a = func2(); | |
| func3(); | |
| printf("a[0]=%d, a[1]=%d, a[2]=%d\n", a[0], a[1], a[2]); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment