tek4

[Lập trình C từ 0 đến 1] Bài 24. Cấp phát và giải phóng bộ nhớ

by - September. 21, 2021
Kiến thức
<p>B&agrave;i viết &ldquo;[Lập tr&igrave;nh C từ 0 đến 1] B&agrave;i 24. Cấp ph&aacute;t v&agrave; giải ph&oacute;ng bộ nhớ&rdquo; l&agrave; b&agrave;i viết tiếp theo nằm trong series hướng dẫn học lập tr&igrave;nh C của&nbsp;<a href="../../../" target="_blank" rel="noopener">tek4.vn</a>.</p> <p>Độc giả c&oacute; thể tham khảo c&aacute;c b&agrave;i viết kh&aacute;c trong series b&agrave;i viết n&agrave;y&nbsp;<a href="../../../lap-trinh-c-co-ban-den-nang-cao/" target="_blank" rel="noopener">tại đ&acirc;y</a></p> <p>B&agrave;i viết n&agrave;y sẽ cung cấp đến độc giả về kh&aacute;i niệm về Con trỏ v&agrave; c&aacute;ch sử dụng. Trong loạt b&agrave;i hướng dẫn [Lập tr&igrave;nh C từ 0 đến 1], IDE được sử dụng l&agrave; Code::Blocks</p> <h2 id="ftoc-heading-2" class="cap1 ftwp-heading">1. NHẮC LẠI VỀ CON TRỎ</h2> <p>Ch&uacute;ng ta đ&atilde; l&agrave;m quen với kiểu con trỏ trong &ldquo;B&agrave;i 6&rdquo;. Con trỏ l&agrave; biến chứa địa chỉ chứ kh&ocirc;ng phải chứa gi&aacute; trị số liệu.</p> <p>Nhận x&eacute;t:</p> <ul> <li>*p c&oacute; nghĩa: p l&agrave; con trỏ trỏ tới biến chứa gi&aacute; trị *p.</li> <li>&amp;x l&agrave; địa chỉ của biến x.</li> </ul> <h2 id="ftoc-heading-3" class="ftwp-heading">2.&nbsp;&nbsp;&nbsp;&nbsp; BIẾN &ETH;ỘNG</h2> <h3 id="ftoc-heading-4" class="ftwp-heading"><strong>2.1.&nbsp;&nbsp;Kh&aacute;i niệm</strong></h3> <p><strong>Thế n&agrave;o l&agrave; một biến&nbsp;</strong><strong>động</strong></p> <p>L&agrave; c&aacute;c biến&nbsp;<em>được tạo ra l&uacute;c chạy chương tr&igrave;nh,&nbsp;</em>t&ugrave;y theo nhu cầu. Do vậy, m&agrave; số biến n&agrave;y ho&agrave;n to&agrave;n kh&ocirc;ng&nbsp;được x&aacute;c định từ trước. C&aacute;c biến được tạo ra như vậy được gọi l&agrave; c&aacute;c biến&nbsp;động.</p> <p>C&aacute;c biến&nbsp;động kh&ocirc;ng c&oacute; t&ecirc;n (v&igrave; việc&nbsp;đặt t&ecirc;n thực chất l&agrave; g&aacute;n cho n&oacute; một&nbsp;địa chỉ x&aacute;c định).</p> <p><strong>C&aacute;ch tạo ra biến&nbsp;động v&agrave; truy nhập&nbsp;đến biến động được tiến h&agrave;nh như sau</strong></p> <p>Việc tạo ra biến&nbsp;động v&agrave; x&oacute;a n&oacute;&nbsp;đi (để thu hồi lại bộ nhớ) được thực hiện nhờ c&aacute;c h&agrave;m như malloc() v&agrave; free()&nbsp;đ&atilde; c&oacute; sẵn trong stdlib.h</p> <p>Việc truy nhập&nbsp;đến biến động được tiến h&agrave;nh nhờ c&aacute;c biến con trỏ. C&aacute;c biến con trỏ&nbsp;được định nghĩa như c&aacute;c biến tĩnh ( được khai b&aacute;o ngay từ đầu trong phần khai b&aacute;o biến) v&agrave;&nbsp;được d&ugrave;ng&nbsp;để chứa địa chỉ c&aacute;c biến động.</p> <p><strong>V&iacute; dụ minh họa</strong></p> <p>int *p; /* Khai b&aacute;o biến con trỏ p*/</p> <p>p= (int *) malloc(100)/* Tạo biến&nbsp;động*/</p> <p>&ETH;oạn chương tr&igrave;nh tr&ecirc;n sẽ cấp ph&aacute;t cho ch&uacute;ng ta 100 bytes trong bộ nhớ v&agrave; g&aacute;n&nbsp;địa chỉ khối bộ nhớ n&agrave;y cho p. 100 bytes n&agrave;y&nbsp;được d&ugrave;ng&nbsp;để lưu trữ 50 số nguy&ecirc;n.</p> <p>Giả sử, muốn cấp ph&aacute;t bộ nhớ ch&iacute;nh x&aacute;c cho 80 số nguy&ecirc;n, ch&uacute;ng ta c&oacute; thể viết:</p> <p>p=(int *) malloc(80*sizeof(int));</p> <p>Hoặc muốn cấp ph&aacute;t bộ nhớ ch&iacute;nh x&aacute;c cho 75 k&yacute; tự, c&oacute; thể viết:</p> <p>char *cp; /* Khai b&aacute;o biến con trỏ kiểu char */</p> <p>cp=(char *) malloc(75* sizeof(char));/* Tạo biến&nbsp;động */</p> <p>&ETH;ể chi tiết hơn về vấn đề n&agrave;y, ch&uacute;ng ta sang phần 2.2</p> <h3 id="ftoc-heading-5" class="ftwp-heading">2.2.Cấp ph&aacute;t v&agrave; giải ph&oacute;ng bộ nhớ&nbsp;động (c&aacute;c h&agrave;m thuộc stdlib.h v&agrave; alloc.h)</h3> <p>&ETH;ể cấp ph&aacute;t bộ nhớ&nbsp;động ta sử dụng c&aacute;c h&agrave;m trong thư viện stdlib.h hoặc alloc.h.</p> <p>Gồm:</p> <ol> <li>Cấp ph&aacute;t bộ nhớ&nbsp;động bằng h&agrave;m&nbsp;malloc</li> <li>Cấp ph&aacute;t bộ nhớ mở rộng bằng h&agrave;m alloc</li> <li>Cấp ph&aacute;t bộ nhớ động bằng h&agrave;m relloc</li> <li>Giải ph&oacute;ng bộ nhớ động bằng h&agrave;m free</li> <li>V&iacute; dụ sử dụng h&agrave;m malloc</li> <li>V&iacute; dụ sử dụng h&agrave;m calloc</li> <li>V&iacute; dụ sử dụng h&agrave;m relloc</li> </ol> <p><strong>Cấp ph&aacute;t bộ nhớ&nbsp;động bằng h&agrave;m malloc()</strong></p> <p>C&uacute; ph&aacute;p</p> <p>void *malloc(size_t size)</p> <p>Chức năng: H&agrave;m malloc cấp ph&aacute;t một v&ugrave;ng nhớ c&oacute; k&iacute;ch thước l&agrave; size.</p> <p>Trong&nbsp;đ&oacute;:</p> <p>size l&agrave; một gi&aacute; trị kiểu size_t (l&agrave; một kiểu dữ liệu&nbsp;định sẵn trong thư viện stdlib.h, ta c&oacute; thể khai b&aacute;o kiểu unsigned cũng được).</p> <p>H&agrave;m n&agrave;y trả về con trỏ kiểu void chứa&nbsp;địa chỉ &ocirc; nhớ đầu của v&ugrave;ng nhớ&nbsp;được cấp ph&aacute;t. Nếu kh&ocirc;ng đủ v&ugrave;ng nhớ&nbsp;để cấp ph&aacute;t n&oacute; sẽ trả về gi&aacute; trị&nbsp;<strong>NULL</strong>, v&igrave; vậy ta phải kiểm tra gi&aacute; trị trả về khi sử dụng h&agrave;m malloc.</p> <p><strong>Ph&acirc;n t&iacute;ch một số trường hợp minh họa</strong></p> <p><strong>V&iacute; dụ 1:</strong></p> <div id="urvanov-syntax-highlighter-61109994a5fed632024417" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5fed632024417-27">27</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-1" class="crayon-line"><span class="crayon-p"># include &amp;lt;stdlib.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-3" class="crayon-line"><span class="crayon-p"># include &amp;lt;conio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-4" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-5" class="crayon-line"><span class="crayon-t">void</span> <span class="crayon-e">main</span> <span class="crayon-sy">(</span><span class="crayon-sy">)</span> <span class="crayon-c">/* Ham chinh */</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-6" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-7" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-8" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-9" class="crayon-line"><span class="crayon-t">void</span> <span class="crayon-o">*</span><span class="crayon-v">v</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-10" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-11" class="crayon-line"><span class="crayon-e">clrscr</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-12" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-13" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-v">v</span><span class="crayon-o">=</span><span class="crayon-e">malloc</span><span class="crayon-sy">(</span><span class="crayon-cn">100</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-o">==</span><span class="crayon-t">NULL</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-14" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-15" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-16" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-17" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"Khong du bo nho"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-18" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-19" class="crayon-line"><span class="crayon-e">exit</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-20" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-21" class="crayon-line"><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-22" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-23" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"Bo nho da duoc cap phat"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-24" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-25" class="crayon-line"><span class="crayon-e">getch</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-26" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5fed632024417-27" class="crayon-line"><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p>Nếu muốn cấp ph&aacute;t v&ugrave;ng nhớ&nbsp;để lưu một loại dữ liệu x&aacute;c định n&agrave;o&nbsp;đ&oacute; th&igrave; ta c&oacute; thể &eacute;p kiểu&nbsp;đối với con trỏ trả về của h&agrave;m malloc:</p> <p><strong>V&iacute; dụ 2:</strong></p> <div id="urvanov-syntax-highlighter-61109994a5ff6866917296" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ff6866917296-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ff6866917296-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ff6866917296-3">3</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-61109994a5ff6866917296-1" class="crayon-line"><span class="crayon-t">int</span> <span class="crayon-o">*</span><span class="crayon-v">num</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ff6866917296-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ff6866917296-3" class="crayon-line"><span class="crayon-v">num</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-t">int</span><span class="crayon-o">*</span><span class="crayon-sy">)</span><span class="crayon-e">malloc</span><span class="crayon-sy">(</span><span class="crayon-cn">50</span><span class="crayon-o">*</span><span class="crayon-e">sizeof</span><span class="crayon-sy">(</span><span class="crayon-t">int</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p>d&ograve;ng lệnh tr&ecirc;n y&ecirc;u cầu cấp ph&aacute;t v&ugrave;ng nhớ&nbsp;để lưu giữ 50 gi&aacute; trị kiểu nguy&ecirc;n. Gi&aacute; trị trả về l&agrave; con trỏ kiểu nguy&ecirc;n lưu&nbsp;địa chỉ đầu của v&ugrave;ng nhớ&nbsp;được cấp ph&aacute;t.</p> <p><strong>Ch&uacute; &yacute;:&nbsp;</strong>To&aacute;n tử&nbsp;<strong>sizeof</strong>&nbsp;cho ta k&iacute;ch cỡ t&iacute;nh theo byte của một kiểu dữ liệu (int, float, hay c&aacute;c kiểu&nbsp;được định nghĩa trong chương tr&igrave;nh bằng typedef, enum.) cũng như một&nbsp;đối tượng dữ liệu (biến, mảng,cấu tr&uacute;c). N&oacute; được viết như sau:</p> <p>sizeof(kiểu dữ liệu)</p> <p>sizeof(đối tượng dữ liệu)</p> <p>V&iacute; dụ: Như tr&ecirc;n sizeof(int) cho ta k&iacute;ch cỡ t&iacute;nh theo byte của kiểu int (bằng 2 byte ).</p> <p><strong>Cấp ph&aacute;t bộ nhớ&nbsp;động bằng h&agrave;m calloc()</strong></p> <p>C&uacute; ph&aacute;p</p> <p>(datatype *) calloc(n, sizeof(object));</p> <p>Chức năng của h&agrave;m l&agrave;: cấp ph&aacute;t bộ nhớ&nbsp;động cho c&aacute;c kiểu dữ liệu (c&oacute; thể l&agrave; những kiểu dữ liệu kh&ocirc;ng phải kiểu cơ sở)</p> <p><strong>Trong&nbsp;đ&oacute;:</strong></p> <p>datatype *) l&agrave; kiểu con trỏ trỏ tới kiểu dữ liệu datatype.</p> <p>n l&agrave; số lượng object thuộc kiểu datatype m&agrave; ta cần cấp ph&aacute;t bộ nhớ.</p> <p>Kiểu datatype c&oacute; thể l&agrave; những kiểu dữ liệu mới do người lập tr&igrave;nh s&aacute;ng tạo ra.</p> <p><strong>Ph&acirc;n t&iacute;ch trường hợp minh họa</strong></p> <p>struct sv {</p> <p>char ht[30];</p> <p>unsigned int tuoi;</p> <p>unsigned int diem;</p> <p>} /* Khai b&aacute;o cấu tr&uacute;c */</p> <p>struct sv *p_sv;/* Khai b&aacute;o con trỏ sv*/</p> <p>Cấp ph&aacute;t bộ nhớ cho 10 người:</p> <p>calloc(10, sizeof(struct sv));</p> <p><strong>Cấp ph&aacute;t bộ nhớ&nbsp;động bằng h&agrave;m relloc()</strong></p> <p>C&uacute; ph&aacute;p</p> <p>(datatype *) realloc(buf _p, newsize);</p> <p>Ph&acirc;n t&iacute;ch</p> <p>Chức năng: H&agrave;m cấp ph&aacute;t lại bộ nhớ</p> <p>Trong&nbsp;đ&oacute;:</p> <p>buf_p l&agrave; con trỏ&nbsp;đang trỏ đến v&ugrave;ng &ocirc; nhớ&nbsp;đ&atilde;&nbsp;được cấp ph&aacute;t từ trước.</p> <p>newsize l&agrave; k&iacute;ch thước mới cần cấp ph&aacute;t, c&oacute; thể to hoặc nhỏ hơn.</p> <p><strong>Giải ph&oacute;ng bộ nhớ&nbsp;động bằng h&agrave;m free()</strong></p> <p>C&uacute; ph&aacute;p</p> <p>void free( void *prt)</p> <p>Chức năng:</p> <p>H&agrave;m free giải ph&oacute;ng v&ugrave;ng nhớ&nbsp;được trỏ đến bởi con trỏ ptr.</p> <p>Nếu ptr = NULL th&igrave; free kh&ocirc;ng l&agrave;m g&igrave; cả.</p> <p>Chương tr&igrave;nh v&iacute; dụ</p> <p>Viết chương tr&igrave;nh nhập c&aacute;c số nguy&ecirc;n từ b&agrave;n ph&iacute;m. Xuất c&aacute;c số ra m&agrave;n h&igrave;nh ( kh&ocirc;ng sử dụng mảng):</p> <p># include &ldquo;stdio.h&rdquo;</p> <p># include &ldquo;stdlib.h&rdquo;</p> <p># include &ldquo;conio.h&rdquo;</p> <p>void main () /* Ham chinh */</p> <p>{</p> <p>int *num,i,j;</p> <p>clrscr();</p> <p>printf(&ldquo;Nhap so phan tu&rdquo;);</p> <p>scanf(&ldquo;%d&rdquo;,&amp;i);</p> <p>/* Cấp ph&aacute;t bộ nhớ bằng h&agrave;m malloc*/</p> <p>num=(int*)malloc(i*sizeof(int));</p> <p>for (j=0;j&lt;i;j++)</p> <p>{</p> <p>printf(&ldquo;\n K&yacute; tự thứ %d&rdquo;,j);</p> <p>scanf(&rdquo; %d&rdquo;,(num+j));</p> <p>}</p> <p>for (j=0;j&lt;i;j++)</p> <p>printf(&ldquo;%c \n&rdquo;,*(num+j));</p> <p>getch();</p> <p>/*Giải ph&oacute;ng bộ nhớ*/</p> <p>free(num);</p> <p>}</p> <p><strong>V&iacute; dụ sử dụng h&agrave;m malloc</strong></p> <p><em><strong>V&iacute; dụ:</strong></em>&nbsp;D&ugrave;ng h&agrave;m malloc cấp ph&aacute;t bộ nhớ cho một chuỗi k&yacute; tự</p> <p>#include &lt;stdio.h&gt;</p> <p>#include &lt;string.h&gt;</p> <p>#include &lt;alloc.h&gt;</p> <p>#include &lt;process.h&gt;</p> <p>int main(void)</p> <p>{</p> <p>char *str;</p> <p>/*Cấp ph&aacute;t bộ nhớ cho chuỗi */</p> <p>if ((str = (char *) malloc(10)) == NULL)</p> <p>{</p> <p>printf(&ldquo;Kh&ocirc;ng&nbsp;đủ bộ nhớ để cấp ph&aacute;t \n&rdquo;);</p> <p>exit(1); /* Kết th&uacute;c chương tr&igrave;nh, nếu tr&agrave;n bộ nhớ*/</p> <p>}</p> <p>/* Sao ch&eacute;p &ldquo;Hello&rdquo; v&agrave;o chuỗi */</p> <p>strcpy(str, &ldquo;Hello&rdquo;);</p> <p>/* Hiển thị chuỗi */</p> <p>printf(&ldquo;Chuỗi k&yacute; tự l&agrave;: %s\n&rdquo;, str);</p> <p>/* Giải ph&oacute;ng bộ nhớ */</p> <p>free(str);</p> <p>return 0;</p> <p>}</p> <p><strong>V&iacute; dụ sử dụng h&agrave;m calloc</strong></p> <p><em><strong>V&iacute; dụ:</strong></em>&nbsp;D&ugrave;ng h&agrave;m calloc cấp ph&aacute;t bộ nhớ cho một chuỗi k&yacute; tự</p> <p>#include &lt;stdio.h&gt;</p> <p>#include &lt;alloc.h&gt;</p> <p>#include &lt;string.h&gt;</p> <p>int main(void)</p> <p>{</p> <p>char *str = NULL;</p> <p>/* Cấp ph&aacute;t bộ nhớ cho chuỗi */</p> <p>str = (char *) calloc(10, sizeof(char));</p> <p>/* Sao ch&eacute;p &ldquo;Hello&rdquo; v&agrave;o chuỗi */</p> <p>strcpy(str, &ldquo;Hello&rdquo;);</p> <p>/* Hiển thị chuỗi */</p> <p>printf(&ldquo;Chuỗi k&yacute; tự l&agrave; %s\n&rdquo;, str);</p> <p>/* Giải ph&oacute;ng bộ nhớ */</p> <p>free(str);</p> <p>return 0;</p> <p>}</p> <p><strong>V&iacute; dụ sử dụng h&agrave;m realloc</strong></p> <p><em><strong>V&iacute; dụ:</strong></em>&nbsp;D&ugrave;ng h&agrave;m realloc cấp ph&aacute;t lại bộ nhớ cho một chuỗi k&yacute; tự</p> <p>#include &lt;stdio.h&gt;</p> <p>#include &lt;alloc.h&gt;</p> <p>#include &lt;string.h&gt;</p> <p>int main(void)</p> <p>{</p> <p>char *str;</p> <p>/* Cấp ph&aacute;t bộ nhớ cho chuỗi */</p> <p>str = (char *) malloc(10);</p> <p>/*Sao ch&eacute;p &ldquo;Hello&rdquo; v&agrave;o chuỗi */</p> <p>strcpy(str, &ldquo;Hello&rdquo;);</p> <p>printf(&ldquo;Chuỗi k&yacute; tự l&agrave; %s\n &ETH;ịa chỉ l&agrave; %p\n&rdquo;, str, str);</p> <p>str = (char *) realloc(str, 20);</p> <p>printf(&ldquo;Chuỗi k&yacute; tự l&agrave; %s\n &ETH;ịa chỉ mới l&agrave; %p\n&rdquo;, str, str);</p> <p>/*Giải ph&oacute;ng bộ nhớ */</p> <p>free(str);</p> <p>return 0;</p> <p>}</p> <h2 id="ftoc-heading-6" class="ftwp-heading">3.&nbsp;&nbsp;&nbsp;&nbsp; BỘ NHỚ HEAP V&Agrave; CƠ CHẾ TẠO BIẾN &ETH;ỘNG</h2> <h3 id="ftoc-heading-7" class="ftwp-heading"><strong>3.1.&nbsp;&nbsp;Kh&aacute;i niệm</strong></h3> <p>C&aacute;c biến&nbsp;động do malloc tạo ra được C xếp v&agrave;o một v&ugrave;ng &ocirc; nhớ tự do theo kiểu xếp chồng v&agrave;&nbsp;được gọi l&agrave; HEAP ( bộ nhơ cấp ph&aacute;t&nbsp;động). Ng&ocirc;n ngữ C quản l&yacute; HEAP th&ocirc;ng qua một con trỏ của HEAP l&agrave; HEAPPTR. N&oacute; lu&ocirc;n trỏ v&agrave;o byte tự do&nbsp;đầu ti&ecirc;n của v&ugrave;ng &ocirc; nhớ c&ograve;n tự do của HEAP. Mỗi lần gọi malloc(), con trỏ của HEAP&nbsp;được dịch chuyển về ph&iacute;a đỉnh của v&ugrave;ng &ocirc; nhớ tự do một số byte tương ứng với k&iacute;ch thước của biến&nbsp;động mới tạo ra.</p> <p>Ngược lại, mỗi khi giải ph&oacute;ng bộ nhớ biến&nbsp;động, bộ nhớ biến động được thu hồi. Tuy nhi&ecirc;n, nếu việc tạo v&agrave; thu hồi kh&ocirc;ng phải l&agrave; qu&aacute; tr&igrave;nh li&ecirc;n tục v&agrave; kế cận th&igrave; c&oacute; thể xảy ra trường hợp: C&oacute; những v&ugrave;ng thu hồi nằm lọt trong v&ugrave;ng c&aacute;c biến&nbsp;động kh&aacute;c vẫn c&ograve;n&nbsp;đang hoạt động.</p> <h3 id="ftoc-heading-8" class="ftwp-heading">3.2.V&iacute; dụ &aacute;p dụng</h3> <p>T&igrave;m c&aacute;c số nguy&ecirc;n tố trong d&atilde;y số tự nhi&ecirc;n.</p> <table width="70%"> <tbody> <tr> <td width="100%"> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-27">27</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-28">28</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-29">29</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-30">30</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-31">31</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-32">32</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-33">33</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-34">34</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-35">35</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-36">36</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-37">37</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-38">38</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-39">39</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-40">40</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-41">41</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-42">42</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-43">43</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-44">44</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-45">45</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-46">46</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-47">47</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-48">48</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-49">49</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-50">50</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-51">51</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-52">52</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-53">53</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-54">54</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-55">55</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-56">56</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-57">57</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-58">58</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-59">59</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-60">60</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-61">61</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-62">62</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-63">63</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-64">64</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-65">65</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-66">66</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-67">67</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-68">68</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a5ffc966084908-69">69</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-1" class="crayon-line"><span class="crayon-p">#include&amp;lt;stdio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-3" class="crayon-line"><span class="crayon-p">#include&amp;lt;conio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-4" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-5" class="crayon-line"><span class="crayon-p">#include&amp;lt;stdlib.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-6" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-7" class="crayon-line"><span class="crayon-p">#include&amp;lt;alloc.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-8" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-9" class="crayon-line"><span class="crayon-t">void</span> <span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-10" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-11" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-12" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-13" class="crayon-line"><span class="crayon-t">long</span> <span class="crayon-o">*</span><span class="crayon-v">primes</span><span class="crayon-o">=</span><span class="crayon-t">NULL</span><span class="crayon-sy">,</span> <span class="crayon-o">*</span><span class="crayon-v">start</span><span class="crayon-o">=</span><span class="crayon-t">NULL</span><span class="crayon-sy">,</span> <span class="crayon-o">*</span><span class="crayon-v">open</span><span class="crayon-o">=</span><span class="crayon-t">NULL</span><span class="crayon-sy">,</span> <span class="crayon-v">trial</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-14" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-15" class="crayon-line"><span class="crayon-t">int</span> <span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">,</span> <span class="crayon-v">found</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">,</span> <span class="crayon-v">total</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-16" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-17" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"Bao nhieu so nguyen to ban can tim ?"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-18" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-19" class="crayon-line"><span class="crayon-e">scanf</span><span class="crayon-sy">(</span><span class="crayon-s">"%d"</span><span class="crayon-sy">,</span><span class="crayon-o">&amp;</span><span class="crayon-v">amp</span><span class="crayon-sy">;</span><span class="crayon-v">total</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-20" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-21" class="crayon-line"><span class="crayon-v">primes</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-t">long</span> <span class="crayon-o">*</span><span class="crayon-sy">)</span> <span class="crayon-e">malloc</span><span class="crayon-sy">(</span><span class="crayon-e ">total*</span><span class="crayon-e">sizeof</span><span class="crayon-sy">(</span><span class="crayon-t">long</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-22" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-23" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">==</span><span class="crayon-t">NULL</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-24" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-25" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-26" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-27" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"\n Khong du bo nho"</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-28" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-29" class="crayon-line"><span class="crayon-st">return</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-30" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-31" class="crayon-line"><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-32" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-33" class="crayon-line"><span class="crayon-c">/*3 so nguyen to dau tien da biet*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-34" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-35" class="crayon-line"><span class="crayon-o">*</span><span class="crayon-v">primes</span><span class="crayon-o">=</span><span class="crayon-cn">2</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-36" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-37" class="crayon-line"><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-o">=</span><span class="crayon-cn">3</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-38" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-39" class="crayon-line"><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-o">=</span><span class="crayon-cn">5</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-40" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-41" class="crayon-line"><span class="crayon-v">open</span><span class="crayon-o">=</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-cn">3</span><span class="crayon-sy">;</span><span class="crayon-c">/*Lay dia chi phan o nho tu do tiep theo*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-42" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-43" class="crayon-line"><span class="crayon-v">trial</span><span class="crayon-o">=</span><span class="crayon-cn">5</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-44" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-45" class="crayon-line"><span class="crayon-st">do</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-46" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-47" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-48" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-49" class="crayon-line"><span class="crayon-v">trial</span><span class="crayon-o">+=</span><span class="crayon-cn">2</span><span class="crayon-sy">;</span> <span class="crayon-c">/*Gia tri tiep theo de kiem tra*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-50" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-51" class="crayon-line"><span class="crayon-v">start</span><span class="crayon-o">=</span><span class="crayon-v">primes</span><span class="crayon-sy">;</span> <span class="crayon-c">/*Start tro vao phan dau cua prime*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-52" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-53" class="crayon-line"><span class="crayon-v">found</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-54" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-55" class="crayon-line"><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span><span class="crayon-v">open</span><span class="crayon-o">-</span><span class="crayon-v">primes</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-56" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-57" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-v">found</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-v">trial</span><span class="crayon-o">%</span><span class="crayon-o">*</span><span class="crayon-v">start</span><span class="crayon-o">++</span><span class="crayon-sy">)</span><span class="crayon-o">==</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span> <span class="crayon-st">break</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-58" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-59" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-v">found</span><span class="crayon-o">==</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span> <span class="crayon-c">/*Tim thay mot so moi*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-60" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-61" class="crayon-line"><span class="crayon-o">*</span><span class="crayon-v">open</span><span class="crayon-o">++</span><span class="crayon-o">=</span><span class="crayon-v">trial</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-62" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-63" class="crayon-line"><span class="crayon-sy">}</span> <span class="crayon-st">while</span> <span class="crayon-sy">(</span><span class="crayon-v">open</span><span class="crayon-o">-</span><span class="crayon-v">primes</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span><span class="crayon-o">=</span><span class="crayon-v">total</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-64" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-65" class="crayon-line"><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span> <span class="crayon-cn">5</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">total</span><span class="crayon-o">/</span><span class="crayon-cn">5</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">+=</span><span class="crayon-cn">5</span><span class="crayon-sy">)</span> <span class="crayon-c">/*Hien thi 5 so 1 lan*/</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-66" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-67" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"\n %12ld %12ld %12ld %12ld %12ld"</span><span class="crayon-sy">,</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-o">+</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-o">+</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-o">+</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-o">*</span><span class="crayon-sy">(</span><span class="crayon-v">primes</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-o">+</span><span class="crayon-cn">4</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-68" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a5ffc966084908-69" class="crayon-line"><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <br />&nbsp;</td> </tr> <tr> <td width="100%">Kết quả chạy chương tr&igrave;nh: <p>Bao nhieu so nguyen to ban can tim ?10</p> <p>2 3 5 7 11</p> <p>13 17 19 23 29</p> </td> </tr> </tbody> </table> <h2 id="ftoc-heading-9" class="ftwp-heading">4.&nbsp;&nbsp;&nbsp;&nbsp; B&Agrave;I TẬP MINH HỌA</h2> <h3 id="ftoc-heading-10" class="ftwp-heading">&nbsp;B&agrave;i 1: Viết chương tr&igrave;nh nhập một chữ, xuất ra chữ&nbsp;đ&oacute; nhiều lần d&ugrave;ng con trỏ</h3> <table width="70%"> <tbody> <tr> <td width="100%"> <div id="urvanov-syntax-highlighter-61109994a6007906081399" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-27">27</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-28">28</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-29">29</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-30">30</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-31">31</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-32">32</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-33">33</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-34">34</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-35">35</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-36">36</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-37">37</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-38">38</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-39">39</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-40">40</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-41">41</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-42">42</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-43">43</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-44">44</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-45">45</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a6007906081399-46">46</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-61109994a6007906081399-1" class="crayon-line"><span class="crayon-c">/*Viết chương tr&igrave;nh nhập một chữ, xuất ra chữ&nbsp;đ&oacute; nhiều lần d&ugrave;ng con trỏ*/</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-3" class="crayon-line"><span class="crayon-p">#include&amp;lt;stdio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-4" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-5" class="crayon-line"><span class="crayon-p">#include&amp;lt;conio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-6" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-7" class="crayon-line"><span class="crayon-t">char</span> <span class="crayon-o">*</span><span class="crayon-e">lap</span><span class="crayon-sy">(</span><span class="crayon-t">char</span> <span class="crayon-v">chu</span><span class="crayon-sy">,</span><span class="crayon-t">int</span> <span class="crayon-v">lan</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-8" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-9" class="crayon-line"><span class="crayon-t">void</span> <span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-10" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-11" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-12" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-13" class="crayon-line"><span class="crayon-t">char</span> <span class="crayon-v">c</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-14" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-15" class="crayon-line"><span class="crayon-e">clrscr</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-16" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-17" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"\n%s"</span><span class="crayon-sy">,</span><span class="crayon-e">lap</span><span class="crayon-sy">(</span><span class="crayon-s">'s'</span><span class="crayon-sy">,</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-18" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-19" class="crayon-line"><span class="crayon-v">c</span><span class="crayon-o">=</span><span class="crayon-e">getchar</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-20" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-21" class="crayon-line"><span class="crayon-e">printf</span><span class="crayon-sy">(</span><span class="crayon-s">"\n%s"</span><span class="crayon-sy">,</span><span class="crayon-e">lap</span><span class="crayon-sy">(</span><span class="crayon-v">c</span><span class="crayon-sy">,</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-22" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-23" class="crayon-line"><span class="crayon-e">getch</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-24" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-25" class="crayon-line"><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-26" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-27" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-28" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-29" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-30" class="crayon-line crayon-striped-line"><span class="crayon-t">char</span> <span class="crayon-o">*</span><span class="crayon-e">lap</span><span class="crayon-sy">(</span><span class="crayon-t">char</span> <span class="crayon-v">chu</span><span class="crayon-sy">,</span><span class="crayon-t">int</span> <span class="crayon-v">lan</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-31" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-32" class="crayon-line crayon-striped-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-33" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-34" class="crayon-line crayon-striped-line"><span class="crayon-t">char</span> <span class="crayon-o">*</span><span class="crayon-v">supp</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-35" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-36" class="crayon-line crayon-striped-line"><span class="crayon-t">int</span> <span class="crayon-v">i</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-37" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-38" class="crayon-line crayon-striped-line"><span class="crayon-v">supp</span><span class="crayon-o">=</span><span class="crayon-e">calloc</span><span class="crayon-sy">(</span><span class="crayon-v">lan</span><span class="crayon-sy">,</span><span class="crayon-e">sizeof</span><span class="crayon-sy">(</span><span class="crayon-t">char</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-39" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-40" class="crayon-line crayon-striped-line"><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span><span class="crayon-o">=</span><span class="crayon-v">lan</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-41" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-42" class="crayon-line crayon-striped-line"><span class="crayon-v">supp</span><span class="crayon-sy">[</span><span class="crayon-v">i</span><span class="crayon-sy">]</span><span class="crayon-o">=</span><span class="crayon-v">chu</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-43" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-44" class="crayon-line crayon-striped-line"><span class="crayon-st">return</span> <span class="crayon-sy">(</span><span class="crayon-v">supp</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-45" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-61109994a6007906081399-46" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> </td> </tr> </tbody> </table> <h3 id="ftoc-heading-11" class="ftwp-heading">&nbsp;B&agrave;i 2: Viết chương tr&igrave;nh cho một d&ograve;ng quảng c&aacute;o chạy từ phải sang tr&aacute;i của m&agrave;n h&igrave;nh</h3> <table width="70%"> <tbody> <tr> <td width="100%"> <div id="urvanov-syntax-highlighter-61109994a600d637871863" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-27">27</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-28">28</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-29">29</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-30">30</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-31">31</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-32">32</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-33">33</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-34">34</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-35">35</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-36">36</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-37">37</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-38">38</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-39">39</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-40">40</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-41">41</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-42">42</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-43">43</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-44">44</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-45">45</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-46">46</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-61109994a600d637871863-47">47</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-61109994a600d637871863-1" class="crayon-line"><span class="crayon-c">/*Viết chương tr&igrave;nh cho một d&ograve;ng quảng c&aacute;o chạy từ phải sang tr&aacute;i của m&agrave;n h&igrave;nh*/</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-3" class="crayon-line"><span class="crayon-p">#include&amp;lt;stdio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-4" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-5" class="crayon-line"><span class="crayon-p">#include&amp;lt;conio.h&amp;gt;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-6" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-7" class="crayon-line"><span class="crayon-t">void</span> <span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-8" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-9" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-10" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-11" class="crayon-line"><span class="crayon-t">char</span> <span class="crayon-v">st</span><span class="crayon-sy">[</span><span class="crayon-cn">123</span><span class="crayon-sy">]</span><span class="crayon-o">=</span><span class="crayon-s">"Turbo C kinh chao cac ban "</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-12" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-13" class="crayon-line"><span class="crayon-t">char</span> <span class="crayon-o">*</span><span class="crayon-v">st_show</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-14" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-15" class="crayon-line"><span class="crayon-t">int</span> <span class="crayon-v">i</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-16" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-17" class="crayon-line"><span class="crayon-e">clrscr</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-18" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-19" class="crayon-line"><span class="crayon-v">st_show</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-t">char</span> <span class="crayon-o">*</span><span class="crayon-sy">)</span> <span class="crayon-e">calloc</span><span class="crayon-sy">(</span><span class="crayon-cn">80</span><span class="crayon-sy">,</span><span class="crayon-e">sizeof</span><span class="crayon-sy">(</span><span class="crayon-t">char</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-20" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-21" class="crayon-line"><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span><span class="crayon-cn">80</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-22" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-23" class="crayon-line"><span class="crayon-e">strcat</span><span class="crayon-sy">(</span><span class="crayon-v">st_show</span><span class="crayon-sy">,</span><span class="crayon-s">" "</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-24" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-25" class="crayon-line"><span class="crayon-e">strrev</span><span class="crayon-sy">(</span><span class="crayon-v">st</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-26" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-27" class="crayon-line"><span class="crayon-e">strcat</span><span class="crayon-sy">(</span><span class="crayon-v">st</span><span class="crayon-sy">,</span><span class="crayon-v">st_show</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-28" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-29" class="crayon-line"><span class="crayon-e">strrev</span><span class="crayon-sy">(</span><span class="crayon-v">st</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-30" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-31" class="crayon-line"><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">&amp;</span><span class="crayon-v">lt</span><span class="crayon-sy">;</span><span class="crayon-cn">80</span><span class="crayon-sy">;</span><span class="crayon-v">i</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-32" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-33" class="crayon-line"><span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-34" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-35" class="crayon-line"><span class="crayon-e">strncpy</span><span class="crayon-sy">(</span><span class="crayon-v">st_show</span><span class="crayon-sy">,</span><span class="crayon-v">st</span><span class="crayon-o">+</span><span class="crayon-v">i</span><span class="crayon-sy">,</span><span class="crayon-cn">79</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-36" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-37" class="crayon-line"><span class="crayon-e">gotoxy</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-38" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-39" class="crayon-line"><span class="crayon-e">cprintf</span><span class="crayon-sy">(</span><span class="crayon-s">"%s"</span><span class="crayon-sy">,</span><span class="crayon-v">st_show</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-40" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-41" class="crayon-line"><span class="crayon-e">delay</span><span class="crayon-sy">(</span><span class="crayon-cn">100</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-42" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-43" class="crayon-line"><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-44" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-45" class="crayon-line"><span class="crayon-e">free</span><span class="crayon-sy">(</span><span class="crayon-v">st_show</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-46" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-61109994a600d637871863-47" class="crayon-line"><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> </td> </tr> </tbody> </table> <p>Ch&uacute;ng ta sẽ kết th&uacute;c &ldquo;B&agrave;i 24. Cấp ph&aacute;t v&agrave; giải ph&oacute;ng bộ nhớ&rdquo; trong series [Lập tr&igrave;nh C từ 0 đến 1] ở đ&acirc;y. H&atilde;y c&ugrave;ng đ&oacute;n xem &ldquo;B&agrave;i 25. Struct trong C, c&aacute;c kiểu dữ liệu tự định nghĩa&rdquo;.</p> <p>H&atilde;y để lại b&igrave;nh luận v&agrave; theo d&otilde;i series b&agrave;i viết: &ldquo;<a href="../../../tu-khoa/lap-trinh-c-tu-0-den-1/">Lập tr&igrave;nh C từ 0 đến 1</a>&rdquo; để tiếp tục cập nhật những th&ocirc;ng tin mới nhất.</p>