0

I want to share accountrefence variable to another c file where the other c file has accountrefence = NULL; and is having another called function which is using accountrefence variable.

The second function always sees the variable as NULL. how can I share the variable between the 2 called function so that whenever the variable changes the 2nd called function sees it and do operations upon it? the functions are in the first c file the main is in the second c file


    EN_serverError_t isValidAccount(ST_cardData_t* cardData, ST_accountsDB_t* accountRefrence)
      
        for (int i = 0 ; i < 3; i++)
        {
            //printf("\n%s\n", accountRefrence[i].primaryAccountNumber);
    
            if (!(strcmp(cardData->primaryAccountNumber, accountsDB[i].primaryAccountNumber)))
    
            {   
                accountRefrence = &accountsDB[i];
                return SERVER_OK;
            }           
        }
        return ACCOUNT_NOT_FOUND;
    }
    ```
    EN_serverError_t isBlockedAccount(ST_accountsDB_t* accountRefrence)
    {
        printf("%p", accountRefrence);
    
        if (accountRefrence->state == RUNNING)
        {
            return SERVER_OK;
        }
        else
        {
            return BLOCKED_ACCOUNT;
        }
    }
    ST_transaction_t* accountRefrence = NULL;
    int main(void)
{
    CheckAccountDB = isValidAccount(cardData, accountRefrence);

        if (CheckAccountDB == ACCOUNT_NOT_FOUND)
        {
            printf("\nAccount Doesn't Exist");
        }
        else
        {
            printf("\nAccount Exist");
        }

    CheckAccountState = isBlockedAccount(accountRefrence);
    if (CheckAccountState == SERVER_OK)
    {
        printf("Account is Running");
    }
    else
    {
        printf("Account is Blocked");
    }

    return 0;
}
  • in a header file declare the variable as `external AA* accountrefence;` – rioV8 Dec 20 '22 at 17:29
  • @rioV8 I guess the OP should also remove the assignment to ```NULL``` from the other function/file (it depends of course on the details of the implementation the OP hasn't shared), otherwise the problem will persist... – picchiolu Dec 20 '22 at 17:34
  • i added all the functions with the main – Hassan Bosha Dec 20 '22 at 17:43
  • read a C book about using `external` variables – rioV8 Dec 20 '22 at 17:48
  • In a function like `isValidAccount`, if you set the parameter `accountRefrence` to point somewhere new (as you do in the line `accountRefrence = &accountsDB[i];`), this does **not** change the value of the (global) `accountRefrence` pointer in `isValidAccount`'s caller. See [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function). – Steve Summit Dec 20 '22 at 17:54
  • Hassan Bosha, even with code to achieve your goal, it likely goes against good design practice. Consider passing the info through pointers in the call. Good luck. – chux - Reinstate Monica Dec 20 '22 at 17:58
  • @SteveSummit the problem is that this is a template function I can't change in their arguments, all the solutions are referring to using a pointer to pointer which I'm not able to use – Hassan Bosha Dec 20 '22 at 18:19
  • @HassanBosha Yes, that sounds like a problem. Fundamentally, though, the answer to the question in your title: "Sharing a local variable inside a function to another c file" — is, "You can't." It's the definition of a local variable that it is not visible and therefore cannot be shared outside of the function it's local to. If you want to share data between functions, it may be that you *do* want to use global variables. – Steve Summit Dec 20 '22 at 18:25

0 Answers0