1

I have created the following two files to illustrate what my problem is.

main.c

#include <gtk/gtk.h>
#include "app_struct.h"

static void activation(GtkApplication *app, gpointer user_data);
static void check_file(GFile *file);

int main(int argc, char **argv)
{
        int status;

        GtkApplication *test = gtk_application_new("idk.for.now.test", G_APPLICATION_FLAGS_NONE);
        g_signal_connect(test, "activate", G_CALLBACK(activation), NULL);
        status = g_application_run(G_APPLICATION(test), argc, argv);
        g_object_unref(test);
        return status;
}

static void activation(GtkApplication *app, gpointer user_data)
{
        // create app my_struct
        struct app_struct my_struct;

        g_print("%d\n", my_struct.file);
        // set no file
        my_struct.file = NULL;
        g_print("%d\n", my_struct.file);
        check_file(my_struct.file);
        g_print("%d\n", my_struct.file);

        // add application to my_struct
        my_struct.app = app;
}

static void check_file(GFile *file)
{
        g_print("%d\n", file);
        file = (GFile *) 0xdeadbeef;
        g_print("%d\n", file);
}

app_struct.h

#ifndef APP_STRUCT_H
#define APP_STRUCT_H
struct app_struct
{
        GtkApplication *app; 
        GFile *file;
};
#endif

I want to modify the original file pointer in the check_file function, but I find that I cannot do that for some reason.

Here is what I get when I run this program:

-1137322208
0
0
-559038737
0

It seems that check_file function gets only the copy of my_struct.file, but since it accepts a pointer, shouldn't the value of my_struct.file, which is an address, be assigned to GFile *file, which is supposed to be set to an address, as if I wrote GFile *file = my_struct.file;? Then file and mystruct.file would point to the same location in memory.

Hanlon
  • 432
  • 4
  • 13
  • 3
    Pointers are passed by value too. You'll need to pass a pointer to the pointer, or return the new pointer. – Ian Abbott Sep 07 '18 at 16:34
  • 1
    `my_struct.file` in `activation` and `file` in `check_file` are different pointer objects that are pointing to the same thing. Changing the value of one of the pointer objects does not magically change the value of the other one. – Ian Abbott Sep 07 '18 at 16:39
  • There is a duplicate, I just do not have access to my dupe lib now. – Yunnosch Sep 07 '18 at 16:43
  • 1
    Also, `%d` is not the correct format specifier for printing a pointer value. Use `%p` instead, but technically, that needs a `void *`, so you need to cast the pointer to `void *` in the `g_print` like this (I'm assuming `g_print` follows the same rules as `printf`): `g_print("%p\n", (void *)file);`. – Ian Abbott Sep 07 '18 at 16:44

1 Answers1

2

What about this: if you want to change the value pointed by file, you must pass a pointer on the pointer...

static void check_file(GFile **file)
{
        g_print("%p\n", *file);
        *file = (GFile *) 0xdeadbeef;
        g_print("%p\n", *file);
}

And use it this way :

check_file(&my_struct.file);
Mathieu
  • 8,840
  • 7
  • 32
  • 45