0
Is This Statements Are Same And If Yes Then How?
Is Z = *(ptr1) * *(ptr2); Equal to Z = (*ptr1) * (*ptr2); And Z= *ptr1 * *ptr2 ;
9 Answers
+ 4
As I understand it, the use of parentheses is only necessary when we use pointer arithmetic, as in if we do `*(ptr1 + 2) * *(ptr2 + 5)` because here we are dereferencing from another address rather than the one actually be pointed to by <ptr1> or <ptr2>.
(Edit)
The value of <Z> are the same for the 3 assignments above.
P.S. Specify language in Relevant Tags please.
+ 3
When a pointer is used with array like that, & (address-of operator) is not necessary, because when an array is assigned to a pointer (<ptr1>), array <x> here is decayed into an address of the first element in the array <x> itself.
Read more about array decay in the response written by ~ swim ~ in the following thread:
https://www.sololearn.com/Discuss/2043862/?ref=app
Try this code (paste in main).
int x[5] = {1,2,3,4,5};
printf("Array <x> => %x\n", x);
printf("Address of array <x> => %x\n", &x);
printf("Address of first element in array <x> => %x\n", &x[0]);
int *ptr = x;
// array <x> decays into address of
// first element in it.
printf("Pointer <ptr> points to address => %x\n", ptr);
+ 3
You're welcome Aman
BTW you can edit your reply or your original post above if you want. Sometimes autocorrect can be annoying I know : )
+ 2
Thanks for adding language specification in tags đ
+ 2
Hii Ipang thankyou very much for explain me.. đ my doubles are clear now.
+ 2
Sorry *doubts
+ 2
Next Time I'll take care of this. đ. Thank you.
+ 1
Ipang understand
Int x[10];
int *ptr1 = x; is valid while x is array and size is 10
And int *ptr1 = &x; is invalid.
Whyyyy????
+ 1
As I know & operator indicates address of variable!!!!