gpt4 book ai didi

c - 段错误 : g_ptr_array_foreach

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:50 25 4
gpt4 key购买 nike

任务 3:我正在使用 g_ptr_array_foreach 来打印 GPtrArray ...根据我的说法...我们就是这样做的...但我仍然遇到段错误...拜托...任何有见识的同行? :)

/************************************************************************************************************
* FILE NAME : ex-garray-7.c
*
* DESCRIPTION : demonstrates implementation of pointer arrays i.e GPtrArray
*
************************************************************************************************************/


#include<stdio.h>
#include<glib.h>

#define SUCCESS 0

/************************************************************************************************************
* FILE NAME : main
*
* DESCRIPTION : allocates memory to pointer array, pointers point to strings
*
* RETURNS : SUCCESS
*
***********************************************************************************************************/

int main(int argc, char** argv)
{

/**********************************NEW LEARNING************************************************************
- all glib functions are perfectly alright
- */

//gpointer ret_val = NULL;

/* GPtrArray is designed to hold pointers,
- no need to specify particular type when creating it or adding and indexing elements. */

/******************************REQUIRED DECLARATIONS*******************************************************/

// declare GPtrArray pointer variable
GPtrArray* ptrarr = NULL;


/********************************REQUIREDD INITIALIZATIONS*************************************************/

// allocate memory to GPtrArray using g_ptr_array_new();
ptrarr = g_ptr_array_new(); /* No argument while allocating memory to array of pointers


/*********************************PERFORMING REQUIRED TASKS************************************************/

/* 1. add string hello to it using
g_ptr_array_add(
GPtrArray* array,
g_strdup(<"string">)); */
g_ptr_array_add(
ptrarr,
g_strdup("Let's"));

// 2. add other strings like I love C and gpointers too
g_ptr_array_add(
ptrarr,
g_strdup("Use"));

g_ptr_array_add(
ptrarr,
g_strdup("GLib"));


g_ptr_array_add(
ptrarr,
g_strdup("\n"));


/* 3. print entire GPtrArray using
void g_ptr_array_foreach(
GPtrArray* array,
GFunc function,
gpointer user_data); */
g_ptr_array_foreach(
ptrarr,
(GFunc)printf,
NULL);

printf("\n Removing the third element i.e index is 2");

/* 4. remove the third element using
gpointer g_ptr_array_remove_index(
GPtrArray* array,
guint index_
); */
g_ptr_array_remove_index(ptrarr, 0);



/* 5. remove the new 3rd and 2nd element
gpointer g_ptr_array_remove_range(
GPtrArray* array,
guint index_,
guint length
); */
g_ptr_array_remove_range(ptrarr, 0,1);

// 6. print the new GPtrArray
printf("\n pointer array now is \n ");
g_ptr_array_foreach(
ptrarr,
(GFunc)printf,
NULL);

// 7. print the first item in GPtrArray
printf("\n The first element in the GPtrArray is %s ", g_ptr_array_index(ptrarr, 0)/* same as g_array_index*/);

// 8. free the memory
g_ptr_array_free(ptrarr, TRUE); // same as g_array_free
return SUCCESS;

}

还有其他方法可以打印 GPtrarray 的元素吗?

最佳答案

glib foreach 函数的第二个参数必须匹配以下原型(prototype):

void funcname (gpointer data, gpointer user_data);

printf 与该原型(prototype)不匹配。如果您删除 (GFunc)printf 中的转换,编译器会告诉您这一点。使用强制转换来隐藏告诉您代码中存在错误的消息并不是一个好主意。

因此,您需要使用包装函数:

g_ptr_array_foreach (ptrarr, print_cb, NULL);

static void
print_cb (gpointer data,
gpointer user_data)
{
printf ("%s\n", data);
}

或者,更好的是,只进行内联循环以使事情更清楚并节省多次调用回调的开销:

for (gsize i = 0; i < ptrarr->len; i++)
{
const gchar *element = g_ptr_array_index (ptrarr, i);
printf ("%s\n", element);
}

关于c - 段错误 : g_ptr_array_foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41504818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com