0

I have a code where I use a pointer

One of the functions is in file 1

In the main file I call the function of file 1

When I pass the pointer it is not being defined,

I get Test is NULL message

What is the correct way for me to use this?

my code:

File 1

struct mystruct {
    unsigned short id;
    int number;
    ....
}

struct mystruct *test_check(state *ck, char *name);

void GetMytest(state *ck, char *name, struct mystruct *test) {
        checkfield(ck, name);
        test = test_check(ck, name);
        .....
}

Main file

struct mystruct *test

void MainTest() {

    state *ck = check_new();
    .....

    GetMytest(ck, "Stats", test);
    
    if(test == NULL)
        printf("Test is NULL");

}
carl
  • 67
  • 6
  • 1
    We need at least the `test_check` definition. Declarations (and even better definitions) of the other functions and types would be helpful too – Pignotto Jan 26 '23 at 06:56
  • 3
    This is C, therefore, `GetMytest(ck, "Stats", test);` cannot possibly modify the value held by the pointer `test`, as the local by-value argument shadows the global `test` (which is NULL on inception as a global and passed from `main`). `test = ...` means *nothing* to the caller of `GetMytest` (i.e. `main`). – WhozCraig Jan 26 '23 at 07:00
  • 1
    This a classic error. Changing `test` inside `GetMytest` will **not** change `test` in `main`. C uses pass by value, **not** pass by reference. So either pass a pointer to `test` or let the function return the new value and assign it to `test`. Since `test` is global you could in principle remove it as function argument. Then it would work but global variables should be avoided. – Support Ukraine Jan 26 '23 at 07:00
  • Here is a few links explaining it (a little search will give you many. many more): https://stackoverflow.com/questions/2229498/passing-by-reference-in-c https://stackoverflow.com/questions/72311632/what-does-pass-by-value-mean-exactly-in-c https://stackoverflow.com/questions/14149959/why-would-i-pass-function-parameters-by-value-in-c – Support Ukraine Jan 26 '23 at 07:08
  • Please provide a [mre], skipping includes and headers and the `...........` leave to much to guessing. – Yunnosch Jan 26 '23 at 07:17
  • @yunnosch I don't see the need to post more of the code, the problem was obvious just by looking at the code – Megasantos Jan 26 '23 at 08:23

1 Answers1

3

The statement test = test_check(ck, name); in GetMytest only modifies the local argument variable test, not the global variable by the same name.

If you want to update the pointer in the calling scope (the MainTest function or the global scope), you must pass a pointer to this variable.

I do something similar in some of my code

Try as follows

File 1:

void GetMytest(state *ck, char *name, struct mystruct **test) {
        checkfield(ck, name);
        *test = test_check(ck, name);
        .....
}

Main function:

void MainTest() {

    state *ck = check_new();
    .....

    GetMytest(ck, "Stats", &test);
    
    if (test == NULL)
        printf("Test is NULL");
}

If the other functions are correct this should solve the problem.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Megasantos
  • 156
  • 3