0

I am learning pointers.

int main() {
   int a = 2;
   cal(&a);
}

void cal(int* a) {
   //Here:
  //What does the value of a mean?
  //What does the value of &a mean?
}

As you see above, in main(), I passed the address of a to function cal(int* a).

I am wondering what is the meaning of value a and &a in cal(int*) ?

Is a in cal(int*) represents the address of a in main() ?

Is &a in cal(int*) represents only the address which points to the address of a in main()?

1
  • 7
    Yes you are correct on both accounts. Commented Feb 19, 2015 at 9:32

3 Answers 3

1

What does the value of &a mean?

Take the address of a. This is done by applying the address-operator & to the int variable a.

What does the value of a mean?

Inside cal() a represents the address of an int as having been passed when calling cal().

Sign up to request clarification or add additional context in comments.

Comments

0

a variable in main() function is an integer variable. a variable which is an argument in function cal() is pointer to integer which will point or hold the address of variable a in main() .

Is a in cal(int*) represents the address of a in main() ?

yes

Is &a in cal(int*) represents only the address which points to the address of a in main()?

yes

&a in cal() means address of variable a in cal() that holds the address of a in main() function.

From your example ,you might have got confused by the usage of same variables names.But there scope is limited to there respective fucntions and they dont conflict.

Comments

0

In the your code you have two scopes:

On the main scope 'a' is a variable with value 2, and a pointer to it. You pass the pointer to the function 'cal'.

On the cal scope 'a' is the pointer given as an input from the main function. &a would be the pointer to the pointer, and conversely *a is the value the pointer point to (which is 2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.