更改静态数组

发布于 2024-09-17 01:54:53 字数 351 浏览 8 评论 0原文

我在文件中声明了一个静态变量:

static char *msgToUser[] = {
    "MSG1                ", 
    "MSG2                ",
};

在一个类的方法之一中,我正在执行以下操作:

void InfoUser::ModifyMsg( BYTE msgIdx, char *msgString ){
    strncpy( msgToUser[ idx ], msgString, DISPLAY_SIZE );
}

当我执行 strncopy 时,程序崩溃。我不确定我做错了什么

I have a static variable declared in a file:

static char *msgToUser[] = {
    "MSG1                ", 
    "MSG2                ",
};

Inside one of the methods of a class I'm doing this:

void InfoUser::ModifyMsg( BYTE msgIdx, char *msgString ){
    strncpy( msgToUser[ idx ], msgString, DISPLAY_SIZE );
}

When I do the strncopy the program crashes. I'm not sure what I'm doing wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪之魂 2024-09-24 02:00:44

您已将 msgToUser 定义为字符指针向量。这些字符指针指向存储在已标记为只读的内存中的字符串(字符数组)(在 Microsoft 的 Visual Studio 中,您可以通过编译器选项更改此设置)。

因此,如果您更改此内存,处理器将引发异常(您正尝试在只读内存中写入),并且您的应用程序将崩溃。

You have defined msgToUser as a vector of char-pointers. These char-pointers point to strings (character arrays) that are stored in memory that has been marked as read-only (In Microsoft's Visual Studio you can change this via a compiler option).

Therefore, if you change this memory, the processor will raise an exception (you are trying to write in read-only memory) and your application will crash.

此刻的回忆 2024-09-24 02:00:00

不要将数组保留为指针数组,而是将其设为二维字符数组,这将使其分配空间。

现在,由于它是一个 char * 数组,并且使用字符串文字进行初始化,因此当您尝试覆盖字符串文字的只读内存时,它会崩溃。

Instead of keeping your array as an array of pointers, make it a two dimensional array of characters which will make it allocate space.

Now, since it is an array of char * and initialization is happening using string literals, when you try to overwrite read only memory of string literals, it crashes.

治碍 2024-09-24 01:59:19

请参阅 C-常见问题解答。 问题 1.32

See C-FAQ. Question 1.32

凉风有信 2024-09-24 01:58:30

您定义的数组是一个指向字符串的指针数组;每个字符串都是一个文字(即,解释为指针的带引号的字符串) - 这意味着它是一个常量,即使您没有这样声明它。您无法修改字符串文字。

如果您希望能够修改它们,可以使用显式数组分配:

// Note: The space padding isn't needed if all you require is that the string
// be able to hold DISPLAY_SIZE characters (incl the null terminator)
static char str_1[DISPLAY_SIZE] = "MSG1                ";
static char str_2[DISPLAY_SIZE] = "MSG1                ";
static char *msgToUser[] = { str_1, str_2 };

The array you have defined is an array of pointers to character strings; each character string is a literal (ie, a quoted string interpreted as a pointer) - this means it's a constant, even if you didn't declare it as such. You cannot modify string literals.

If you want to be able to modify them, you can use an explicit array allocation:

// Note: The space padding isn't needed if all you require is that the string
// be able to hold DISPLAY_SIZE characters (incl the null terminator)
static char str_1[DISPLAY_SIZE] = "MSG1                ";
static char str_2[DISPLAY_SIZE] = "MSG1                ";
static char *msgToUser[] = { str_1, str_2 };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文