修改已初始化的字符串数组

时间:2013-03-19 05:32:08

标签: c

在下面的代码中,如果我删除strncpy它的编译和运行没有seg错误。 但随着strcpy它的抛出错误。 在这两种情况下我都试图修改一个只读地址吗?那么为什么会出现意想不到的行为..

#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])) ;
}

提前感谢..

我现在面临一个问题。我需要传递au1CLIPromptStrings的值 到另一个双指针,这是在许多地方使用的。所以,如果我分配 au1CLIPromptStrings的地址为unsigned char类型的双重指针 在一个结构elemet.Now我无法检索它获得NULL偶数的元素 地址即将到来。我无法在任何地方使用相同的au1CLIPromptStrings varibale, 这是我告诉的一个原型,

 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]);
}

2 个答案:

答案 0 :(得分:2)

您自己的程序经过修改以显示如何使用[][],有助于减少混淆,而不是使用*[]

#include<stdio.h> 
#include<string.h>
int main()
{
unsigned char newPrompt[10]="# ";
unsigned char au1CLIPromptStrings [5][30] =
{
 "",
 "Login: ",
 "Password: ",
  " 0123456789012345678901234",
  " 0123456789012345678901234",
};

printf("a1 = %s and a2 = %s\n",au1CLIPromptStrings[3],au1CLIPromptStrings[4]);

strcpy(au1CLIPromptStrings[3],"# \0");
strcpy(au1CLIPromptStrings[4],"# \0");

printf("a1 = %s and a2 = %s\n",au1CLIPromptStrings[3],au1CLIPromptStrings[4]);

strncpy(au1CLIPromptStrings[3],newPrompt,strlen(au1CLIPromptStrings[3])) ;
strncpy(au1CLIPromptStrings[4],newPrompt,strlen(au1CLIPromptStrings[4])) ;
}

答案 1 :(得分:1)

static unsigned char* au1CLIPromptStrings [] =
{
    "",
    "Login: ",
    "Password: ",
    "0123456789012345678901234",
    "0123456789012345678901234",
};

这定义了一个数组,其中每个成员都是一个指向char类型的指针。初始化后,每个成员都指向一个您无法修改的静态区域。是的,您可以将指针更改为指向另一个内存块,就像您在

中所做的那样
au1CLIPromptStrings[3] = "# \0";
au1CLIPromptStrings[4]  = "# \0";

但实际上你正在改变指针的值(或者只是指向另一个内存块),而不是改变原始内存块。原来的是READ-ONLY。

我的第二个问题的代码:

#include<stdio.h> 
#include<string.h>

int main()
{
    unsigned char **newPrompt1 =NULL;
    static unsigned char* au1CLIPromptStrings [] =
    {
        "",
        "Login: ",
        "Password: ",
        "0123456789012345678901234",
        "0123456789012345678901234",
    };

    newPrompt1 = au1CLIPromptStrings;
    printf("a1 = %s and a2 = %s\n", au1CLIPromptStrings[3],au1CLIPromptStrings[4]);
    printf("a1 = %s and a2 = %s\n", newPrompt1[3],newPrompt1[4]); 

    return 0;
}

在使用gcc的cygwin下打印:

a1 = 0123456789012345678901234 and a2 = 0123456789012345678901234
a1 = 0123456789012345678901234 and a2 = 0123456789012345678901234