Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your input buffer is defined to be 64 characters, then I would recommend using a <code>char</code> array instead of a <code>char*</code>. Something like <code>char input_buffer[65];</code> should serve your purposes (add an extra character for the trailing <code>'\0'</code>).</p> <p>As far as command history goes, you can use a two-dimensional array for that. Something like <code>char command_history[20][65];</code> should let you store 20 old commands of 64 characters each.</p> <p>Allocating these buffers statically should make things a bit easier for you, as you won't have to worry about <code>malloc</code> and friends.</p> <p>It's hard to give you too much specific advice without seeing your code. I have a feeling that you are making the same type of mistakes that are typical to people first learning C. Can you post the part of your code that is giving you problems so that we can learn more about what you are doing?</p> <p><strong>Update after posted provided code:</strong></p> <p>One problem I'm seeing is that the function <code>takeInput</code> doesn't have a return statement. When you use <code>input = takeInput();</code> inside your main function, the value of <code>input</code> isn't being set to what you think it is. It's probably not a valid pointer, which is causing your line that says <code>input[j]</code> to segfault.</p> <p>Your usage of <code>cmdHistory</code> also needs revisiting. You allocate it with <code>cmdHistory = (char**)calloc(21,sizeof(int));</code>, which gives you enough space to store 21 integers. In the function <code>printHistory</code>, you pass elements of <code>cmdHistory</code> to <code>printw</code> as if they were strings (they're only integers). This is most definitely not doing what you want it to do. Instead, your allocation logic for <code>cmdHistory</code> needs to look more like your de-allocation logic (except backwards). Allocate an array of <code>char**</code>, then iterate through the array, assigning each pointer to a newly-allocated buffer. Just like you have one <code>free</code> statement for each element in the array plus a <code>free</code> for the array as a whole, you should have one <code>malloc</code> for each element plus one <code>malloc</code> for the array as a whole.</p> <p>Even if you can't use a statically-allocated stack, try writing your program using one anyway. This will let you work the kinks out of your key detection logic, etc without having to worry about the dynamic memory part of the program. Once the rest of it is working, go back in and swap out the static memory for dynamic memory allocation. That way, you're only having to debug a little bit at a time.</p>
 

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