{"id":722,"date":"2021-08-26T12:27:55","date_gmt":"2021-08-26T17:27:55","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=722"},"modified":"2021-08-26T16:39:43","modified_gmt":"2021-08-26T21:39:43","slug":"common-c-programming-mistakes","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=722","title":{"rendered":"Common C Programming Mistakes"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">I taught C programming for many years and saw that some mistakes were fairly common, especially among people new to programming. All of these are mistakes according to the C89 standard (referred to as C89 below), but many are still wrong according to any C standard. Note that these specific error messages were generated when compiling with the GCC compiler, so the error messages you will see with a different compiler may vary from these.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You can find a video version of this with commentary at <a href=\"https:\/\/www.youtube.com\/watch?v=WS8aHMK-a9c\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=WS8aHMK-a9c\">https:\/\/www.youtube.com\/watch?v=WS8aHMK-a9c<\/a><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #1: <\/strong> get the warning<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nISO C90 forbids mixed declarations and code\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int x = 5;\r\n    printf(&quot;x = %d\\n&quot;, x);\r\n    \r\n    int z = 99;  \/* you can&#039;t declare here *\/\r\n    printf(&quot;z = %d\\n&quot;, z);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int x = 5;\r\n    int z = 99;\r\n\r\n    printf(&quot;x = %d\\n&quot;, x);\r\n    printf(&quot;z = %d\\n&quot;, z);\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: C89 requires that variables be declared at the top of a block, which is a set of curly braces. You can&#8217;t mix variable declarations with other types of statements.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #2:<\/strong> get the compilation error<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexpected expression before &#039;\/&#039; token\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int x = 5;\r\n\r\n    printf(&quot;x = %d\\n&quot;, x);   \/\/ C++ style comment\r\n}\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int x = 5;\r\n\r\n    printf(&quot;x = %d\\n&quot;, x);   \/* this IS a valid C89 \r\n                                comment style and\r\n                                can span rows  *\/\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: C89 only has one type of comment tag. Comments must be begin with \/* and end with *\/.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #3:<\/strong> get weird values<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int i, sum;\r\n\r\n    for(i = 1; i &lt; 5; i++)\r\n        sum += i;\r\n\r\n    printf(&quot;%d\\n&quot;, sum);\r\n}\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int i, sum = 0;\r\n\r\n    for(i = 1; i &lt; 5; i++)\r\n        sum += i;\r\n\r\n    printf(&quot;%d\\n&quot;, sum);\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: The first program adds i to sum and re-assigns to sum on each iteration. The problem is that sum was never given an initial value, so we are adding i to the garbage value that sum begins with. The solution is to initialize sum to whatever you want it to begin with. What makes this error even worse is that sometimes when you run your program, the initial garbage value for sum might be 0 which results in the correct value during that run.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #4:<\/strong> using = instead of ==<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int x = 5;\r\n\r\n    if(x = 99)     \/*  only a single equal sign  *\/\r\n        printf(&quot;prints this&quot;);\r\n    else\r\n        printf(&quot;doesn&#039;t print&quot;);\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: When you write<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">\tx = 99<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">you are assigning a value of 99 to x; the overall statement has a value of 99, which is TRUE. If you really want to check if x already has a value of 99 and then do something if this statement is TRUE, then you should use<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">\tx == 99<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #5:<\/strong> using int d[] instead of int d[10]<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int d&#x5B;];    \/*  error occurs here  *\/\r\n\r\n    d&#x5B;0] = 99;\r\n}\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    int d&#x5B;10];\r\n    int x&#x5B;] = {1, 2, 3};\r\n\r\n    d&#x5B;0] = 99;\r\n}\r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: The first program has the square brackets required for statically allocated arrays, but we never tell the computer how much memory to allocate. The second program demonstrates two different ways of letting the computer know how much memory to allocate. Array d explicitly tells the computer to allocate enough memory to store 10 ints. Array x leaves the square brackets empty, so the computer will count how many ints we have provided and will allocate just enough memory for these ints.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #6:<\/strong> applying sizeof to a passed array<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int d&#x5B;]) {\r\n    int i;\r\n    int cols = sizeof(d)\/sizeof(d&#x5B;0]);\r\n\r\n    for(i = 0; i &lt; cols; i++)\r\n        printf(&quot;%d&quot;, d&#x5B;i]);\r\n}\r\n\r\nint main(void) {\r\n    int d&#x5B;] = {1, 2, 3, 4};\r\n\r\n    fx(d);\r\n}\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int d&#x5B;], int cols) {\r\n    int i;\r\n\r\n    for(i = 0; i &lt; cols; i++)\r\n        printf(&quot;%d  &quot;, d&#x5B;i]);\r\n}\r\n\r\nint main(void) {\r\n    int d&#x5B;] = {1, 2, 3, 4};\r\n    int cols = sizeof(d)\/sizeof(d&#x5B;0]);\r\n\r\n    fx(d, cols);\r\n}\r\n \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: When we &#8216;pass&#8217; an array to a function, we are really passing the address of the first element of the array. Therefore, when we try to use sizeof inside the function to determine the overall size of the array, we are really getting the size of an address.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake <\/strong>#7: changing passed variables from a function (this is a logic issue)<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int x) {\r\n    x = x + 99;\r\n}\r\n\r\nint main(void) {\r\n    int x = 5;\r\n\r\n    fx(x);\r\n    printf(&quot;x is now %d\\n&quot;, x);  \/*  prints 5  *\/\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int* x) {\r\n    *x = *x + 99;  \/* a pointer is used to \r\n                      change the x in main *\/\r\n}\r\n\r\nint main(void) {\r\n    int x = 5;\r\n\r\n    fx(&amp;x);\r\n    printf(&quot;x is now %d\\n&quot;, x);  \/*  prints 104  *\/\r\n}\r\n  \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: The first program passes x to the function fx() with the goal of changing x. The problem is that the x declared in main() and the x declared in fx() are two different variables, with the initial value of the x declared in fx() being the value of the x declared in main().<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Because they are two different variables, changes to the x in fx() do not affect the x in main().<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #8: <\/strong> changing passed pointer from a function<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int* p, int d&#x5B;]) {\r\n    p = &amp;d&#x5B;1];\r\n}\r\n\r\nint main(void) {\r\n    int d&#x5B;] = {1, 99};\r\n    int* ptr = &amp;d&#x5B;0];\r\n\r\n    printf(&quot;*ptr is %d\\n&quot;, *ptr);     \/*  *ptr is 1      *\/\r\n    fx(ptr, d);\r\n    printf(&quot;*ptr is now %d\\n&quot;, *ptr); \/*  *ptr is now 1  *\/\r\n}\r\n\r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fx(int** p, int d&#x5B;]) {\r\n    *p = &amp;d&#x5B;1];\r\n}\r\n\r\nint main(void) {\r\n    int d&#x5B;] = {1, 99};\r\n    int* ptr = &amp;d&#x5B;0];\r\n\r\n    printf(&quot;*ptr is %d\\n&quot;, *ptr);     \/*  *ptr is 1            *\/\r\n    fx(&amp;ptr, d);                      \/*  pass address of ptr  *\/\r\n    printf(&quot;*ptr is now %d\\n&quot;, *ptr); \/*  *ptr is now 99       *\/\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: This is the same problem as the previous mistake. We want to change the value of the pointer created in main(), which means that we need its address. This requires a pointer to a pointer in fx().<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #9:<\/strong> using char*s instead of char s[20] and then trying to change s[0]<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    char* s = &quot;cat&quot;;\r\n\r\n    s&#x5B;0] = &#039;X&#039;;\r\n\r\n    printf(&quot;%s\\n&quot;, s);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(void) {\r\n    char s&#x5B;20] = &quot;cat&quot;;\r\n\r\n    s&#x5B;0] = &#039;X&#039;;\r\n\r\n    printf(&quot;%s\\n&quot;, s);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: In the first program, s is a <em>string literal<\/em> and contains the address of the c in cat. The C standard is fuzzy about what should happen if we try to change the contents of this string, so we should treat it as being constant. We can change the address stored in s to point to a different string, but we can&#8217;t change the original string itself. The second program allocates space for an array of 20 characters, any of which we can change.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #10:<\/strong> passing incorrect values to strcmp()<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    char text&#x5B;] = &quot;X&quot;;\r\n\r\n    if( strcmp(text, &#039;X&#039;) == 0 )      \/* error is here *\/\r\n        printf(&quot;the strings are equal\\n&quot;);\r\n    else\r\n        printf(&quot;the strings are not equal\\n&quot;);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    char text&#x5B;] = &quot;X&quot;;\r\n\r\n    if( strcmp(text, &quot;X&quot;) == 0 )\r\n        printf(&quot;the strings are equal\\n&quot;);\r\n    else\r\n        printf(&quot;the strings are not equal\\n&quot;);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: strcmp() expects two strings (i.e., null-terminated arrays of characters). These are enclosed by double quotes. A single character enclosed by single quotes is just that: a single character, which is a type of int.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #11:<\/strong> attempting to increment a value being pointed to with *ptr++<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    int x = 99;\r\n    int* ptr = &amp;x;\r\n\r\n    printf(&quot;ptr = %p and *ptr = %d\\n&quot;, ptr, x);\r\n\r\n    *ptr++;\r\n    printf(&quot;ptr = %p and *ptr = %d\\n&quot;, ptr, x);\r\n}\r\n\/*\r\nptr = 0x7fff617a4e84 and *ptr = 99\r\nptr = 0x7fff617a4e88 and *ptr = 99\r\n*\/\r\n   \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    int x = 99;\r\n    int* ptr = &amp;x;\r\n\r\n    printf(&quot;ptr = %p and *ptr = %d\\n&quot;, ptr, x);\r\n\r\n    *ptr = *ptr + 1;\r\n    printf(&quot;ptr = %p and *ptr = %d\\n&quot;, ptr, x);\r\n}\r\n\/*\r\nptr = 0x7ffffd6ac0d4 and *ptr = 99\r\nptr = 0x7ffffd6ac0d4 and *ptr = 100\r\n*\/\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: It is legal to write *ptr++. The problem, in this situation, is that due to the precedence of operators, *ptr++ says to use what is currently pointed to and then increment the address stored in the pointer when what you really want is to increment the value (i.e., the integer in this case) being pointed at.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #12:<\/strong> using char*a instead of char a[20]; this is a memory storage issue<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    char * temp;\r\n    char s&#x5B;] = &quot;this is a long string&quot;;\r\n\r\n    strcpy(temp, s);\r\n    printf(&quot;%s&quot;, temp);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    char temp&#x5B;50];\r\n    char s&#x5B;] = &quot;this is a long string&quot;;\r\n\r\n    strcpy(temp, s);\r\n    printf(&quot;%s&quot;, temp);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: In the first program, temp is of type char* but uninitialized and therefore contains garbage. The program attempts to copy the string in s to the address stored in temp. The second program allocates enough space in temp to store 50 characters.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #13<\/strong>: copying a string into dynamically-allocated space<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main(void) {\r\n    char* text = malloc(50 * sizeof(char) );\r\n    char buffer&#x5B;] = &quot;this is a string&quot;;\r\n\r\n    \/* attempt to copy buffer into text *\/\r\n    text = buffer;\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void) {\r\n    char* text = malloc(50 * sizeof(char) );\r\n    char buffer&#x5B;] = &quot;this is a string&quot;;\r\n\r\n    strcpy(text, buffer);\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: In the first program, the address of buffer is stored in text and the address of the dynamically allocated memory is lost.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Mistake #14<\/strong>: storing an address versus what is at the address; this is a memory storage issue<\/p>\r\n\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* WRONG *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nvoid readFile(FILE *fp) {\r\n    char buffer&#x5B;100];\r\n    char* firsts&#x5B;5];\r\n    char* del = &quot; &quot;;\r\n\r\n    for(i = 0; i &lt; 5; i++) {\r\n        fgets(buffer, sizeof(buffer), fp);\r\n        firsts&#x5B;i] = strtok(buffer, del);\r\n    }\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* RIGHT *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nvoid f(FILE *fp) {\r\n    char buffer&#x5B;100];\r\n    char* firsts&#x5B;5];\r\n    char *del = &quot; &quot;, *token;\r\n\r\n    for(i = 0; i &lt; 5; i++) {\r\n        fgets(buffer, sizeof(buffer), fp);\r\n        token = strtok(buffer, del);\r\n        firsts&#x5B;i] = malloc( strlen(token) + 1 );\r\n        strcpy(firsts&#x5B;i], token);\r\n    }\r\n}\r\n    \r\n<\/pre><\/div>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Explanation: The first program attempts to store the first word of each line, but really only stores the address of the first token of each line. This address is to a position in buffer and likely is to the first character of the array. The second program allocates space to store a word, saves its address in the array of addresses, and then copies the token to this space.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>I taught C programming for many years and saw that some mistakes were fairly common, especially among people new to programming. All of these are mistakes according to the C89 standard (referred to as C89 below), but many are still wrong according to any C standard. Note that these specific error messages were generated when compiling with the GCC compiler, so the error messages you will see with a different compiler may vary from these. You can find a video&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=722\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,9],"tags":[],"class_list":["post-722","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=722"}],"version-history":[{"count":10,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/722\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/722\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}