【发布时间】:2021-09-12 01:01:34
【问题描述】:
我运行了我为 GUI 程序编写的这段代码,我得到了一个奇怪的错误,其中一个字符总是按顺序丢失。此代码已简化。
#include <gtk/gtk.h>
int main()
{
GArray *array = g_array_new(FALSE, FALSE, 1000); /* I create the array */
int i; /* An iterator used in the for loop. */
char *question = "What does hypersomatic mean?";
/* The insertion loop. */
for (i = 0; i < 10; i++)
{
/* I pass the array along, the data, and the number of elements to insert. */
array = g_array_append_vals(array, question, 1);
}
/* The reading loop */
for (i = 0; i < 10; i++)
{
/* I pass along the array, the element type (gchar is correct, I think), and the index of the element. */
char *what_is_this = &g_array_index(array, gchar, i);
printf("%s\n", what_is_this);
}
return 0;
}
我使用以下代码编译:gcc `pkg-config gtk+-3.0 --libs --cflags` main.c。
这是输出:
超体是什么意思?
请问hypersomatic 是什么意思?
超体是什么意思?
t 是什么意思?
hypersomatic 是什么意思?
oes 超体型是什么意思?
es 超体是什么意思?
s 超体的意思是什么?
超体平均?
这是什么意思?
【问题讨论】:
-
你为什么把
1000传给g_array_new!?为什么在存储char*时会读取gchar? -
这是每个元素的大小,我想我可以用 29 代替。这就是字符串的大小。
标签: arrays missing-data glib