Thread: Using pointer like global variables.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Using pointer like global variables.

    Hello, I'm trying to make Blackjack game using C.
    How should I use pointers like global variables?

    For example, if variable named money is on the main function and playing function will increase or decrease money.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I assume you mean something like:
    Code:
    #include <stdio.h>
    
    void func(int *money) {
        *money += 100;
    }
    
    int main() {
        int money = 50;
        func(&money);
        printf("%d\n", money);
        return 0;
    }
    Although you could also do this:
    Code:
    #include <stdio.h>
    
    int func(int money) {
        money += 100;
        return money;
    }
    
    int main() {
        int money = 50;
        money = func(money);
        printf("%d\n", money);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  2. global variables
    By sandeepmhptr in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2007, 08:30 PM
  3. Global variables???
    By Crocklip in forum C Programming
    Replies: 6
    Last Post: 03-05-2006, 11:36 AM
  4. Global Variables
    By jerez_z in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 01:52 PM
  5. C global variables
    By Dohojar in forum C Programming
    Replies: 18
    Last Post: 02-25-2002, 12:00 PM

Tags for this Thread