Gtk Programing 的問題

如果您覺得您的問題不屬於 debian desktop 或是 debian server 版的範圍內,請在這裡發問。

版主: mufa

Gtk Programing 的問題

文章windcloud » 週日 9月 21, 2008 9:14 am

不好意思...因為我不知道要去那裡發問..所以就在這邊貼出我的問題
因為最近我在學用c和gtk+來寫程式..但是現在碰到一個我一直想不懂的問題,下面是我寫的程式碼
代碼: 選擇全部
#include <gtk/gtk.h>
typedef struct _cstring Cstring;
struct _cstring
{
   GString *st1;
   GString *st2;
};

int main(int argc, char** argv)
{
   Cstring str;
   Cstring *sstr;

   sstr=&str;
   str.st1=g_string_new("str1");
   str.st2=g_string_new("str2");
   g_print("%s,%s\n",str.st1->str,str.st2->str);

   sstr->st1=g_string_new("str3");
   sstr->st2=g_string_new("str4");
   g_print("%s,%s\n",sstr->st1->str,sstr->st2->str);
   return 0;
}

在這個程式中我寫了個struct並設定為變數Cstring,那在程式中要使用時是用Cstring str來宣告使用,但是如果是宣告成指標如Cstring *sstr時就必須用sstr=&str讓sstr取得位址後才能使用這個指標變數,但是我想不透的是為什麼在gtk+和homebank這些也是用c所寫的程式可以不用取得位址這個動作就可以直接使用所以設定的struct 指標變數了......
有人可以替小弟解惑嗎....謝謝
windcloud
可愛的小學生
可愛的小學生
 
文章: 35
註冊時間: 週一 11月 29, 2004 11:25 pm

文章訪客 » 週日 9月 21, 2008 4:07 pm

用gdb去trace出那個struct source code吧
訪客
 

文章iamkee » 週日 9月 21, 2008 9:10 pm

可以不用取得位址這個動作就可以直接使用所以設定的struct 指標變數
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

不是很了解你說的意思
可以舉個不用的例子來看看嗎?

在Glib-2.16.5的source code裡
是這樣定義的GString的

typedef struct _GString GString;

struct _GString
{
gchar *str;
gsize len;
gsize allocated_len;
};

其中gchar, gsize又是這樣定義的

typedef char gchar;
typedef unsigned int gsize;

用起來還跟一般的c code一樣沒什麼問題啊!
可以舉個例子嗎?
iamkee
 

文章windcloud » 週一 9月 22, 2008 6:05 am

你好..謝謝您的回答.....
我會有這樣的問題是因為我在看homebank的程式碼的時候
他有個寫法讓我看不懂....
代碼: 選擇全部
typedef struct _operation   Operation;
struct _operation
{
   guint   date;

   gdouble   amount;
   guint   account;
   guint   dst_account;
   gushort   paymode;
   gushort   flags;
   guint   payee;
   guint   category;
   gchar   *wording;
   gchar   *info;
   GList   *same;      //used for import todo: change this
};

代碼: 選擇全部

struct HomeBank
{
   GtkWidget   *mainwindow;   //should be global to access attached window data
   guint32      today;         //today's date

   GList      *acc_list;      //accounts
   GList      *pay_list;      //payees
   GList      *cat_list;      //categories
   GList      *arc_list;      //archives
   GList      *ope_list;      //operations

   // wallet properties
   gchar      *title;
   guint      car_category;
   guint      auto_nbdays;

   // current filename
   gchar      *filename;
   gchar      *oldfilename;
   gboolean   wallet_is_new;
   gboolean   exists_old;

   gint      change;

   gint      define_off;      //>0 when a stat, account window is opened
   gboolean   minor;

   //gchar      fmt_maj_number[16];
   //gchar      fmt_min_number[16];

   // pixbuf datas
   GdkPixbuf   *lst_pixbuf[NUM_LST_PIXBUF];
   gint      lst_pixbuf_maxwidth;



};

代碼: 選擇全部
struct HomeBank *GLOBALS;

代碼: 選擇全部
GList *list;
char buf[G_ASCII_DTOSTR_BUF_SIZE];
gchar *tmpstr;

   list = g_list_first(GLOBALS->ope_list);
   while (list != NULL)
   {
   Operation *item = list->data;

      item->flags &= (OF_VALID|OF_INCOME|OF_REMIND);

      tmpstr = g_markup_printf_escaped(
         "<ope date=\"%d\" amount=\"%s\" account=\"%d\" dst_account=\"%d\" paymode=\"%d\" flags=\"%d\" payee=\"%d\" category=\"%d\" wording=\"%s\" info=\"%s\"/>\n",
         item->date,
         g_ascii_dtostr (buf, sizeof (buf), item->amount),
         item->account,
         item->dst_account,
         item->paymode,
         item->flags,
         item->payee,
         item->category,
         item->wording == NULL ? "" : item->wording,
         item->info == NULL ? "" : item->info
      );

      fprintf(fp, tmpstr);
      g_free(tmpstr);

         list = g_list_next(list);
   }
}

我一直無法理解是為什麼Operation *item = list->data;可以這樣宣告後就可以使用了。
謝謝您的回答
windcloud
可愛的小學生
可愛的小學生
 
文章: 35
註冊時間: 週一 11月 29, 2004 11:25 pm

文章tomjpsun » 週一 9月 22, 2008 10:01 am

延續之前的訪客大大建議用 gdb ,您可以停在這一行觀察 :
猜想應該在這之前,HomeBank 的 ope_list 上面已經有 Operation type 的資料了 。 然後 Operation *item = list->data; 會把整個 Operation type 的資料都複製到 指標 item 所指的位置上(這是基本的 C 語言動作)
tomjpsun
可愛的小學生
可愛的小學生
 
文章: 82
註冊時間: 週一 1月 03, 2005 2:40 pm

文章訪客 » 週一 9月 22, 2008 12:55 pm

windcloud 寫:GList *list;
char buf[G_ASCII_DTOSTR_BUF_SIZE];
gchar *tmpstr;

list = g_list_first(GLOBALS->ope_list);


搞不好list已經被g_list_first給initialize,用gdb去看g_list_first在作什麼。
訪客
 

文章windcloud » 週一 9月 22, 2008 1:24 pm

我了解了.....謝謝大家的幫忙......... :-)
windcloud
可愛的小學生
可愛的小學生
 
文章: 35
註冊時間: 週一 11月 29, 2004 11:25 pm


回到 debian misc

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客