Задача 3: Я использую g_ptr_array_foreach для печати GPtrArray ... По мне ... это так, как мы это делаем .. но все же я получаю ошибку сегментации .. Пожалуйста .. Любой проницательный сверстники? :)Ошибка сегментации: g_ptr_array_foreach
/************************************************************************************************************
* 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?
oh .. Думаю, мне нужно объяснить, что моя функция printf() где-то в программе .. я пишу сэр? –
Вам нужно сделать функцию, соответствующую этому прототипу, который может вызвать printf внутри него. Я бы рекомендовал обратиться к примеру программы, которая использует эти функции glib. –
спасибо sir ... :) –