Jump to content

Recommended Posts

Posted (edited)

Tenho uma função Game que retorna a variavel pc e a variavel pc é um valor double calculado dentro dessa função. Agora eu tenho outra função chamada Color que vai fazer uns cálculos com o valor da variavel pc calculada na variavel Game.

Como posso usar o valor da variavel pc na função Color?

Obrigado

Edited by pmg
ajuste depois de split de topico
Posted

Se tens uma questao nova faz um topico novo. Assim ficam os topicos organizados por tema e torna a gestao, e procura mais facil.

Nao percebi bem a tua duvida. Se a funcao devolve o valor que queres, mete-o numa variavel e usa-o. Estou a ver alguma coisa errada? ???

int Game(void) {
   return 42;
}
void Color(void) {
   int pc = Game();
   /* usa pc */
}

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Posted (edited)

alernativa N :

void game(int * i_am_pointing_to_a_value)
{
 *i_am_pointing_to_a_value = 42;
}
void color(int * i_am_pointing_to_a_value)
{
 *i_am_pointing_to_a_value /= 2;
}
int main()
{
 int value = 0;
 game(&value);
 color(&value);
 return 0;
}

alternativa N+1:

void game(int * i_am_pointing_to_a_value)
{
 *i_am_pointing_to_a_value = 42;
}
void color(int * i_am_pointing_to_a_value)
{
 *i_am_pointing_to_a_value /= 2;
}
int main()
{
 int * i_will_point_to_a_value;

 if ((i_will_point_to_a_value = malloc(sizeof int)) == NULL)
   exit(-1);

 game(i_will_point_to_a_value);
 color(i_will_point_to_a_value);

 free(i_will_point_to_a_value);
 return 0;
}

alternativa N+2:

int * game()
{
 int * please_free_me_in_the_future;

 if ((please_free_me_in_the_future = malloc(sizeof int)) == NULL)
   return NULL;

 *please_free_me_in_the_future = 42;
 return please_free_me_in_the_future;
}
void color(int * i_will_be_free)
{
 printf("%d\n", i_will_be_free);
 free(i_will_be_free);
}
int main()
{
 int * memory;

 if ((memory = game()) == NULL)
   exit(-1);

 color(memory);
 return 0;
}

alternativa N+3:

int please_dont_use_this_alternative = 0;
void game()
{
 please_dont_use_this_alternative = 42;
}
void color()
{
 printf("%d\n", please_dont_use_this_alternative);
}
int main()
{
 game();
 color();
}
Edited by pmg
Murphy's Law (havia um "void main()")
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.