Thread: Can't update value of variable outside of the function

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Can't update value of variable outside of the function

    I am passing the address of the variable 'jog' to the function 'aplica_cordenada', and I update its values inside that function's definition, but it is not being updated outside of it. I am aware of variable scope, but since I'm passing its address (&jog), shouldn't it be updated outside the function as well?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define TRUE 1
    #define FALSE 0
    
    /* === PROTÓTIPOS DAS FUNÇÕES. === {{{ */
    int run_game(int);
    void aplica_cordenada(char [], int *);
    /* === Protótipos fim. }}} */
    
    int main(void) { /* {{{ */
    
        /* === VARIABLES === {{{ */
        /* Jogador 1 ou 2. */
        int jog = 1;
        //int *ptr_jog;
        //ptr_jog = &jog;
        /* Mantém o jogo rodando. */
        int run = 1;
        int i = 0, j = 0; /* Iteradores. */
        char casas[10] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
        int cord = 0; 
        /* End variables. }}} */
    
        do { /* {{{ */
            printf("jog %d\n", jog);
            if (jog % 2 != 0) { // {{{ 
                printf("Jogador um...\n\n");
            }
            else {
                printf("Jogador dois.\n\n");
            } // }}}
    
            for (i = 0; i < 10; i++) { // {{{
                printf(" %c ", casas[i]);
                if (i == 2 || i == 5) {
                    printf("\n"); /* Formatar colunas. */
                }
            }
            printf("\n\n"); // }}}
    
            aplica_cordenada(casas, &jog);
    
        } while (run_game(run)); /* Fim de do while. }}} */
    
        return 0;
    } /* main() fim. }}} */
    
    /* === DEFINIÇÕES DAS FUNCÕES. {{{ */
    int run_game(int T) { // {{{
        return TRUE;
    } // }}}
    
    void aplica_cordenada(char casas[], int *ptr_jog) { /* {{{ */
        char opt;
        do {
            printf("Digite uma posição: ");
            opt = getchar();
        } while (!isdigit(opt) || opt == '0');
        opt = atoi(&opt); /* Converter para número. */
        printf("casa %d -- \n", opt);
        if (casas[opt - 1] == '-') {
            casas[opt - 1] = 'x';
            *ptr_jog++;
        }
    } /* Funções cordenada() fim. }}} */
    
    int casa_disponivel(int n) { /* {{{ */
    
        return FALSE;
    } /* }}} */
    
    /* Definiçoes fim }}} */
    
    
    /*
     * vim:foldmethod=marker foldmarker={{{,}}}
     */
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    This line:

    Code:
            *ptr_jog++;
    Does not do what you (seem to) think it does. The ++ operator has higher precedence than the * operator (you generally go right as far as you can - i.e. from "ptr_jog" to "ptr_jog++" then left) so the above is equivalent to:

    Code:
            *(ptr_jog++);
    I.e. the pointer gets incremented, then dereferenced with nothing done with the value. You probably want:

    Code:
            (*ptr_jog)++;

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by JohnGraham View Post
    I.e. the pointer gets incremented, then dereferenced with nothing done with the value. You probably want:

    Code:
            (*ptr_jog)++;
    All right. It worked. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  2. Makefile doesn't update $< variable
    By Kodreanu in forum C Programming
    Replies: 4
    Last Post: 04-13-2011, 10:38 AM
  3. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  4. C++: How can I update a variable
    By nekkron in forum C++ Programming
    Replies: 21
    Last Post: 12-13-2007, 02:35 PM
  5. Buggy Transformation Update Function..
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-04-2006, 04:52 PM