'C' pointers and functions

Soldato
Joined
10 Apr 2004
Posts
13,497
Basically, I need to get two values out of a function.

How does the pointers malarky work? I've googled for a while now and getting no where.

Where do initialize my pointers? (*x and *y)

Then in the function how do I set the values to these pointers?

Eg. I want *x = one value, *y = another value

And then back in the main function how do I recall these values?

&x gets back 'one value', &y gets back 'another value'.

Cheers, final step in my program and it aint working :rolleyes:
 
void IncreaseByOne(int *x, int *y){
//here the magic is done by increasing the original variable, you don't return values when using pointers
*x += 1;
*y += 1;
}

int main(){
int originalX, originalY;
int *pX = &originalX;
int *pY = &originalY;
IncreaseByOne(pX, pY);
}

I hope that clears stuff up for you :p
 
It helps to think of int* (int pointer) as a distinct type, just as int and char are types.

Code:
int someInt = 5;
int* myIntPointer = &someInt; // get the address of someInt
printf("%d", myIntPointer); // print the address of someInt
*myIntPointer += 1 // modify the value of someInt
printf("%d", *myIntPointer); // print the value of someInt
 
Last edited:
Back
Top Bottom