Re: g_array_new problem with initializer element



On Mon, 2006-12-25 at 09:19 +0100, David NeÄas (Yeti) wrote:
On Mon, Dec 25, 2006 at 12:22:50AM -0500, Tony Freeman wrote:
What am I doing wrong?  I would like an array of gchar values.  The
gchar values would be "lx1" "lx2" "dx1" that a user would select from a
GtkTreeView.

Why you ceate some GArray instead of a GtkListStore -- you
will have to create a tree model anyway, so why not use it
directly as the storage?

However, the following line gives an error:

GArray *serverlist = g_array_new(TRUE, FALSE, sizeof(gchar));

"lx1" and "lx2" do not look like single characters, so this
is probably wrong.  You created an array of *characters*,
not an array of strings.

The error is:

main.c:58: error: initializer element is not constant

serverlist is a global variable, right?  Global variables
have to be initialized with constants in C.  You can move
the initializer to main() or some initialization function,
but global variables are Evil and in most cases the best
approach is to avoid them anyway.

Yeti

Thanks Yeti,

Your hint about creating an array of characters as opposed to an array
of strings pointed me in the right direction.  I've changed the code to
create a null terminated string array with this command:

serverlist = g_strsplit(tmp_server_list, " ", -1);


Here's the edited down version of the code:

void on_executebutton_clicked (GtkWidget *widget, gpointer data)
{
        /* **************************************
            NOTE: THE FOLLOWING ARE GLOBALS:
                treeview_servers : treeview widget
                list_of_servers  : a string to use for a label
                serverlist       : string array of server names
         * **************************************/
        
        GtkTreeSelection *servers_selection;
        GtkTreeModel *servers_model;
        GList *list = NULL;
        gchar *tmp_server_list = "";
        
        /* POPULATE GLOBAL 'serverlist' and 'list_of_servers' */
        
        /* get the list of selected servers */
        servers_selection =
gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview_servers));
        servers_model =
gtk_tree_view_get_model(GTK_TREE_VIEW(treeview_servers));
        machines = gtk_tree_selection_get_selected_rows(servers_selection,
&servers_model);
        
        for (list = machines; list != NULL; list = g_list_next(list)) {
                GtkTreeIter iter;
                GtkTreePath *path = list->data;
                if (gtk_tree_model_get_iter(servers_model, &iter, path)) {
                        gchar *text;
                        gtk_tree_model_get(servers_model, &iter, 1, &text, -1);
                        tmp_server_list = g_strjoin(" ", tmp_server_list, text, NULL);
                }
                gtk_tree_path_free(path);
        }
        g_list_free(list);
        
        g_strstrip(tmp_server_list);
        list_of_servers = tmp_server_list;
        serverlist = g_strsplit(tmp_server_list, " ", -1);
        
        gtk_widget_show(confirmdialog);

}


I can now walk through the string array to build my command:


void on_confirm_okbutton_clicked (GtkWidget *widget, gpointer data)
{
        /* ************************************
           NOTE: THE FOLLOWING ARE GLOBALS:
                serverlist : string array of server names
         * ************************************/

        gchar *ssh_command = "";
        gint i = 0;
        gint count = g_strv_length(serverlist);
        
        /* TODO: build the ssh command and start a process for each server */
        for (i=0; i<count; i++) {
                ssh_command=g_strjoin(" ", "ssh", serverlist[i], MAINSCRIPT_PROGRAM,
command, NULL);
                g_print("SSH Command: %s\n", ssh_command);
        }
        
        /* TODO: run command on each server sending output to temporary text
buffers */
        
        gtk_widget_show(progressdialog);
        gtk_widget_hide(confirmdialog);
}


Thanks for the insight!

-- Tony




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]