Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>char*</code> isn't quite a <code>char</code>. <code>char*</code> is basically a string (it's what strings were before C++ came along).</p> <p>For illustration:</p> <pre><code>int array[N]; // An array of N ints. char str[N]; // An array of N chars, which is also (loosely) called a string. </code></pre> <p><code>char[]</code> degrades to <code>char*</code>, so you'll often see functions take a <code>char*</code>.</p> <p>To convert <code>std::string</code> to <code>const char*</code>, you can simply call:</p> <pre><code>std::string s; s.c_str() </code></pre> <p>In this case, it's common to use the preprocessor to define your OS. This way you can use the compiler to do the platform specific stuff:</p> <pre><code>#ifdef OS_LINUX const char cls[] = "clear"; #elif OS_WIN const char cls[] = "cls"; #endif </code></pre> <p>One thing you may want to consider is making it a function. This avoids nasty dependencies of <a href="http://www.devx.com/getHelpOn/10MinuteSolution/17803" rel="nofollow noreferrer">global construction order</a>.</p> <pre><code>string GetClearCommand() { if (OS == "LINUX") { return "clear"; } else if (OS == "WIN") { return "cls"; } FAIL("No OS specified?"); return ""; } </code></pre> <p>What it looks like you're trying to do is this:</p> <pre><code>#include &lt;iostream&gt; using namespace std; #ifdef LINUX const char cls[] = "LINUX_CLEAR"; #elif WIN const char cls[] = "WIN_CLEAR"; #else const char cls[] = "OTHER_CLEAR"; #endif void fake_system(const char* arg) { std::cout &lt;&lt; "fake_system: " &lt;&lt; arg &lt;&lt; std::endl; } int main(int argc, char** argv) { fake_system(cls); return 0; } // Then build the program passing your OS parameter. $ g++ -DLINUX clear.cc -o clear $ ./clear fake_system: LINUX_CLEAR </code></pre>
    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. 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