stummj
asked on
Printing an integer value passed to a function as a pointer
Hi Experts
Im trying to get my head around pointers and passing them into functions as arguments.
Ive got some code - I wont bore you with it at this stage - which seems to sucessfully pass an integer pointer from one function to the other.
However when I try to get to the "real" value and print it out I hit a brick wall. I can print the (correct) memory address, but not the value.
Can someone give me a very simple example of how I can do it?
Its something like:
function1()
{
int test;
int* ptrTest
test = 3;
ptrTest = &test;
function2(&ptrTest)
}
function2 (int *B)
{
printf("%s %i\n","DEBUG = ",*B);
}
function 2 printe the address of B. Ive also tried printing just B and also &B and both seem to print the address too!
I want to print the number 3
Can anyone help?
J
Im trying to get my head around pointers and passing them into functions as arguments.
Ive got some code - I wont bore you with it at this stage - which seems to sucessfully pass an integer pointer from one function to the other.
However when I try to get to the "real" value and print it out I hit a brick wall. I can print the (correct) memory address, but not the value.
Can someone give me a very simple example of how I can do it?
Its something like:
function1()
{
int test;
int* ptrTest
test = 3;
ptrTest = &test;
function2(&ptrTest)
}
function2 (int *B)
{
printf("%s %i\n","DEBUG = ",*B);
}
function 2 printe the address of B. Ive also tried printing just B and also &B and both seem to print the address too!
I want to print the number 3
Can anyone help?
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi stummj,
1) &Test does mean "the address of variable Test".
2) The address of a variable is often passed by preceding the variable name with '&'. Consider the entire scanf() family of functions. They need to store the converted data in the input string (or stream) so they need the address of the variable. And they are usually passed by preceding the variable name with '&'. (Note that C already considered arrays to be addresses so anything defined as char[] or int[] etc. doesn't need the '&'.)
However, sometimes you really do want to save the address in a variable. Any time you allocate memory via malloc() you'll want to save the starting address so you can use the memory and later free it. In this case, you'll save the address in a pointer variable and pass the variable contents (the address) to a function that expects an address.
How your program defines and passes addresses is usually dependent upon how the address is used elsewhere in the program.
Kent
1) &Test does mean "the address of variable Test".
2) The address of a variable is often passed by preceding the variable name with '&'. Consider the entire scanf() family of functions. They need to store the converted data in the input string (or stream) so they need the address of the variable. And they are usually passed by preceding the variable name with '&'. (Note that C already considered arrays to be addresses so anything defined as char[] or int[] etc. doesn't need the '&'.)
However, sometimes you really do want to save the address in a variable. Any time you allocate memory via malloc() you'll want to save the starting address so you can use the memory and later free it. In this case, you'll save the address in a pointer variable and pass the variable contents (the address) to a function that expects an address.
How your program defines and passes addresses is usually dependent upon how the address is used elsewhere in the program.
Kent
Yes, that's correct, it's the memory address. So yes, you can skip using ptrTest. The reason. It would get rather messy writing a program that way, but there's certainly nothing that would technically prevent you from doing it.
ASKER
Brilliant. Excellent clar explaination.
Many thanks
Many thanks
ASKER
Can I ask though. &Test means "The memory address of variable &Test" right?
So I could do that without creating ptrTest in the first place?
So why bother? Why not just use that method of &VariableName when passing a "pointer"?
Julian