Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I started to fix this code but soon realized there is so much wrong in it, I don't know where to start. So instead, this turned out to be a <a href="https://codereview.stackexchange.com/">code review</a> answer instead, I apologize up front if it is detailed and picky.</p> <ul> <li><p>There's a rule of thumb in C saying that if you need more than two levels of pointer indirection, your code is obfuscated and should be rewritten (reference MISRA-C:2004 17.5).</p></li> <li><p>It doesn't make sense whatsoever to use dynamic memory allocation in this case, because you already know at program start-up that no string will be larger than 11 characters and there will not be more than 30 strings. If this condition is not true, you need to write a safer input method, preferably with fgets() which is safe against buffer overflows. Make sure that the input doesn't go out of bounds of array "Cod". You can allocate 30*11=330 bytes statically without a guilty conscience. And it will make the code faster.</p></li> <li><p>It doesn't make sense to have 3 levels of indirection for an array of strings. You aren't even using the dynamic memory to hold a copy of strings, you just allocate pointers. This doesn't make any sense at all. If you need a pointer lookup table pointing at <code>Cod</code> then allocate it statically, it will only require sizeof(char*)*30 bytes. </p></li> <li><p>As already mentioned, you can only use realloc on a pointer that has previously been malloc/calloc:ed.</p></li> <li><p>As already mentioned, never typecast the result of malloc/realloc in C. This is C++ practice. In C, it destroys type safety and hides type compatibility bugs. There are countless, detailed discussions about this here on SO if you want to know the details.</p></li> <li><p>What if you don't find "p" in the user string? The program will go havoc.</p></li> <li><p>Don't name variables that affect fundamental program functionality to abstract things like tmp, vec etc. tmp could be renamed to input_buf or something, etc.</p></li> <li><p>Avoid magic numbers in code, use const or #define for array length constants.</p></li> <li><p>You can initialize strings in C, there is no need for strcpy to do so. <code>char input_buf[INP_BUF_N] = "p";</code></p></li> <li><p>To search for a char in a string, use strchr().</p></li> <li><p>You shouldn't need to have the user inputting the same thing twice with scanf() in the outer do-while loop, likely a typo bug.</p></li> <li><p>You can not do wild typecast between a static array of arrays to a pointer-to-pointer. This depends on the structure of whatever the pointer-to-pointer points at. Because a typical dumb-school-book dynamic 2D-array (malloc(X<em>sizeof(char</em>)... malloc(Y*sizeof(char)) will not allocate memory adjacently. Plenty of discussions about this here on SO.</p></li> </ul> <p>(you can allocate dynamic 2D arrays in adjacent memory with the use of array pointers or with "mangling", but those are rather advanced topics)</p> <ul> <li>free() the dynamic memory once you are done using it.</li> </ul> <p>As you hopefully can tell, the wise choice here is to rewrite this code from scratch.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload