Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Pass parameters by char *&amp; will fix the problem.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void reassgnReference( char * &amp;ba, char * &amp;num ); void reassgnPointerToPointer( char ** ba, char ** num ); int main( int argc, char ** argv ) { if ( argc &gt; 4 ) { char * ba = new char[strlen( argv[ 4 ] ) + 1]; char * num = new char[strlen( argv[ 2 ] ) + 1]; strncpy( ba, argv[ 4 ], strlen( argv[ 4 ] ) ); strncpy( num, argv[ 2 ], strlen( argv[ 2 ] ) ); printf( "In the beginning, ba = %s, num = %s\n", ba, num ); reassgnReference( ba, num ); printf( "reassgnReference(), ba = %s, num = %s\n", ba, num ); reassgnPointerToPointer( &amp;ba, &amp;num ); printf( "reassgnPointerToPointer(), ba = %s, num = %s\n", ba, num ); } else { printf( "%s Expects at least 4 arguments\n", argv[ 0 ] ); } return( 0 ); } void reassgnReference( char *&amp; ba, char *&amp; num ) { delete[] ba; delete[] num; char const * const newBa = "ba from reference"; char const * const newNum = "num from reference"; ba = new char[strlen( newBa ) + 1]; num = new char[strlen( newNum ) + 1]; strncpy( ba, newBa, strlen( newBa ) ); strncpy( num, newNum, strlen( newNum ) ); } void reassgnPointerToPointer( char ** ba, char ** num ) { delete[] *ba; delete[] *num; char const * const newBa = "ba from pointer to pointer"; char const * const newNum = "num from pointer to pointer"; *ba = new char[strlen( newBa ) + 1]; *num = new char[strlen( newNum ) + 1]; strncpy( *ba, newBa, strlen( newBa ) ); strncpy( *num, newNum, strlen( newNum ) ); } </code></pre>
 

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