Note that there are some explanatory texts on larger screens.

plurals
  1. POmodifying already initialised array of strings
    primarykey
    data
    text
    <p>In the code below if i remove both strncpy its compiling and running without seg fault. But with strcpy its throwing seg fault. IN both cases i am trying to modify a read only address right ?Then why the unexpected behaviour ..</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;string.h&gt; int main() { unsigned char* newPrompt="# "; static unsigned char* au1CLIPromptStrings [] = { "", "Login: ", "Password: ", "0123456789012345678901234", "0123456789012345678901234", }; /* here am trying to moodify the read only address */ au1CLIPromptStrings[3] = "# \0"; au1CLIPromptStrings[4] = "# \0"; /* removed two strncpy second time */ printf("a1 = %s and a2 = %s\n",au1CLIPromptStrings[3],au1CLIPromptStrings[4]); /* here using strcpy am trying to modify */ strncpy(au1CLIPromptStrings[3],newPrompt,strlen(au1CLIPromptStrings[3])) ; strncpy(au1CLIPromptStrings[4],newPrompt,strlen(au1CLIPromptStrings[4])) ; } </code></pre> <p>thanks in advance..</p> <p>There is one problem i am facing now. I need to pass the values of au1CLIPromptStrings to another double pointer which is bieng used in many places .So if i assign the address of au1CLIPromptStrings to that doublepointer of type unsigned char which is in a structure elemet.Now i am not able to retrive the elements its getting NULL evenif address is coming proper.I cannot use the same au1CLIPromptStrings varibale everywhere , Here is a proto of waht i told,</p> <pre><code> unsigned char **newPrompt1 =NULL; newPrompt1 = au1CLIPromptStrings; printf("a1 = %s and a2 = %s\n", newPrompt1[3],newPrompt1[4]); #include&lt;stdio.h&gt; #include&lt;string.h&gt; int main() { unsigned char *newPrompt="# "; unsigned char **newPrompt1 =NULL; unsigned char au1CLIPromptStrings [7][30] = { "", "Login: ", "Password: ", " 0123456789012345678901234", " 0123456789012345678901234", }; newPrompt1 = au1CLIPromptStrings; // here am assigning the address printf("b1 = %u and b2 = %u\n",newPrompt1,au1CLIPromptStrings); printf("a1 = %s and a2 = %s\n",newPrompt1[3],newPrompt1[4]); } </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. 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