Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ゼロからわかるポインター

Aruneko
January 26, 2018

 ゼロからわかるポインター

SAMIT 18.01で発表したC言語のポインタに関する資料です

Aruneko

January 26, 2018
Tweet

More Decks by Aruneko

Other Decks in Programming

Transcript

  1. ม਺ͱܕ ܕͱ͸ ▸ σʔλͷ௕͞ͱղऍํ๏Λࣔͨ͠ϥϕϧ ▸ intͱ͔charͱ͔voidͱ͔ ▸ int͸࠷௿2bytesҎ্ͷූ߸෇͖੔਺Λද͢ܕ ▸ ᐆດͳఆٛͷ͍ͤͰॲཧܥʹΑͬͯintͷαΠζ͸·ͪ·ͪͳͷͰ஫ҙ

    ▸ char͸1byteͰจࣈ(ASCII)Λද͢ܕ ▸ void͸௕͞ͱղऍํ๏Λ࣋ͨͳ͍͜ͱΛࣔ͢ܕ ▸ σʔλͷऴ୺͕Θ͔Βͳ͘ͳΔͨΊvoidܕม਺͸એݴͰ͖ͳ͍
  2. ม਺ͱܕ ม਺ΛಡΉ ▸ ʮint a = 1ʯͷ৔߹ ▸ ϝϞϦ্ʹ͋Δʮ(0x31ݸ)1ʯͱ͍͏σʔλͷઌ಄ʹʮaʯͱ͍͏ϥϕϧΛషΔ ▸

    ͦͷσʔλ͸ɺintͷαΠζ෼͚ͩޙΖʹଓ͍͍ͯΔ ▸ ͦͷσʔλ͸ɺූ߸෇͖੔਺஋ͱͯ͠ղऍ͢Δ ▸ ʮchar b = '0'ʯͷ৔߹ ▸ ϝϞϦ্ͷʮ00110000ʯͱ͍͏σʔλͷઌ಄ʹϥϕϧʮbʯΛషΔ ▸ ͦͷσʔλ͸ɺcharͷαΠζ(1byte)෼͚ͩޙΖʹଓ͍͍ͯΔ ▸ ͦͷσʔλ͸ɺASCIIίʔυʹैͬͯղऍ͢Δ
  3. ม਺ͱܕ ม਺ͷαΠζΛௐ΂ͯΈΔ #include <stdio.h> int main(void) { printf("%lu\n", sizeof(short)); printf("%lu\n",

    sizeof(int)); printf("%lu\n", sizeof(long)); printf("%lu\n", sizeof(float)); printf("%lu\n", sizeof(double)); printf("%lu\n", sizeof(char)); return 0; }
  4. ม਺ͱܕ ม਺ͷઌ಄ΞυϨεΛ֬ೝͯ͠ΈΔ ▸ ࣍ͷϓϩάϥϜΛ࣮ߦͯ͠ΈΑ͏ ▸ ͍Ζ͍ΖͳαΠζͷม਺Ͱ࣮ݧ ▸ ม਺ͷઌ಄ΞυϨε͸&ͰऔಘͰ͖Δ #include <stdio.h>

    int main(void) { int a = 5; char b = '0'; double c = 5.3; printf("%p\n", &a); printf("%p\n", &b); printf("%p\n", &c); return 0; }
  5. ม਺ͱܕ ͜͜·Ͱͷ·ͱΊ ▸ ม਺͸ϝϞϦ্ͷྖҬͷઌ಄Λࣔ͢ϥϕϧ ▸ ܕ͸σʔλͷ௕͞ͱղऍํ๏Λද͢ϥϕϧ ▸ sizeofԋࢉࢠͰܕͷαΠζΛ֬ೝͰ͖Δ ▸ &Ͱม਺ͷઌ಄ΞυϨεΛऔಘͰ͖Δ

    ▸ ม਺ͷΞυϨε͸ຖճมΘΔ ▸ ม਺͸͖ͬͪΓ٧·ͬͯྖҬ͕֬อ͞ΕΔΘ͚Ͱ͸ͳ͍ ▸ ม਺ͷྖҬ͸એݴॱͱٯʹ֬อ͞Ε͍ͯΔ
  6. ഑ྻ ഑ྻܕͷαΠζͱղऍํ๏ ▸ ഑ྻܕͷαΠζ͸ʮܕͷαΠζ×ཁૉ਺ ʯ ▸ ഑ྻͷऴ୺Λ໌ࣔ͢ΔͨΊ ▸ ऴ୺͕Θ͔Βͳ͍ม਺͸એݴͰ͖ͳ͍ʂ ▸

    ഑ྻܕ͸ʮϝϞϦ্ͷΞυϨεʯͱͯ͠ղऍ͞ΕΔ ▸ ϝϞϦ্ͷΞυϨεΛૢ࡞͢Δํ๏͸ޙ΄Ͳ঺հ͠·͢
  7. ϙΠϯλܕม਺ ࣮ݧ ▸ ܕͷղऍΛ͋͑ͯؒҧ͑ͯΈΔ ▸ ͳͥ͜Μͳ݁Ռʹͳ͔ͬͨߟ͑ͯΈΑ͏ #include <stdio.h> int main(void)

    { int i = 25185; char* p = &i; printf("%d\n", *p); printf("%c\n", *p); return 0; } 25185 : int char
  8. ϙΠϯλܕม਺ ͜͜·Ͱͷ·ͱΊ ▸ ม਺ͷΞυϨεΛ֨ೲ͢ΔͨΊʹɺϙΠϯλܕม਺͕͋Δ ▸ ϙΠϯλܕม਺͸ʮܕ *ม਺໊ʯͰએݴ͢Δ ▸ ϙΠϯλܕม਺͸ҎԼͷ2ཁૉΛ࣋ͭ ▸

    ͋ΔϝϞϦ্ͷྖҬͷઌ಄ΞυϨε ▸ ͦͷྖҬΛԿܕͰղऍ͢Δ͔ ▸ ϙΠϯλܕม਺ʹ֨ೲ͞Ε͍ͯΔΞυϨεͷத਎ΛಡΉʹ͸*Λ࢖͏
  9. ػցޠ͔ΒϙΠϯλΛ೷͘ ػցޠͱ͸ ▸ CݴޠͷϓϩάϥϜ͸ίϯύΠϧ͞ΕΔͱػցޠʹ຋༁͞ΕΔ ▸ ػցޠ͸CPUͷछྨɺϏοτ਺ɺOSͳͲʹΑͬͯશ͘ҟͳΔ ▸ ࠓճ͸x86_64ͷELF(Executable and Linkable

    Format)Ͱ΍Γ·͢ ▸ LinuxͳͲͰར༻͞Ε͍ͯΔ࣮ߦϑΝΠϧϑΥʔϚοτ ▸ ϙΠϯλͱ͸ͳΜͧ΍ͱ͍͏ͷ͕͍͍ͩͨػցޠͰઆ໌Ͱ͖·͢
  10. ػցޠ͔ΒϙΠϯλΛ೷͘ αϯϓϧίʔυ ▸ ͜ͷίʔυͰ࣮ݧͯ͠ΈΑ͏ #include <stdio.h> int succ(int x) {

    int ans = x + 1; return ans; } int main(void) { int x = 5; int y = succ(x); printf("%d\n", y); return 0; }
  11. ػցޠ͔ΒϙΠϯλΛ೷͘ ػցޠͷݟͨ໨ ▸ ྫ͑͹succ͸͜Μͳײ͡ ▸ ࠨ͔ΒɺϓϩάϥϜதͷ૬ରΞυϨεɺੜͷػցޠɺΦϖίʔυɺΦϖϥϯυ 000000000000064a <succ>: 64a: 55

    push rbp 64b: 48 89 e5 mov rbp,rsp 64e: 89 7d ec mov DWORD PTR [rbp-0x14],edi 651: 8b 45 ec mov eax,DWORD PTR [rbp-0x14] 654: 83 c0 01 add eax,0x1 657: 89 45 fc mov DWORD PTR [rbp-0x4],eax 65a: 8b 45 fc mov eax,DWORD PTR [rbp-0x4] 65d: 5d pop rbp 65e: c3 ret
  12. ػցޠ͔ΒϙΠϯλΛ೷͘ MAINؔ਺ 0000000000000659 <main>: 659: 55 push rbp 65a: 48

    89 e5 mov rbp,rsp 65d: 48 83 ec 10 sub rsp,0x10 661: c7 45 f8 05 00 00 00 mov DWORD PTR [rbp-0x8],0x5 668: 8b 45 f8 mov eax,DWORD PTR [rbp-0x8] 66b: 89 c7 mov edi,eax 66d: e8 d8 ff ff ff call 64a <succ> 672: 89 45 fc mov DWORD PTR [rbp-0x4],eax 675: 8b 45 fc mov eax,DWORD PTR [rbp-0x4] 678: 89 c6 mov esi,eax 67a: 48 8d 3d a3 00 00 00 lea rdi,[rip+0xa3] 681: b8 00 00 00 00 mov eax,0x0 686: e8 95 fe ff ff call 520 <printf@plt> 68b: b8 00 00 00 00 mov eax,0x0 690: c9 leave 691: c3 ret ؔ਺ʹೖΔॲཧ int x = 5; int y = succ(x); printf(“%d\n”, y); return 0;
  13. ػցޠ͔ΒϙΠϯλΛ೷͘ ม਺΁ͷ୅ೖ ▸ 0x5ͱ͍͏஋ΛࢦఆՕॴʹίϐʔ ▸ ࢦఆՕॴ͸ʮrbp͔Β0x8ΛҾ͍ͨΞυϨεͷத਎ʯ ▸ ͔ͦ͜ΒDWORD(intܕ)෼ॻ͖ࠐΉ ▸ DWORD

    = WORD * 2 = BYTE * 2 * 2 = 4bytes ▸ ༨ͬͨྖҬ͸0ͰຒΊΔ 661: c7 45 f8 05 00 00 00 mov DWORD PTR [rbp-0x8],0x5 rbp → rsp → rbp - 0x8 → 0x5 DWORD
  14. ػցޠ͔ΒϙΠϯλΛ೷͘ ؔ਺ݺͼग़͠ ▸ rbp-0x8͔ΒDWORD෼த਎ΛಡΜͰeaxʹίϐʔ ▸ ୈ1Ҿ਺͸ediʹ٧ΊΔ ▸ call໋ྩͰݺͼग़͢ ▸ ฦΓ஋͸eaxϨδελʹೖ͍ͬͯΔ

    668: 8b 45 f8 mov eax,DWORD PTR [rbp-0x8] 66b: 89 c7 mov edi,eax 66d: e8 d8 ff ff ff call 64a <succ> 672: 89 45 fc mov DWORD PTR [rbp-0x4],eax rbp → rsp → rbp - 0x8 → rbp - 0x8 → rbp - 0x8 → rbp - 0x8 → 0x5 DWORD 0x6 rbp - 0x4 →
  15. ػցޠ͔ΒϙΠϯλΛ೷͘ ؔ਺࣮ߦ ▸ ΋͏ҰճrbpͷୀආͱrspͰͷ্ॻ͖Λ࣮ࢪ ▸ ͋ͱ͸ܭࢉͯ݁͠ՌΛeaxʹ٧ΊͯrbpΛ෮ݩ 000000000000064a <succ>: 64a: 55

    push rbp 64b: 48 89 e5 mov rbp,rsp 64e: 89 7d ec mov DWORD PTR [rbp-0x14],edi 651: 8b 45 ec mov eax,DWORD PTR [rbp-0x14] 654: 83 c0 01 add eax,0x1 657: 89 45 fc mov DWORD PTR [rbp-0x4],eax 65a: 8b 45 fc mov eax,DWORD PTR [rbp-0x4] 65d: 5d pop rbp 65e: c3 ret mainͷrbp → succͷrbp →
  16. ػցޠ͔ΒϙΠϯλΛ೷͘ දࣔॲཧ ▸ ม਺yΛeaxʹ٧Ίͯ ▸ ϑΥʔϚοτจࣈྻͷΞυϨεΛrdiʹ٧Ίͯ ▸ ΞυϨεͷઌʹ͸ʮ25 64 0a

    00ʯ͕ೖ͍ͬͯΔ ▸ eaxΛϦηοτͯ͠ ▸ printfΛ࣮ߦ 675: 8b 45 fc mov eax,DWORD PTR [rbp-0x4] 678: 89 c6 mov esi,eax 67a: 48 8d 3d a3 00 00 00 lea rdi,[rip+0xa3] 681: b8 00 00 00 00 mov eax,0x0 686: e8 95 fe ff ff call 520 <printf@plt>
  17. ഑ྻͱϙΠϯλ ϙΠϯλͷΠϯΫϦϝϯτͱσΫϦϝϯτ ▸ ίʔυͰ͔֬ΊͯΈΔ ▸ ͨͩ͠ɺ഑ྻΛ͸Έग़͢Α͏ͳΞΫηεΛͨ͠৔߹Ͳ͏ͳΔ͔͸ෆ໌ #include <stdio.h> int main(void)

    { int nums[5] = {0, 1, 2, 3, 4}; printf("%p\n", nums + 0); printf("%p\n", nums + 1); printf("%p\n", nums + 2); return 0; }
  18. ഑ྻͱϙΠϯλ ϙΠϯλͷΠϯΫϦϝϯτͱσΫϦϝϯτ ▸ ΠϯΫϦϝϯτͨ͠΋ͷ΋ΞυϨε ▸ ͱ͍͏͜ͱ͸ʮ*ʯͰத਎ʹΞΫηεͰ͖Δ ▸ nums[0]ͱ͍͏දه͸*(nums + 0)ͱಉٛ

    #include <stdio.h> int main(void) { int nums[5] = {0, 1, 2, 3, 4}; printf("%d\n", *(nums + 0)); printf("%d\n", *(nums + 1)); printf("%d\n", *(nums + 2)); return 0; }
  19. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ ී௨ͷม਺ͷ৔߹ ▸ ࢖͍͍ͨܕΛهड़ͯ͠౉͚ͩ͢ #include <stdio.h> void put_address(int num) {

    printf("%p\n", &num); } int main(void) { int x = 0; printf("%p\n", &x); put_address(x); return 0; }
  20. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ ϙΠϯλܕม਺ΛҾ਺ʹऔΔؔ਺ ▸ ΞυϨε͕ཉ͍͚ͩ͠ͳͷͰɺૉ௚ʹ౉ͯ͋͛͠Δ ▸ ؔ਺ͷதͰ͸ΞυϨεΛѻ͍ͬͯΔ͜ͱʹ஫ҙ ▸ ͋ͱ͸࢖ΘΕΔํʹͦΖ͑ΔݪଇͰOK! ▸ ίϐʔ͞ΕΔͷ͸ΞυϨε͚ͩ

    #include <stdio.h> void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } int main(void) { int a = 1; int b = 2; printf("%d, %d\n", a, b); swap(&a, &b); printf("%d, %d\n", a, b); return 0; }
  21. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ ؔ਺ͷҾ਺ʹ1࣍ݩ഑ྻΛऔΔʹ͸ ▸ ౉͢΋ͷ͸ΞυϨε ▸ ΞυϨεΛҾ਺ʹऔΕ͹OK ▸ αΠζΛҾ਺ʹऔΔཧ༝͸ʁ #include <stdio.h>

    void print_array(int *array, int size) { for(int i = 0; i < size; ++i) { printf("%d\n", array[i]); } } int main(void) { int nums[] = {0, 1, 2, 3, 4}; int size = sizeof(nums) / sizeof(int); print_array(nums, size); return 0; }
  22. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ ؔ਺ͷҾ਺ʹ2࣍ݩ഑ྻΛऔΔʹ͸ ▸ C99͔ΒҾ਺Λྲྀ༻Ͱ͖ΔΑ͏ʹ ▸ n࣍ݩ഑ྻʹ΋Ԡ༻Մೳ #include <stdio.h> void print_array(int

    s1, int s2, int array[][s2]) { for(int i = 0; i < s1; ++i) { for (int j = 0; j < s2; ++j) { printf("%d\n", array[i][j]); } } } int main(void) { int nums[][3] = {{0, 1, 2}, {3, 4, 5}}; int s1 = sizeof(nums) / (sizeof(int) * 3); print_array(s1, 3, nums); return 0; }
  23. ϙΠϯλܕ഑ྻ VS ഑ྻͷϙΠϯλ ϙΠϯλܕ഑ྻ ▸ ϙΠϯλܕΛฒ΂ͨ഑ྻΛ࡞Γ͍ͨͱ͖͸ҎԼͷΑ͏ʹએݴ͢Ε͹OK ▸ int *x[5] ▸

    ʮ࢖ΘΕํʯͰએݴ͢ΔݪଇΛࢥ͍ग़͢ ▸ *x[1]Ͱxͷ1൪໨ͷΞυϨεͷ
 த਎͕ಡΊΔ #include <stdio.h> int main(void) { int a = 0; int b = 1; int *x[2] = {&a, &b}; printf("%d, %d\n", *x[0], *x[1]); return 0; }
  24. ϙΠϯλܕ഑ྻ VS ഑ྻͷϙΠϯλ ഑ྻͷϙΠϯλ ▸ ഑ྻͷઌ಄ΞυϨεͱܕ৘ใΛ࣋ͬͨϙΠϯλܕม਺ͷ͜ͱ ▸ ϙΠϯλܕએݴ͸഑ྻએݴΑΓऑ͍ ▸ ׅހΛ͚ͭͯ༏ઌ౓Λ্͛Δ

    ▸ p͸ϙΠϯλͰɺܕ৘ใ͸ཁૉ਺3ͷchar഑ྻ ▸ ʮ࢖ΘΕํʯͷݪଇͰ͢Αʂ #include <stdio.h> int main(void) { char str[3] = "ab"; char (*p)[3] = &str; printf("%c\n", (*p)[1]); return 0; }
  25. ϝϞϦͷಈత֬อ ώʔϓͷ֬อํ๏ ▸ stdlib.hʹmallocؔ਺͕͋ΔͷͰ͜ΕΛར༻͢Δ ▸ Memory allocateͷུͰ͢ ▸ ֬อ͍ͨ͠όΠτ਺ΛҾ਺ʹ٧ΊΔ ▸

    ฦΓ஋͸voidϙΠϯλܕͳͷͰཁ஫ҙ ▸ ܕ৘ใΛ͍࣋ͬͯͳ͍ͨΊɺ͜ͷ··Ͱ͸ར༻Ͱ͖ͳ͍ ▸ ద੾ͳܕʹΩϟετ͢Δඞཁ͕͋Δ ▸ ࢖͍ऴΘͬͨྖҬ͸freeؔ਺Ͱղ์͠Α͏
  26. ϝϞϦͷಈత֬อ ώʔϓΛ֬อͯ͠ΈΔ ▸ scanfͰαΠζΛ΋Β͖ͬͯͯ ▸ mallocͯ͠ ▸ ΤϥʔͳΒҟৗऴྃͯ͠ ▸ ֬อͨ͠ΞυϨεΛදࣔͯ͠

    ▸ ๨Εͣʹfree͢Δ #include <stdio.h> #include <stdlib.h> int main(void) { int n; scanf("%d", &n); char *str = (char *)malloc(sizeof(char) * n); if (str == NULL) { puts("memory allocate error\n"); exit(1); } printf("%p\n", str); free(str); return 0; }
  27. ϝϞϦͷಈత֬อ Մม௕഑ྻ ▸ C99Ͱಋೖ͞ΕɺC11ͰΦϓγϣϯʹ
 ֨Լ͛͞Εͨػೳ ▸ malloc͠ͳͯ͘΋scanfͳͲͰ
 औ͖ͬͯͨαΠζͰ഑ྻΛ֬อͰ͖Δ ▸ ֬อͰ͖Δͷ͸ϩʔΧϧม਺ͷ


    ഑ྻ͚ͩͳͷͰ஫ҙ ▸ ͏·͘࢖ͬͯखൈ͖͠Α͏ʂ #include <stdio.h> #include <stdlib.h> int main(void) { int n; scanf("%d", &n); char str[n]; for (int i = 0; i < n - 1; ++i) { str[i] = 'a' + i; } str[n-1] = '\0'; printf("%s\n", str); return 0; }
  28. ؔ਺ͷฦΓ஋ͱͯ͠ͷϙΠϯλ ؔ਺ͷฦΓ஋ʹϙΠϯλΛ࢖͏ʹ͸ ▸ ී௨ʹฦΓ஋ͷܕΛϙΠϯλܕʹ͢Ε͹OK #include <stdlib.h> char *repeat(char c, int

    n) { char *str = (char *)malloc(sizeof(char) * (n + 1)); for (int i = 0; i < n; ++i) { str[i] = c; } str[n] = '\0'; return str; }
  29. ؔ਺ϙΠϯλ ؔ਺ϙΠϯλܕ ▸ એݴํ๏͸࣍ͷ௨Γ ▸ ฦΓ஋ͷܕ (*ม਺໊)(Ҿ਺ͷܕ); ▸ ΍͸Γʮ࢖ΘΕํʯͷ๏ଇ ▸

    ؔ਺એݴ΋ϙΠϯλܕએݴΑΓڧ͍ͷͰ
 Χοί͕ඞཁ ▸ ୅ೖ͍ͨ͠ͱ͖͸ؔ਺໊͚ͩΛ࢖༻ #include <stdio.h> int to_int(char c) { return (int)c; } int main(void) { int (*func)(char c) = to_int; printf("%p\n", func); printf("%d\n", (*func)('a')); return 0; }
  30. ؔ਺ϙΠϯλ ܕΛߟ͑ͯΈΔ ▸ ࣍ͷؔ਺Λ୅ೖͰ͖ΔܕΛߟ͑ͯΈΑ͏ʂ void putStr(char* str) { puts(str); }

    int fact(int n) { if (n == 2) { return 2; } else { return n * fact(n - 1); } } char first(char* str) { return str[0]; } double sum(double a, double b) { return a + b; }
  31. ؔ਺ϙΠϯλ ౴͑߹Θͤ ▸ ࠨ্ʣvoid (*hoge)(char *) ▸ ࠨԼʣint (*hoge)(int) ▸

    ӈ্ʣchar (*hoge)(char *) ▸ ӈԼʣdouble (*hoge)(double, double)
  32. ؔ਺ϙΠϯλ ͜͜·Ͱͷ·ͱΊ ▸ ؔ਺΋ϝϞϦ্ʹల։͞Ε͍ͯΔͨΊઌ಄ΞυϨε͕͋Δ ▸ ܕΛ޻෉͢Ε͹ͦΕΛ୅ೖ͢Δ͜ͱ͕Ͱ͖Δ ▸ ฦΓ஋ͷܕ (*ม਺໊)(Ҿ਺ͷܕ); ▸

    ಈ࡞͍ͤͨؔ͞਺ΛϓϩάϥϜதͰ੾Γସ͍͑ͨͱ͖ʹར༻͠Α͏ ▸ ͨͩ͠ฦΓ஋ͱҾ਺ͷܕ͕Ұக͠ͳ͚Ε͹ͳΒͳ੍͍໿͕...
  33. ઢܗϦετ ϊʔυΛ࡞Δ ▸ mallocͰ࡞੒ͯ͠ɺத਎ΛॳظԽ͓͖ͯ͠·͢ ▸ ҰԠNULLνΣοΫ΋͓͖ͯ͠·͢ Node *create_node(int x) {

    Node *new_node = (Node *)malloc(sizeof(Node)); if (new_node == NULL) { return NULL; } new_node->value = x; new_node->next = NULL; return new_node; }
  34. ઢܗϦετ N൪໨ͷϊʔυΛऔಘ͢Δ ▸ forจͰnextΛͨͲΔ͜ͱͰn൪໨ͷ஋Λऔಘ͠·͢ int take_n(Node *list, int n) {

    Node *tmp = list; for (int i = 0; i < n; ++i) { tmp = tmp->next; } return tmp->value; }
  35. ઢܗϦετ N൪໨ͷϊʔυΛ࡟আ͢Δ ▸ ࡟আର৅ͷ1ͭલ·ͰͨͲΔ ▸ ࡟আର৅ͱͦͷ࣍ͷϊʔυͷΞυϨεΛୀආͤ͞Δ ▸ ࡟আର৅ͷΞυϨεΛfree͢Δ void delete_n(Node

    *list, int n) { Node *tmp = list; for (int i = 0; i < n - 1; ++i) { tmp = tmp->next; } Node *target = tmp->next; tmp->next = tmp->next->next; free(target); }
  36. ઢܗϦετ શཁૉΛදࣔ͢Δ ▸ nextΛͨͲͬͯදࣔ͢Δ ▸ ͨͩ͠࠷ޙͷཁૉ͸ݸผʹදࣔ void print_list(Node *list) {

    Node *tmp = list; while (tmp->next != NULL) { printf("%d\n", tmp->value); tmp = tmp->next; } printf("%d\n", tmp->value); }
  37. ઢܗϦετ ςετͯ͠ΈΔ ▸ 1. ϊʔυΛ3ͭ࡞੒͠ɺ݁߹͢Δ ▸ 2. શཁૉΛදࣔ ▸ 3.

    1൪໨ͷཁૉ͚ͩΛදࣔ ▸ 4. 1൪໨ͷཁૉΛ࡟আ ▸ 5. શཁૉΛදࣔ int main(void) { Node *node0 = create_node(0); Node *node1 = create_node(1); concat_node(node1, node0); Node *node2 = create_node(2); concat_node(node2, node1); print_list(node2); puts(""); printf("%d\n", take_n(node2, 1)); puts(""); delete_n(node2, 1); print_list(node2); return 0; }