Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you use the <code>%s</code> format specifier, it expect a value which is a pointer to the start of a string. In C, this type is <code>char *</code>.</p> <p>Taking your <code>main</code> function, your variable <code>s1</code> is of type <code>char *</code>. Therefore, <code>s1</code> is a valid parameter to <code>printf</code>, so this line is valid:</p> <pre><code>printf("%s\n", s1); </code></pre> <p>Note the <em>absence</em> of an <code>&amp;</code> in front of <code>s1</code>. In your code, you used the <code>&amp;</code>, which takes the address of <code>s1</code>, the result of which will be of type <code>char **</code>. This is the <em>wrong</em> type, so <em>don't</em> use the <code>&amp;</code>.</p> <p>The thing is, <code>printf</code> can't actually tell what type its arguments are, since it is a variadic function. It simply uses whatever arguments are there, according to the types specified in the format string.</p> <p>The same thing goes for <code>scanf</code>, but there is a pitfall: you must make sure that enough memory is allocated to account for the user input, else you will experience a buffer overflow with unpredictable results. Aside from this, <code>printf</code> and <code>scanf</code> are perfectly complementary.</p> <p>Anyhoo, this takes care of the compiler warnings, aside from the unused <code>cmds</code> variable (it's unnecessary in the provided code). Also, there is the part of <code>args</code> - it really should be a variable declared inside of <code>shell</code>, and not passed as a parameter, since its value is not even used inside <code>shell</code>.</p> <p>Don't know what's up with the rest of the code. It's superfluous considering your <code>main</code> function only calls on <code>shell</code>.</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