{"id":482,"date":"2025-04-03T08:14:22","date_gmt":"2025-04-03T08:14:22","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=482"},"modified":"2025-04-03T08:14:22","modified_gmt":"2025-04-03T08:14:22","slug":"%f0%9f%94%a5-con-tro-trong-c-tu-co-ban-den-nang-cao%f0%9f%94%a5","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=482","title":{"rendered":"\ud83d\udd25 Con tr\u1ecf trong C \u2013 T\u1eeb C\u01a1 B\u1ea3n \u0110\u1ebfn N\u00e2ng Cao\ud83d\udd25"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/04\/image.png\" alt=\"\" class=\"wp-image-483\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/04\/image.png 640w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/04\/image-300x169.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>N\u1ebfu b\u1ea1n \u0111ang h\u1ecdc C m\u00e0 ch\u01b0a hi\u1ec3u v\u1ec1 <strong>con tr\u1ecf (pointers)<\/strong>, th\u00ec b\u1ea1n ch\u01b0a th\u1ef1c s\u1ef1 l\u00e0m ch\u1ee7 ng\u00f4n ng\u1eef n\u00e0y! Con tr\u1ecf l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean <strong>qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb<\/strong>, <strong>t\u0103ng hi\u1ec7u su\u1ea5t ch\u01b0\u01a1ng tr\u00ecnh<\/strong> v\u00e0 <strong>x\u00e2y d\u1ef1ng c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p<\/strong> nh\u01b0 danh s\u00e1ch li\u00ean k\u1ebft, c\u00e2y nh\u1ecb ph\u00e2n\u2026<\/p>\n\n\n\n<p>H\u00f4m nay, ch\u00fang ta s\u1ebd \u0111i s\u00e2u v\u00e0o con tr\u1ecf, gi\u1ea3i th\u00edch chi ti\u1ebft t\u1eebng kh\u00eda c\u1ea1nh v\u00e0 c\u00f3 nhi\u1ec1u v\u00ed d\u1ee5 \u0111\u1ec3 b\u1ea1n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc <strong>1. Con tr\u1ecf l\u00e0 g\u00ec?<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>\u0110\u1ecbnh ngh\u0129a:<\/strong><\/h3>\n\n\n\n<p>Con tr\u1ecf l\u00e0 m\u1ed9t bi\u1ebfn <strong>l\u01b0u tr\u1eef \u0111\u1ecba ch\u1ec9 c\u1ee7a m\u1ed9t bi\u1ebfn kh\u00e1c<\/strong> thay v\u00ec l\u01b0u tr\u1eef m\u1ed9t gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3. \u0110i\u1ec1u n\u00e0y gi\u00fap ch\u00fang ta thao t\u00e1c tr\u1ef1c ti\u1ebfp v\u1edbi d\u1eef li\u1ec7u trong b\u1ed9 nh\u1edb, thay v\u00ec ph\u1ea3i l\u00e0m vi\u1ec7c v\u1edbi b\u1ea3n sao c\u1ee7a n\u00f3.<\/p>\n\n\n\n<p>\ud83d\udccc <strong>C\u00e1ch khai b\u00e1o con tr\u1ecf:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int *ptr;\nchar *ptr;\nfloat *ptr;\n<\/code><\/pre>\n\n\n\n<p>D\u1ea5u <code>*<\/code> d\u00f9ng \u0111\u1ec3 khai b\u00e1o m\u1ed9t con tr\u1ecf. Ki\u1ec3u d\u1eef li\u1ec7u tr\u01b0\u1edbc d\u1ea5u <code>*<\/code> x\u00e1c \u0111\u1ecbnh lo\u1ea1i d\u1eef li\u1ec7u m\u00e0 con tr\u1ecf tr\u1ecf \u0111\u1ebfn.<\/p>\n\n\n\n<p>\ud83d\udccc <strong>C\u00e1ch g\u00e1n gi\u00e1 tr\u1ecb cho con tr\u1ecf:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int x = 10;\nint *ptr = &amp;x;  \/\/ ptr l\u01b0u \u0111\u1ecba ch\u1ec9 c\u1ee7a x\n<\/code><\/pre>\n\n\n\n<p>\u1ede \u0111\u00e2y, <code>&amp;x<\/code> l\u1ea5y \u0111\u1ecba ch\u1ec9 c\u1ee7a <code>x<\/code> v\u00e0 l\u01b0u v\u00e0o <code>ptr<\/code>.<\/p>\n\n\n\n<p>\ud83d\udccc <strong>C\u00e1ch truy xu\u1ea5t gi\u00e1 tr\u1ecb b\u1eb1ng con tr\u1ecf:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf(\"%d\", *ptr); \/\/ In ra gi\u00e1 tr\u1ecb c\u1ee7a x th\u00f4ng qua con tr\u1ecf\n<\/code><\/pre>\n\n\n\n<p>D\u1ea5u <code>*<\/code> \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 <strong>to\u00e1n t\u1eed dereference<\/strong>, gi\u00fap ch\u00fang ta l\u1ea5y gi\u00e1 tr\u1ecb t\u1ea1i \u0111\u1ecba ch\u1ec9 m\u00e0 con tr\u1ecf \u0111ang tr\u1ecf \u0111\u1ebfn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 minh h\u1ecda c\u01a1 b\u1ea3n<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    int x = 42;\n    int *ptr = &amp;x;  \n\n    printf(\"Gi\u00e1 tr\u1ecb c\u1ee7a x: %d\\n\", x);\n    printf(\"\u0110\u1ecba ch\u1ec9 c\u1ee7a x: %p\\n\", &amp;x);\n    printf(\"Gi\u00e1 tr\u1ecb c\u1ee7a ptr (\u0111\u1ecba ch\u1ec9 c\u1ee7a x): %p\\n\", ptr);\n    printf(\"Gi\u00e1 tr\u1ecb t\u1ea1i \u0111\u1ecba ch\u1ec9 ptr tr\u1ecf \u0111\u1ebfn: %d\\n\", *ptr);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong>\ud83d\udccc K\u1ebft qu\u1ea3 ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Gi\u00e1 tr\u1ecb c\u1ee7a x: 42  \n\u0110\u1ecba ch\u1ec9 c\u1ee7a x: 0x7ffcd5b6a6a8  \nGi\u00e1 tr\u1ecb c\u1ee7a ptr: 0x7ffcd5b6a6a8  \nGi\u00e1 tr\u1ecb t\u1ea1i \u0111\u1ecba ch\u1ec9 ptr tr\u1ecf \u0111\u1ebfn: 42  \n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>L\u01b0u \u00fd quan tr\u1ecdng:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0110\u1ecba ch\u1ec9 b\u1ed9 nh\u1edb c\u00f3 th\u1ec3 kh\u00e1c nhau t\u00f9y v\u00e0o m\u1ed7i l\u1ea7n ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh.<\/li>\n\n\n\n<li><code>ptr<\/code> l\u01b0u <strong>\u0111\u1ecba ch\u1ec9<\/strong> c\u1ee7a <code>x<\/code>, c\u00f2n <code>*ptr<\/code> gi\u00fap l\u1ea5y gi\u00e1 tr\u1ecb <code>x<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>2. Con tr\u1ecf NULL \u2013 Tr\u00e1nh l\u1ed7i truy c\u1eadp b\u1ed9 nh\u1edb<\/strong><\/h2>\n\n\n\n<p>M\u1ed9t con tr\u1ecf ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o s\u1ebd tr\u1ecf \u0111\u1ebfn m\u1ed9t \u0111\u1ecba ch\u1ec9 kh\u00f4ng x\u00e1c \u0111\u1ecbnh, c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i ch\u01b0\u01a1ng tr\u00ecnh. Do \u0111\u00f3, <strong>lu\u00f4n kh\u1edfi t\u1ea1o con tr\u1ecf v\u1edbi NULL n\u1ebfu ch\u01b0a c\u00f3 gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>V\u00ed d\u1ee5 v\u1ec1 con tr\u1ecf NULL<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int *ptr = NULL;\n<\/code><\/pre>\n\n\n\n<p>N\u1ebfu b\u1ea1n c\u1ed1 g\u1eafng truy xu\u1ea5t <code>*ptr<\/code>, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd b\u1ecb l\u1ed7i <strong>Segmentation Fault<\/strong>.<\/p>\n\n\n\n<p>\ud83d\udccc <strong>Ki\u1ec3m tra con tr\u1ecf tr\u01b0\u1edbc khi truy c\u1eadp<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (ptr != NULL) {\n    printf(\"Gi\u00e1 tr\u1ecb: %d\", *ptr);\n} else {\n    printf(\"Con tr\u1ecf \u0111ang NULL!\");\n}\n<\/code><\/pre>\n\n\n\n<p><strong>\ud83d\udca1 M\u1eb9o hay:<\/strong> Khi s\u1eed d\u1ee5ng <code>malloc<\/code> \u0111\u1ec3 c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb, h\u00e3y lu\u00f4n ki\u1ec3m tra xem n\u00f3 c\u00f3 th\u00e0nh c\u00f4ng hay kh\u00f4ng.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\ude80 <strong>3. Con tr\u1ecf v\u00e0 m\u1ea3ng \u2013 Truy xu\u1ea5t d\u1eef li\u1ec7u nhanh h\u01a1n<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>M\u1ea3ng b\u1ea3n ch\u1ea5t l\u00e0 con tr\u1ecf<\/strong><\/h3>\n\n\n\n<p>M\u1ea3ng trong C th\u1ef1c ch\u1ea5t l\u00e0 m\u1ed9t <strong>h\u1eb1ng con tr\u1ecf<\/strong>, ngh\u0129a l\u00e0 <code>arr<\/code> ch\u00ednh l\u00e0 \u0111\u1ecba ch\u1ec9 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean c\u1ee7a m\u1ea3ng.<\/p>\n\n\n\n<p>\ud83d\udccc <strong>V\u00ed d\u1ee5 s\u1eed d\u1ee5ng con tr\u1ecf \u0111\u1ec3 duy\u1ec7t m\u1ea3ng<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    int arr&#091;] = {1, 2, 3, 4, 5};\n    int *ptr = arr;  \/\/ ptr tr\u1ecf \u0111\u1ebfn ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean c\u1ee7a m\u1ea3ng\n\n    for (int i = 0; i &lt; 5; i++) {\n        printf(\"arr&#091;%d] = %d\\n\", i, *(ptr + i));\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u0110i\u1ec3m quan tr\u1ecdng:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ptr + i<\/code> gi\u00fap truy c\u1eadp t\u1eebng ph\u1ea7n t\u1eed trong m\u1ea3ng.<\/li>\n\n\n\n<li><code>*(ptr + i)<\/code> t\u01b0\u01a1ng \u0111\u01b0\u01a1ng v\u1edbi <code>arr[i]<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\ude80 <strong>4. C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb \u0111\u1ed9ng v\u1edbi con tr\u1ecf<\/strong><\/h2>\n\n\n\n<p>Trong C, b\u1ed9 nh\u1edb c\u00f3 hai lo\u1ea1i ch\u00ednh:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>B\u1ed9 nh\u1edb t\u0129nh (Static Memory)<\/strong> \u2013 \u0111\u01b0\u1ee3c c\u1ea5p ph\u00e1t khi bi\u00ean d\u1ecbch (compile time) v\u00e0 kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc trong qu\u00e1 tr\u00ecnh ch\u1ea1y.<\/li>\n\n\n\n<li><strong>B\u1ed9 nh\u1edb \u0111\u1ed9ng (Dynamic Memory)<\/strong> \u2013 c\u00f3 th\u1ec3 c\u1ea5p ph\u00e1t v\u00e0 gi\u1ea3i ph\u00f3ng trong qu\u00e1 tr\u00ecnh ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 <strong>T\u1ea1i sao c\u1ea7n c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb \u0111\u1ed9ng?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Khi b\u1ea1n kh\u00f4ng bi\u1ebft tr\u01b0\u1edbc s\u1ed1 l\u01b0\u1ee3ng ph\u1ea7n t\u1eed c\u1ea7n l\u01b0u tr\u1eef (VD: nh\u1eadp d\u1eef li\u1ec7u t\u1eeb ng\u01b0\u1eddi d\u00f9ng).<\/li>\n\n\n\n<li>Khi c\u1ea7n t\u1ea1o c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p nh\u01b0 danh s\u00e1ch li\u00ean k\u1ebft, c\u00e2y nh\u1ecb ph\u00e2n.<\/li>\n\n\n\n<li>Gi\u00fap s\u1eed d\u1ee5ng b\u1ed9 nh\u1edb hi\u1ec7u qu\u1ea3 h\u01a1n, tr\u00e1nh l\u00e3ng ph\u00ed t\u00e0i nguy\u00ean.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 <strong>C\u00e1c h\u00e0m c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb trong C<\/strong><\/h3>\n\n\n\n<p>C s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n <code>&lt;stdlib.h&gt;<\/code> \u0111\u1ec3 c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb \u0111\u1ed9ng v\u1edbi c\u00e1c h\u00e0m sau:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>H\u00e0m<\/th><th>C\u00f4ng d\u1ee5ng<\/th><\/tr><\/thead><tbody><tr><td><code>malloc(size_t size)<\/code><\/td><td>C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb v\u1edbi k\u00edch th\u01b0\u1edbc <code>size<\/code>, tr\u1ea3 v\u1ec1 con tr\u1ecf void (<code>void *<\/code>)<\/td><\/tr><tr><td><code>calloc(size_t n, size_t size)<\/code><\/td><td>C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb cho <code>n<\/code> ph\u1ea7n t\u1eed, m\u1ed7i ph\u1ea7n t\u1eed c\u00f3 k\u00edch th\u01b0\u1edbc <code>size<\/code>, v\u00e0 kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb 0<\/td><\/tr><tr><td><code>realloc(void *ptr, size_t new_size)<\/code><\/td><td>Thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc c\u1ee7a b\u1ed9 nh\u1edb \u0111\u00e3 c\u1ea5p ph\u00e1t b\u1edfi <code>malloc<\/code> ho\u1eb7c <code>calloc<\/code><\/td><\/tr><tr><td><code>free(void *ptr)<\/code><\/td><td>Gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb \u0111\u00e3 c\u1ea5p ph\u00e1t \u0111\u1ec3 tr\u00e1nh r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 1: S\u1eed d\u1ee5ng <code>malloc<\/code> \u0111\u1ec3 c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb cho m\u1ea3ng \u0111\u1ed9ng<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main() {\n    int *ptr;\n    int n;\n\n    printf(\"Nh\u1eadp s\u1ed1 l\u01b0\u1ee3ng ph\u1ea7n t\u1eed: \");\n    scanf(\"%d\", &amp;n);\n\n    ptr = (int*)malloc(n * sizeof(int));  \/\/ C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb cho n s\u1ed1 nguy\u00ean\n\n    if (ptr == NULL) {\n        printf(\"Kh\u00f4ng th\u1ec3 c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb!\\n\");\n        return 1;\n    }\n\n    for (int i = 0; i &lt; n; i++) {\n        printf(\"Nh\u1eadp gi\u00e1 tr\u1ecb cho ph\u1ea7n t\u1eed %d: \", i + 1);\n        scanf(\"%d\", &amp;ptr&#091;i]);\n    }\n\n    printf(\"\\nD\u1eef li\u1ec7u v\u1eeba nh\u1eadp:\\n\");\n    for (int i = 0; i &lt; n; i++) {\n        printf(\"%d \", ptr&#091;i]);\n    }\n\n    free(ptr);  \/\/ Gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb sau khi d\u00f9ng xong\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u0110i\u1ec3m quan tr\u1ecdng:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>malloc(n * sizeof(int))<\/code> c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb cho <code>n<\/code> s\u1ed1 nguy\u00ean.<\/li>\n\n\n\n<li><code>free(ptr);<\/code> \u0111\u1ec3 tr\u00e1nh r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 2: <code>calloc<\/code> vs. <code>malloc<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main() {\n    int *ptr1, *ptr2;\n    int n = 5;\n\n    ptr1 = (int*)malloc(n * sizeof(int));  \n    ptr2 = (int*)calloc(n, sizeof(int));  \n\n    printf(\"Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh khi d\u00f9ng malloc: \");\n    for (int i = 0; i &lt; n; i++) printf(\"%d \", ptr1&#091;i]);  \/\/ Kh\u00f4ng kh\u1edfi t\u1ea1o\n\n    printf(\"\\nGi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh khi d\u00f9ng calloc: \");\n    for (int i = 0; i &lt; n; i++) printf(\"%d \", ptr2&#091;i]);  \/\/ T\u1ea5t c\u1ea3 l\u00e0 0\n\n    free(ptr1);\n    free(ptr2);\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>Kh\u00e1c bi\u1ec7t:<\/strong> <code>malloc<\/code> <strong>kh\u00f4ng kh\u1edfi t\u1ea1o d\u1eef li\u1ec7u<\/strong>, c\u00f2n <code>calloc<\/code> <strong>kh\u1edfi t\u1ea1o t\u1ea5t c\u1ea3 v\u1ec1 0<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd25 <strong>5. Con tr\u1ecf v\u00e0 con tr\u1ecf h\u00e0m \u2013 S\u1ee9c m\u1ea1nh c\u1ee7a Callback Function<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 <strong>Con tr\u1ecf h\u00e0m l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p>Con tr\u1ecf h\u00e0m l\u00e0 m\u1ed9t con tr\u1ecf tr\u1ecf \u0111\u1ebfn \u0111\u1ecba ch\u1ec9 c\u1ee7a m\u1ed9t <strong>h\u00e0m<\/strong> thay v\u00ec m\u1ed9t bi\u1ebfn. \u0110i\u1ec1u n\u00e0y gi\u00fap ch\u00fang ta c\u00f3 th\u1ec3 <strong>g\u1ecdi h\u00e0m th\u00f4ng qua con tr\u1ecf<\/strong>, gi\u00fap ch\u01b0\u01a1ng tr\u00ecnh linh ho\u1ea1t h\u01a1n.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 1: Con tr\u1ecf h\u00e0m \u0111\u01a1n gi\u1ea3n<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid sayHello() {\n    printf(\"Xin ch\u00e0o! \u0110\u00e2y l\u00e0 con tr\u1ecf h\u00e0m.\\n\");\n}\n\nint main() {\n    void (*ptrFunc)();  \n    ptrFunc = sayHello;  \n    ptrFunc();  \n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>\u00dd ngh\u0129a:<\/strong> <code>ptrFunc<\/code> l\u00e0 m\u1ed9t con tr\u1ecf tr\u1ecf \u0111\u1ebfn h\u00e0m <code>sayHello<\/code>, cho ph\u00e9p g\u1ecdi h\u00e0m gi\u00e1n ti\u1ebfp.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>V\u00ed d\u1ee5 2: Truy\u1ec1n con tr\u1ecf h\u00e0m v\u00e0o m\u1ed9t h\u00e0m kh\u00e1c (Callback Function)<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid add(int a, int b) {\n    printf(\"T\u1ed5ng: %d\\n\", a + b);\n}\n\nvoid multiply(int a, int b) {\n    printf(\"T\u00edch: %d\\n\", a * b);\n}\n\n\/\/ H\u00e0m nh\u1eadn con tr\u1ecf h\u00e0m l\u00e0m tham s\u1ed1\nvoid calculate(void (*operation)(int, int), int x, int y) {\n    operation(x, y);\n}\n\nint main() {\n    calculate(add, 5, 3);\n    calculate(multiply, 5, 3);\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 <strong>\u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Callback Function<\/strong> trong l\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng.<\/li>\n\n\n\n<li><strong>Giao di\u1ec7n \u0111\u1ed3 h\u1ecda<\/strong> (GUI) khi c\u1ea7n x\u1eed l\u00fd s\u1ef1 ki\u1ec7n (event handling).<\/li>\n\n\n\n<li><strong>X\u00e2y d\u1ef1ng API linh ho\u1ea1t<\/strong> \u0111\u1ec3 ch\u1ecdn h\u00e0m th\u1ef1c thi theo tham s\u1ed1.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>6. T\u1ed5ng k\u1ebft \u2013 Nh\u1eefng l\u01b0u \u00fd quan tr\u1ecdng khi s\u1eed d\u1ee5ng con tr\u1ecf<\/strong><\/h2>\n\n\n\n<p>\ud83d\udd39 <strong>D\u00f9ng con tr\u1ecf \u0111\u00fang c\u00e1ch gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t ch\u01b0\u01a1ng tr\u00ecnh.<\/strong><br>\ud83d\udd39 <strong>Kh\u00f4ng kh\u1edfi t\u1ea1o con tr\u1ecf c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i Segmentation Fault.<\/strong><br>\ud83d\udd39 <strong>Lu\u00f4n ki\u1ec3m tra NULL khi d\u00f9ng <code>malloc<\/code> ho\u1eb7c <code>calloc<\/code>.<\/strong><br>\ud83d\udd39 <strong>D\u00f9ng <code>free(ptr);<\/code> \u0111\u1ec3 tr\u00e1nh r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb.<\/strong><br>\ud83d\udd39 <strong>Con tr\u1ecf h\u00e0m gi\u00fap vi\u1ebft m\u00e3 linh ho\u1ea1t h\u01a1n, \u00e1p d\u1ee5ng cho Callback Function.<\/strong><\/p>\n\n\n\n<p>\ud83d\udcac <strong>B\u1ea1n \u0111\u00e3 t\u1eebng g\u1eb7p l\u1ed7i n\u00e0o khi d\u00f9ng con tr\u1ecf ch\u01b0a? H\u00e3y chia s\u1ebb kinh nghi\u1ec7m c\u1ee7a b\u1ea1n!<\/strong> \u2b07\ufe0f<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>N\u1ebfu b\u1ea1n \u0111ang h\u1ecdc C m\u00e0 ch\u01b0a hi\u1ec3u v\u1ec1 con tr\u1ecf (pointers), th\u00ec b\u1ea1n ch\u01b0a th\u1ef1c s\u1ef1 l\u00e0m ch\u1ee7 ng\u00f4n ng\u1eef n\u00e0y! Con tr\u1ecf l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap l\u1eadp&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}