• C

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:

int test;
int* ptrTest
 test = 3;
 ptrTest = &test;

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
Hi stummj,

The problem is in the original call to function2().  ptrTest is already a pointer to an integer.  By passing '&ptrTest' you're actually passing a pointer to a pointer -- not what you want to do.

Change the call to be:




Either of them should give you what you want.

Good Luck!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stummjAuthor Commented:
I used function2(&Test) and it works. Thank you Kent. Once again you have dug me out of a hole. The points are yours.
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"?

Kent OlsenDBACommented:
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.

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.
stummjAuthor Commented:
Brilliant. Excellent clar explaination.
Many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.