Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>char **argv[] </code></pre> <p>Is wrong. It should be either <code>char **argv</code> or <code>char *argv[]</code>, not a mixture of both. And then it becomes a pointer-to-pointer to characters, or rather a pointer to c-strings, i.e., an array of c-strings. :) <a href="http://cdecl.org" rel="nofollow">cdecl.org</a> is also quite helpful at thing like this.<br> Then, for the access, sure. Just, well, access it. :) <code>argv[0]</code> would be the first string, <code>argv[3]</code> would be the 4th string. But I totally wouldn't recommend replacing stuff in an array that isn't yours or that you know the internals of.<br> On array resize, since you're writing C++, use <a href="http://www.cplusplus.com/reference/stl/vector/" rel="nofollow"><code>std::vector</code></a>, which does all the complicated allocation stuff for you and is really safe. Generally, it depends on the array type. Dynamically allocated arrays (<code>int* int_arr = new int[20]</code>) can, static arrays (<code>int int_arr[20]</code>) can't.<br> To copy everything in <code>argv</code> into a single <code>std::string</code>, loop through the array and append every c-string to your <code>std::string</code>. I wouldn't recommend that though, rather have a <code>std::vector&lt;std::string&gt;</code>, i.e., an array of <code>std::strings</code>, each holding one of the arguments.</p> <pre><code>std::vector&lt;std::string&gt; args; for(int i=0; i &lt; argc; ++i){ args.push_back(argv[i]); } </code></pre> <p>On your last point, since the standard demands <code>argv</code> to be terminated by a <code>NULL</code> pointer, it's quite easy.</p> <pre><code>int myargc = 0; char** argv_copy = argv; while(++argv_copy) ++myargc; </code></pre> <p>The <code>while(++argv_copy)</code> will first increment the pointer of the array, letting it point to the next element (e.g., after the first iteration it will point at c-string #2 (<code>argv[1]</code>)). After that, if the pointer evaluates to false (if it is <code>NULL</code>), then the loop brakes and you have your <code>myargc</code>. :)</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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