2017-01-06 3 views
0

タスク3:私はGPtrArrayを印刷するためにg_ptr_array_foreachを使用しています...私によれば...これは私たちがそれを行う方法です..しかし、まだ私はセグメンテーションフォールトを取得しています。同僚ですか? :)セグメンテーションフォールト: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の要素を印刷するにはいくつかの方法がありますか?

答えて

2

glibのforeach機能の2番目の引数は、次のプロトタイプと一致する必要があります。

void funcname (gpointer data, gpointer user_data); 

printfは、そのプロトタイプと一致していません。 (GFunc)printfでキャストを削除すると、コンパイラはこれを通知します。あなたのコードにバグが何であるかを伝えるメッセージを隠すために、キャストを使うのは良い考えではありません。

+0

oh ..私はプログラムのどこかで私のprintf()関数を明示的に書く必要があると思います。私は書いていますか? –

+1

そのプロトタイプと一致する関数を作成する必要があります。その関数は、その中にprintfを呼び出す可能性があります。私はこれらのglib関数を使用するサンプルプログラムのコンサルティングをお勧めします –

+0

ありがとうございました。:) –

関連する問題