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 ..
#include<stdio.h>
#include<string.h>
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])) ;
}
thanks in advance..
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,
unsigned char **newPrompt1 =NULL;
newPrompt1 = au1CLIPromptStrings;
printf("a1 = %s and a2 = %s\n", newPrompt1[3],newPrompt1[4]);
#include<stdio.h>
#include<string.h>
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]);
}
au1CLIPromptStrings. So you mean it prints NULL fornewPrompt1[3]andnewPrompt1[4]?