|
|
View previous topic :: View next topic |
Author |
Message |
Uwe Guest
|
Problem with C |
Posted: Sun Mar 17, 2002 3:01 pm |
|
|
How can I change the value of different global variables with the same function ?
Example program:
int number_1
int number_2
int number_3
INCREMENT_NUMBER(int number_x)
{
number_x = number_x + 1;
}
main()
{
number_1=0;
number_2=0;
number_3=0;
INCREMENT(number_1);//number_1 should now be 1
INCREMENT(number_2);//number_2 should now be 1
INCREMENT(number_3);//number_3 should now be 1
}
How must I change the function INCREMENT_NUMBER() to change
number_1 to number_3 and not only number_x ?
___________________________
This message was ported from CCS's old forum
Original Post ID: 3279 |
|
|
Tyler Coen Guest
|
Re: Problem with C |
Posted: Sun Mar 17, 2002 3:34 pm |
|
|
If you are using global variables, then you don't need to pass them as parameters to the routine.
eg;
int num1, num2, num3;
void IncrementAll() {
num1++;
num2++;
num3++;
}
//or
void IncrementNumber(int num) {
switch(num) {
case 1: num1++; break;
case 2: num2++; break;
case 3: num3++; break;
}
}
//or
void ExchangeNumbers() {
int temp = num1;
num1 = num2;
num2 = temp;
}
Is this what you were asking?
:=How can I change the value of different global variables with the same function ?
:=
:=Example program:
:=
:=int number_1
:=int number_2
:=int number_3
:=
:=
:=INCREMENT_NUMBER(int number_x)
:={
:= number_x = number_x + 1;
:=}
:=
:=
:=main()
:={
:= number_1=0;
:= number_2=0;
:= number_3=0;
:=
:= INCREMENT(number_1);//number_1 should now be 1
:= INCREMENT(number_2);//number_2 should now be 1
:= INCREMENT(number_3);//number_3 should now be 1
:=}
:=
:=How must I change the function INCREMENT_NUMBER() to change
:=number_1 to number_3 and not only number_x ?
___________________________
This message was ported from CCS's old forum
Original Post ID: 3283 |
|
|
John Purbrick Guest
|
Re: Problem with C |
Posted: Sun Mar 17, 2002 11:26 pm |
|
|
:=How can I change the value of different global variables with the same function ?
:=
If I'm understanding your question, it would seem as though you need to use pointers to make this work. I've changed your axample program to make number_x in the function come in as a POINTER to the quantity, and what gets sent from main() is the ADDRESS of the quantity to be changed, not the contents of that address. Is this what you wanted?
Incidentally, you've named the function INCREMENT_NUMBER and then called it with the name INCREMENT, but that looks like a typo.
:=Example program:
:=
:=int number_1
:=int number_2
:=int number_3
:=
:=
:=INCREMENT_NUMBER(int *number_x)
:={
:= *number_x = *number_x + 1;
:=}
:=
:=
:=main()
:={
:= number_1=0;
:= number_2=0;
:= number_3=0;
:=
:= INCREMENT(&number_1);//number_1 should now be 1
:= INCREMENT(&number_2);//number_2 should now be 1
:= INCREMENT(&number_3);//number_3 should now be 1
___________________________
This message was ported from CCS's old forum
Original Post ID: 3288 |
|
|
Uwe Guest
|
Thank you very much !!!!!!!!!!!!!! |
Posted: Mon Mar 18, 2002 12:38 am |
|
|
Thank you very much for your fast answer.
You are right, I have made a mistake calling the function
INCREMENT() it must be INCREMENT_NUMBER() in main program.
I think using pointers is what I have to do.
Many thanks
Uwe
:=:=How can I change the value of different global variables with the same function ?
:=:=
:=
:=If I'm understanding your question, it would seem as though you need to use pointers to make this work. I've changed your axample program to make number_x in the function come in as a POINTER to the quantity, and what gets sent from main() is the ADDRESS of the quantity to be changed, not the contents of that address. Is this what you wanted?
:=
:=Incidentally, you've named the function INCREMENT_NUMBER and then called it with the name INCREMENT, but that looks like a typo.
:=
:=
:=:=Example program:
:=:=
:=:=int number_1
:=:=int number_2
:=:=int number_3
:=:=
:=:=
:=:=INCREMENT_NUMBER(int *number_x)
:=:={
:=:= *number_x = *number_x + 1;
:=:=}
:=:=
:=:=
:=:=main()
:=:={
:=:= number_1=0;
:=:= number_2=0;
:=:= number_3=0;
:=:=
:=:= INCREMENT(&number_1);//number_1 should now be 1
:=:= INCREMENT(&number_2);//number_2 should now be 1
:=:= INCREMENT(&number_3);//number_3 should now be 1
___________________________
This message was ported from CCS's old forum
Original Post ID: 3289 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|