GTK_Pointer在C中传递结构时出现问题

时间:2018-05-31 02:41:04

标签: c pointers struct gtk

我遇到的问题是将结构(指针)传递给它们初始化的原始函数。

我注意到的:我从函数中得到的进一步初始化结构(因为我将结构从一个函数传递给下一个作为指针),我基本上开始丢失数据(或者引用数据) )。当我尝试打印结构中的一个整数并且它会给我正确的值,然后交替进入垃圾(可能是mem位置??),然后再次打印出值并再次随机丢弃。

程序:在下面的程序中,我正在尝试创建一组5个标签,我可以在其中按下向上或向下按钮来更改文本。标签附加到事件框,现在,将标签的文本打印到终端上。我在尝试检索文本时遇到了分段错误。

结果是当我尝试使用struct中的数据(更改struct中的标签文本等)时,我遇到了seg错误。

struct Alpha{
GtkWidget *alphalbl[5]; //array of label widgets
gchar *alpha[38];       //array of characters
gint start;             //start position
gint pos;               //current position
};

void createWindow(GtkWidget *widget, gpointer data){

struct Alpha alpha;     //Initialization of structure

/* Create window, layout, notebook, menu, etc*/

createList(&alpha, &layer)  //layer is a struct of fixed layouts 

}

结构在以下函数中起作用:

void createList(gpointer structalpha, gpointer structlayers){

GtkWidget *alphaup, *alphadn;
GtkWidget *alphabox[5];

struct Layers *layer = structlayers;
struct Alpha *alphalist = (struct Alpha *)structalpha;

gchar *alphal[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","<","[]"};

layer->fixedl = gtk_fixed_new();

for (int a = 0; a < 38; a++)                    //Add chararray to struct
    alphalist->alpha[a] = g_strdup(alphal[a]);

alphalist->start = 0;           

for(int i = 0; i < 5; i++){
    alphalist->alphalbl[i] = gtk_label_new(alphalist->alpha[i]);
    alphabox[i] = gtk_event_box_new();
    gtk_widget_set_size_request(alphabox[i], 20, 20);
    gtk_widget_set_name(GTK_WIDGET(alphabox[i]), g_strdup_printf("%d",i));
    gtk_container_add(GTK_CONTAINER(alphabox[i]), alphalist->alphalbl[i]);
    gtk_widget_set_events(alphabox[i], GDK_BUTTON_PRESS_MASK);
    //g_signal_connect(alphabox[i], "button_press_event", G_CALLBACK(setLetter), (alphalist->alphalbl[i]));    //originally tried sending just the single label
    g_signal_connect(alphabox[i], "button_press_event", G_CALLBACK(setLetter), &alphalist);  //<--struct does not send correctly
}

alphaup = gtk_button_new_with_label("^");
gtk_widget_set_size_request(alphaup, 50, 20);
g_signal_connect(alphaup, "clicked", G_CALLBACK(alphaUp), alphalist); //<--struct does not send correctly

alphadn = gtk_button_new_with_label("v");
gtk_widget_set_size_request(alphadn, 50, 20);
g_signal_connect(alphadn, "clicked", G_CALLBACK(alphaDown), alphalist); //<--struct does not send correctly

for(int y = 0; y < 5; y++)
    gtk_fixed_put(GTK_FIXED(layer->fixedl), alphabox[y], 5, (50 + (y*15)));

}

结构不再适用(在setLetter中)。 “ - &gt;”仍然找到变量,但是当我运行代码时我得到分段错误,除非我注释掉gtk_get_text ...我已经打印了alphalist-&gt;从这个函数开始只是为了让它间歇性地工作而不改变它的值。我试过添加'&amp;'回调中的alphalist没有成功。

void setLetter(GtkWidget *alphabox, gpointer alphal){

    struct Alpha *alphalist = alphal;   
    gchar *charnum;

    int num;
    charnum = (gtk_widget_get_name(alphabox));
    num = atoi(charnum);

    g_print(gtk_label_get_text(GTK_LABEL(alphalist->alphalbl[num])));
    g_free(charnum);

}

是否没有为结构正确分配内存的问题?调用malloc()会解决我的问题吗?如果是这样的地方和方式?我试过添加它没有成功。任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

好吧,我确实得到了一个测试程序来处理main。我必须稍微修改它以适应我之前做的事情。需要注意的是,当在原始程序中传递结构时,除非使用gpointer,否则我将遇到编译器错误。之前的函数类型错误。在这里它完美无缺。我还包括了struct的构造函数。我将看看我是否可以获得原始修改和工作。感谢...

#include <gtk/gtk.h>

struct Alpha{
     GtkWidget *label[5];
     gchar *num[10];
     gint start;
};

struct Alpha *Alpha_new(void){
     struct Alpha *p = g_malloc(sizeof *p);
}

void Alpha_delete(struct Alpha *p, gpointer data){
    g_free(p);
}

void createWindow(GtkWidget **window, struct Alpha *alpha, gint x, gint y);
void setLabel(struct Alpha *alpha, GtkWidget *button);
void printNum(struct Alpha *alpha);

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

    GtkWidget *window;

    struct Alpha *alpha;
    alpha = Alpha_new();

    gtk_init(&argc, &argv);

    createWindow(&window, alpha, 600, 400);

    gtk_widget_show_all(window);
    gtk_main();

}
void createWindow(GtkWidget **window, struct Alpha *alpha, gint x, gint y){

    GtkWidget *fixed, *label, *button;
    gchar *num[] = {"0","1","2","3","4","5","6","7","8","9"};

    *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(*window), x, y);

    fixed = gtk_fixed_new();

    label = gtk_label_new("Struct Test");
    gtk_fixed_put(GTK_FIXED(fixed), label, 280, 50);

    button = gtk_button_new_with_label("TEST");
    gtk_fixed_put(GTK_FIXED(fixed), button, 280, 250);

    for(int y = 0; y < 10; y++)
        alpha->num[y] = g_strdup(num[y]);

    for(int a = 0; a < 10; a++)
        g_print(alpha->num[a]);

    g_print("----");   

    printNum(alpha);

    for(int i = 0; i < 5; i++){
        alpha->label[i] = gtk_label_new(alpha->num[i]);
        gtk_fixed_put(GTK_FIXED(fixed), (alpha->label[i]), 310, (100 + (15*i)));
    }

    alpha->start = 1;
    gtk_container_add(GTK_CONTAINER(*window), fixed);

    g_signal_connect_swapped(button, "clicked", G_CALLBACK(setLabel), alpha);
    g_signal_connect_swapped(*window, "destroy", G_CALLBACK(Alpha_delete), alpha);
 }

 void printNum(struct Alpha *alpha){
    for(int i = 0; i < 10; i++)
        g_print(alpha->num[i]);
 }

 void setLabel(struct Alpha *alpha, GtkWidget *button){
     int start = alpha->start;

     for(int i = 0; i < 5; i++){
         if(i + start > 9)
             gtk_label_set_text(GTK_LABEL(alpha->label[i]), alpha->num[(i + (start-10))]);
        else
             gtk_label_set_text(GTK_LABEL(alpha->label[i]), alpha->num[(i + start)]);
    }

    (alpha->start)++;

    if(alpha->start == 10)
        alpha->start = 0;

 }

答案 1 :(得分:0)

g_signal_connect(alphabox[i], "button_press_event", G_CALLBACK(setLetter), &alphalist); //<--struct does not send correctly

createlist alphalist指向struct Alpha的指针。您尝试将指针传递给该指针,即指向struct Alpha 的指针的指针。这是一个特定的错误,因为这个确切的值是在堆栈上分配的,当你从createlist返回时不再有效。

如果第一个代码块不是main而是函数,则会出现同样的问题。